-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDraftUtils.kt
81 lines (73 loc) · 3.46 KB
/
DraftUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Infomaniak Mail - Android
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.utils
import com.infomaniak.lib.core.api.ApiController
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.lib.core.utils.isNetworkException
import com.infomaniak.mail.data.cache.mailboxContent.DraftController
import com.infomaniak.mail.data.models.Attachment
import com.infomaniak.mail.data.models.Attachment.UploadStatus
import com.infomaniak.mail.data.models.draft.Draft
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.ATTACHMENT_TAG
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.findSpecificAttachment
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.startUpload
import io.realm.kotlin.Realm
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
val attachmentsUploadMutex = Mutex()
suspend fun uploadAttachmentsWithMutex(
draft: Draft,
mailbox: Mailbox,
draftController: DraftController,
realm: Realm,
): Draft = attachmentsUploadMutex.withLock {
draft.uploadAttachments(mailbox, draftController, realm)
val updatedDraft = DraftController.getDraft(draft.localUuid, realm)!!
return@withLock updatedDraft
}
private suspend fun Draft.uploadAttachments(mailbox: Mailbox, draftController: DraftController, realm: Realm) {
fun getAwaitingAttachments(): List<Attachment> = attachments.filter { it.uploadStatus == UploadStatus.AWAITING }
fun setUploadStatus(attachment: Attachment, uploadStatus: UploadStatus, step: String) {
realm.writeBlocking {
draftController.updateDraft(localUuid, realm = this) {
it.attachments.findSpecificAttachment(attachment)?.setUploadStatus(uploadStatus, draft = it, step)
}
}
}
val attachmentsToUpload = getAwaitingAttachments()
val attachmentsToUploadCount = attachmentsToUpload.count()
if (attachmentsToUploadCount > 0) {
SentryLog.d(ATTACHMENT_TAG, "Uploading $attachmentsToUploadCount attachments")
SentryLog.d(
tag = ATTACHMENT_TAG,
msg = "Attachments Uuids to localUris : ${attachmentsToUpload.map { it.uuid to it.uploadLocalUri }}}",
)
}
attachmentsToUpload.forEach { attachment ->
runCatching {
setUploadStatus(attachment, UploadStatus.ONGOING, step = "before starting upload")
attachment.startUpload(localUuid, mailbox, draftController, realm)
}.onFailure { exception ->
setUploadStatus(attachment, UploadStatus.AWAITING, step = "after failing upload")
SentryLog.d(ATTACHMENT_TAG, "${exception.message}", exception)
if ((exception as Exception).isNetworkException()) throw ApiController.NetworkException()
throw exception
}
}
}