mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-02-17 04:00:48 +01:00
fixed draft mime cache deletion
This commit is contained in:
parent
2e9f1fe83f
commit
bf0b621c16
@ -1 +1 @@
|
||||
43352c4a6d6b59db75d68fe600b3708243096886
|
||||
e9881eb040d94286c884d572947bc39a082c95e0
|
||||
|
@ -9,7 +9,9 @@ import org.apache.james.mime4j.dom.address.Mailbox
|
||||
import org.apache.james.mime4j.dom.field.*
|
||||
import org.apache.james.mime4j.message.*
|
||||
import org.apache.james.mime4j.parser.MimeStreamParser
|
||||
import org.apache.james.mime4j.storage.Storage
|
||||
import org.apache.james.mime4j.storage.StorageBodyFactory
|
||||
import org.apache.james.mime4j.storage.TempFileStorageProvider
|
||||
import org.apache.james.mime4j.stream.BodyDescriptor
|
||||
import org.apache.james.mime4j.stream.MimeConfig
|
||||
import org.apache.james.mime4j.stream.RawField
|
||||
@ -24,12 +26,14 @@ import org.mariotaku.twidere.model.draft.SendDirectMessageActionExtras
|
||||
import org.mariotaku.twidere.model.draft.UpdateStatusActionExtras
|
||||
import org.mariotaku.twidere.util.JsonSerializer
|
||||
import org.mariotaku.twidere.util.collection.NonEmptyHashMap
|
||||
import org.mariotaku.twidere.util.sync.mkdirIfNotExists
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
|
||||
val Draft.filename: String get() = "$unique_id_non_null.eml"
|
||||
@ -38,7 +42,9 @@ val Draft.unique_id_non_null: String
|
||||
get() = unique_id ?: UUID.nameUUIDFromBytes(("$_id:$timestamp").toByteArray()).toString()
|
||||
|
||||
fun Draft.writeMimeMessageTo(context: Context, st: OutputStream) {
|
||||
val bodyFactory = StorageBodyFactory()
|
||||
val cacheDir = File(context.cacheDir, "mime_cache").apply { mkdirIfNotExists() }
|
||||
val bodyFactory = StorageBodyFactory(TempFileStorageProvider("draft_media_", null,
|
||||
cacheDir), null)
|
||||
val storageProvider = bodyFactory.storageProvider
|
||||
val contentResolver = context.contentResolver
|
||||
|
||||
@ -74,23 +80,30 @@ fun Draft.writeMimeMessageTo(context: Context, st: OutputStream) {
|
||||
this.filename = "twidere.action.extras.json"
|
||||
})
|
||||
}
|
||||
this.media?.forEach { mediaItem ->
|
||||
multipart.addBodyPart(BodyPart().apply {
|
||||
val uri = Uri.parse(mediaItem.uri)
|
||||
val mimeType = mediaItem.getMimeType(contentResolver) ?: "application/octet-stream"
|
||||
val parameters = NonEmptyHashMap<String, String?>()
|
||||
parameters["alt_text"] = mediaItem.alt_text
|
||||
parameters["media_type"] = mediaItem.type.toString()
|
||||
val storage = contentResolver.openInputStream(uri).use { storageProvider.store(it) }
|
||||
this.filename = uri.lastPathSegment
|
||||
this.contentTransferEncoding = MimeUtil.ENC_BASE64
|
||||
this.setBody(bodyFactory.binaryBody(storage), mimeType, parameters)
|
||||
})
|
||||
}
|
||||
|
||||
message.setMultipart(multipart)
|
||||
writer.writeMessage(message, st)
|
||||
st.flush()
|
||||
val storageList = ArrayList<Storage>()
|
||||
try {
|
||||
this.media?.forEach { mediaItem ->
|
||||
multipart.addBodyPart(BodyPart().apply {
|
||||
val uri = Uri.parse(mediaItem.uri)
|
||||
val mimeType = mediaItem.getMimeType(contentResolver) ?: "application/octet-stream"
|
||||
val parameters = NonEmptyHashMap<String, String?>()
|
||||
parameters["alt_text"] = mediaItem.alt_text
|
||||
parameters["media_type"] = mediaItem.type.toString()
|
||||
val storage = contentResolver.openInputStream(uri).use { storageProvider.store(it) }
|
||||
this.filename = uri.lastPathSegment
|
||||
this.contentTransferEncoding = MimeUtil.ENC_BASE64
|
||||
this.setBody(bodyFactory.binaryBody(storage), mimeType, parameters)
|
||||
storageList.add(storage)
|
||||
})
|
||||
}
|
||||
|
||||
message.setMultipart(multipart)
|
||||
writer.writeMessage(message, st)
|
||||
st.flush()
|
||||
} finally {
|
||||
storageList.forEach(Storage::delete)
|
||||
}
|
||||
}
|
||||
|
||||
fun Draft.readMimeMessageFrom(context: Context, st: InputStream): Boolean {
|
||||
@ -139,6 +152,14 @@ fun Draft.applyUpdateStatus(statusUpdate: ParcelableStatusUpdate) {
|
||||
this.action_extras = statusUpdate.draft_extras
|
||||
}
|
||||
|
||||
fun draftActionTypeString(@Draft.Action action: String?): String {
|
||||
return when (action) {
|
||||
Draft.Action.QUOTE -> "quote"
|
||||
Draft.Action.REPLY -> "reply"
|
||||
else -> "tweet"
|
||||
}
|
||||
}
|
||||
|
||||
private class DraftContentHandler(private val context: Context, private val draft: Draft) : SimpleContentHandler() {
|
||||
private val processingStack = Stack<SimpleContentHandler>()
|
||||
private val mediaList: MutableList<ParcelableMediaUpdate> = ArrayList()
|
||||
@ -204,6 +225,7 @@ private class DraftContentHandler(private val context: Context, private val draf
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class BodyPartHandler(private val context: Context, private val draft: Draft) : SimpleContentHandler() {
|
||||
internal lateinit var header: Header
|
||||
internal var media: ParcelableMediaUpdate? = null
|
||||
@ -254,12 +276,3 @@ private class BodyPartHandler(private val context: Context, private val draft: D
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun draftActionTypeString(@Draft.Action action: String?): String {
|
||||
return when (action) {
|
||||
Draft.Action.QUOTE -> "quote"
|
||||
Draft.Action.REPLY -> "reply"
|
||||
else -> "tweet"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user