Skip to content

Commit

Permalink
Fixed a bug of loading attachments.| #793
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jan 14, 2020
1 parent ba36505 commit 2648508
Showing 1 changed file with 15 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import com.flowcrypt.email.api.retrofit.node.NodeRetrofitHelper
import com.flowcrypt.email.api.retrofit.node.NodeService
import com.flowcrypt.email.api.retrofit.request.node.DecryptFileRequest
import com.flowcrypt.email.api.retrofit.response.node.DecryptedFileResult
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.dao.source.AccountDaoSource
import com.flowcrypt.email.database.dao.source.imap.ImapLabelsDaoSource
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.LogsUtil
Expand Down Expand Up @@ -401,6 +401,7 @@ class AttachmentDownloadManagerService : Service() {
if (GeneralUtil.isDebugBuild()) {
Thread.currentThread().name = AttDownloadRunnable::class.java.simpleName + "|" + att.name
}
val roomDatabase = FlowCryptRoomDatabase.getDatabase(context)

var attFile: File = prepareAttFile()

Expand All @@ -416,10 +417,8 @@ class AttachmentDownloadManagerService : Service() {
att.name = attFile.name

if (!Thread.currentThread().isInterrupted) {
if (listener != null) {
val uri = FileProvider.getUriForFile(context, Constants.FILE_PROVIDER_AUTHORITY, attFile)
listener!!.onAttDownloaded(att, uri)
}
val uri = FileProvider.getUriForFile(context, Constants.FILE_PROVIDER_AUTHORITY, attFile)
listener?.onAttDownloaded(att, uri)
}

return
Expand All @@ -430,28 +429,21 @@ class AttachmentDownloadManagerService : Service() {
val account = source.getAccountInformation(context, att.email!!)

if (account == null) {
if (listener != null) {
listener!!.onCanceled(this.att)
return
}
listener?.onCanceled(this.att)
return
}

val session = OpenStoreHelper.getAttsSess(context, account)
val store = OpenStoreHelper.openStore(context, account, session)

val (fullName) = ImapLabelsDaoSource().getFolder(context, att.email!!, att.folder!!)
val label = roomDatabase.labelDao().getLabel(att.email, att.folder!!)
?: if (source.getAccountInformation(context, att.email!!) == null) {
if (listener != null) {
listener!!.onCanceled(this.att)

store.close()
}
listener?.onCanceled(this.att)
store.close()
return
} else {
throw ManualHandledException("Folder \"" + att.folder + "\" not found in the local cache")
}
} else throw ManualHandledException("Folder \"" + att.folder + "\" not found in the local cache")

val remoteFolder = store.getFolder(fullName) as IMAPFolder
val remoteFolder = store.getFolder(label.folderName) as IMAPFolder
remoteFolder.open(Folder.READ_ONLY)

val msg = remoteFolder.getMessageByUID(att.uid.toLong())
Expand Down Expand Up @@ -486,9 +478,7 @@ class AttachmentDownloadManagerService : Service() {
e.printStackTrace()
ExceptionUtil.handleError(e)
removeNotCompletedAtt(attFile)
if (listener != null) {
listener!!.onError(att, e)
}
listener?.onError(att, e)
}

}
Expand All @@ -506,9 +496,8 @@ class AttachmentDownloadManagerService : Service() {
var numberOfReadBytes: Int
var lastPercentage = 0
var currentPercentage = 0
val startTime: Long
var elapsedTime: Long
startTime = System.currentTimeMillis()
val startTime: Long = System.currentTimeMillis()
var lastUpdateTime = startTime
updateProgress(currentPercentage, 0)
while (true) {
Expand Down Expand Up @@ -574,15 +563,15 @@ class AttachmentDownloadManagerService : Service() {
}

FileInputStream(file).use { inputStream ->
val (_, _, _, decryptedBytes) = getDecryptedFileResult(context, inputStream)
val decryptedFileResult = getDecryptedFileResult(context, inputStream)

val decryptedFile = File(file.parent, file.name.substring(0, file.name.lastIndexOf(".")))

var isInnerExceptionHappened = false

try {
FileUtils.openOutputStream(decryptedFile).use { outputStream ->
IOUtils.write(decryptedBytes, outputStream)
IOUtils.write(decryptedFileResult.decryptedBytes, outputStream)
return decryptedFile
}
} catch (e: IOException) {
Expand Down Expand Up @@ -666,7 +655,6 @@ class AttachmentDownloadManagerService : Service() {

private val TAG = AttachmentDownloadManagerService::class.java.simpleName

@JvmStatic
fun newIntent(context: Context?, attInfo: AttachmentInfo?): Intent? {
if (context == null || attInfo == null) {
return null
Expand Down

0 comments on commit 2648508

Please sign in to comment.