Delete downloaded file on failure
This commit is contained in:
parent
25426b4344
commit
b176bfe35e
1 changed files with 12 additions and 2 deletions
|
@ -2,6 +2,7 @@ package io.heckel.ntfy.msg
|
||||||
|
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
|
@ -17,7 +18,6 @@ import io.heckel.ntfy.R
|
||||||
import io.heckel.ntfy.app.Application
|
import io.heckel.ntfy.app.Application
|
||||||
import io.heckel.ntfy.data.*
|
import io.heckel.ntfy.data.*
|
||||||
import io.heckel.ntfy.util.queryFilename
|
import io.heckel.ntfy.util.queryFilename
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -31,6 +31,7 @@ class DownloadWorker(private val context: Context, params: WorkerParameters) : W
|
||||||
.writeTimeout(15, TimeUnit.SECONDS)
|
.writeTimeout(15, TimeUnit.SECONDS)
|
||||||
.build()
|
.build()
|
||||||
private val notifier = NotificationService(context)
|
private val notifier = NotificationService(context)
|
||||||
|
private var uri: Uri? = null
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
if (context.applicationContext !is Application) return Result.failure()
|
if (context.applicationContext !is Application) return Result.failure()
|
||||||
|
@ -43,6 +44,14 @@ class DownloadWorker(private val context: Context, params: WorkerParameters) : W
|
||||||
return Result.success()
|
return Result.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onStopped() {
|
||||||
|
val uriCopy = uri
|
||||||
|
if (uriCopy != null) {
|
||||||
|
val resolver = applicationContext.contentResolver
|
||||||
|
resolver.delete(uriCopy, null, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun downloadAttachment(repository: Repository, subscription: Subscription, notification: Notification) {
|
private fun downloadAttachment(repository: Repository, subscription: Subscription, notification: Notification) {
|
||||||
val attachment = notification.attachment ?: return
|
val attachment = notification.attachment ?: return
|
||||||
Log.d(TAG, "Downloading attachment from ${attachment.url}")
|
Log.d(TAG, "Downloading attachment from ${attachment.url}")
|
||||||
|
@ -74,6 +83,7 @@ class DownloadWorker(private val context: Context, params: WorkerParameters) : W
|
||||||
resolver.insert(MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL), details)
|
resolver.insert(MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL), details)
|
||||||
?: throw Exception("Cannot get content URI")
|
?: throw Exception("Cannot get content URI")
|
||||||
}
|
}
|
||||||
|
this.uri = uri // Required for cleanup in onStopped()
|
||||||
Log.d(TAG, "Starting download to content URI: $uri")
|
Log.d(TAG, "Starting download to content URI: $uri")
|
||||||
var bytesCopied: Long = 0
|
var bytesCopied: Long = 0
|
||||||
val out = resolver.openOutputStream(uri) ?: throw Exception("Cannot open output stream")
|
val out = resolver.openOutputStream(uri) ?: throw Exception("Cannot open output stream")
|
||||||
|
@ -90,7 +100,7 @@ class DownloadWorker(private val context: Context, params: WorkerParameters) : W
|
||||||
val newNotification = notification.copy(attachment = newAttachment)
|
val newNotification = notification.copy(attachment = newAttachment)
|
||||||
notifier.update(subscription, newNotification)
|
notifier.update(subscription, newNotification)
|
||||||
repository.updateNotification(newNotification)
|
repository.updateNotification(newNotification)
|
||||||
return
|
return // File will be deleted in onStopped()
|
||||||
}
|
}
|
||||||
val progress = if (size > 0) (bytesCopied.toFloat()/size.toFloat()*100).toInt() else PROGRESS_INDETERMINATE
|
val progress = if (size > 0) (bytesCopied.toFloat()/size.toFloat()*100).toInt() else PROGRESS_INDETERMINATE
|
||||||
val newAttachment = attachment.copy(progress = progress)
|
val newAttachment = attachment.copy(progress = progress)
|
||||||
|
|
Loading…
Reference in a new issue