-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAttachment.kt
128 lines (109 loc) · 4.51 KB
/
Attachment.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Infomaniak Mail - Android
* Copyright (C) 2022-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.data.models
import android.content.Context
import androidx.core.net.toFile
import androidx.core.net.toUri
import com.infomaniak.lib.core.utils.Utils.enumValueOfOrNull
import com.infomaniak.mail.data.models.draft.Draft
import com.infomaniak.mail.utils.AttachableMimeTypeUtils
import com.infomaniak.mail.utils.LocalStorageUtils
import com.infomaniak.mail.utils.SentryDebug
import io.realm.kotlin.types.EmbeddedRealmObject
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import java.io.File
import java.util.UUID
@Serializable
class Attachment : EmbeddedRealmObject, Attachable {
//region Remote data
var uuid: String = ""
@SerialName("mime_type")
override var mimeType: String = ""
override var size: Long = 0L
override var name: String = ""
@SerialName("disposition")
private var _disposition: String? = null
@SerialName("content_id")
var contentId: String? = null
@SerialName("original_content_id")
var originalContentId: String? = null
override var resource: String? = null
@SerialName("drive_url")
var driveUrl: String? = null
//endregion
//region Local data (Transient)
@Transient
override var localUuid: String = UUID.randomUUID().toString()
@Transient
var uploadLocalUri: String? = null
@Transient
private var _uploadStatus: String = UploadStatus.AWAITING.name
//endregion
val uploadStatus: UploadStatus
get() = enumValueOf<UploadStatus>(_uploadStatus)
val isCalendarEvent: Boolean get() = AttachableMimeTypeUtils.calendarMatches.contains(mimeType)
val disposition: AttachmentDisposition?
get() = enumValueOfOrNull<AttachmentDisposition>(_disposition)
fun initLocalValues(name: String, size: Long, mimeType: String, uri: String): Attachment {
this.name = name
this.size = size
this.mimeType = mimeType
this.uploadLocalUri = uri
return this
}
/**
* After uploading an Attachment, we replace the local version with the remote one.
* The remote one doesn't know about local data, so we have to backup them.
*/
fun backupLocalData(oldAttachment: Attachment, uploadStatus: UploadStatus, draft: Draft) {
localUuid = oldAttachment.localUuid
uploadLocalUri = oldAttachment.uploadLocalUri
setUploadStatus(uploadStatus, draft, "backupLocalData -> setUploadStatus")
}
fun setUploadStatus(uploadStatus: UploadStatus, draft: Draft? = null, step: String = "") {
draft?.let { SentryDebug.addAttachmentsBreadcrumb(it, step) }
_uploadStatus = uploadStatus.name
}
override fun getFileTypeFromMimeType() = AttachableMimeTypeUtils.getFileTypeFromMimeType(safeMimeType)
override fun hasUsableCache(context: Context, file: File?, userId: Int, mailboxId: Int): Boolean {
val cachedFile = file ?: getCacheFile(context, userId, mailboxId)
return cachedFile.length() > 0 && cachedFile.canRead()
}
override fun isInlineCachedFile(context: Context): Boolean {
return getCacheFile(context).exists() && disposition == AttachmentDisposition.INLINE
}
override fun getCacheFile(context: Context, userId: Int, mailboxId: Int): File {
val cacheFolder = LocalStorageUtils.getAttachmentsCacheDir(context, extractPathFromResource(), userId, mailboxId)
return File(cacheFolder, name)
}
private fun extractPathFromResource(): String {
return resource?.substringAfter("folder/")?.replace(Regex("(message|attachment)/"), "") ?: ""
}
fun getUploadLocalFile() = uploadLocalUri?.toUri()?.toFile()
enum class AttachmentDisposition {
INLINE,
ATTACHMENT,
}
enum class UploadStatus {
AWAITING,
ONGOING,
FINISHED,
}
}