Do not download attachments if they are expired

This commit is contained in:
Philipp Heckel 2022-02-10 12:10:27 -05:00
parent d44358f75c
commit 9ca5ebe6d2
5 changed files with 23 additions and 7 deletions

View file

@ -18,13 +18,13 @@ class DownloadManager {
private const val TAG = "NtfyDownloadManager"
private const val DOWNLOAD_WORK_NAME_PREFIX = "io.heckel.ntfy.DOWNLOAD_FILE_"
fun enqueue(context: Context, id: String, userAction: Boolean) {
fun enqueue(context: Context, notificationId: String, userAction: Boolean) {
val workManager = WorkManager.getInstance(context)
val workName = DOWNLOAD_WORK_NAME_PREFIX + id
Log.d(TAG,"Enqueuing work to download attachment for notification $id, work: $workName")
val workName = DOWNLOAD_WORK_NAME_PREFIX + notificationId
Log.d(TAG,"Enqueuing work to download attachment for notification $notificationId, work: $workName")
val workRequest = OneTimeWorkRequest.Builder(DownloadWorker::class.java)
.setInputData(workDataOf(
DownloadWorker.INPUT_DATA_ID to id,
DownloadWorker.INPUT_DATA_ID to notificationId,
DownloadWorker.INPUT_DATA_USER_ACTION to userAction
))
.build()

View file

@ -49,15 +49,20 @@ class NotificationDispatcher(val context: Context, val repository: Repository) {
if (notification.attachment == null) {
return false
}
val attachment = notification.attachment
if (attachment.expires != null && attachment.expires < System.currentTimeMillis()/1000) {
Log.d(TAG, "Attachment already expired at ${attachment.expires}, not downloading")
return false
}
val maxAutoDownloadSize = repository.getAutoDownloadMaxSize()
when (maxAutoDownloadSize) {
Repository.AUTO_DOWNLOAD_ALWAYS -> return true
Repository.AUTO_DOWNLOAD_NEVER -> return false
else -> {
if (notification.attachment.size == null) {
if (attachment.size == null) {
return true // DownloadWorker will bail out if attachment is too large!
}
return notification.attachment.size <= maxAutoDownloadSize
return attachment.size <= maxAutoDownloadSize
}
}
}

View file

@ -305,7 +305,13 @@ class DetailAdapter(private val activity: Activity, private val repository: Repo
infos.add(context.getString(R.string.detail_item_download_info_deleted))
}
} else if (failed) {
infos.add(context.getString(R.string.detail_item_download_info_download_failed))
if (expired) {
infos.add(context.getString(R.string.detail_item_download_info_download_failed_expired))
} else if (expires) {
infos.add(context.getString(R.string.detail_item_download_info_download_failed_expires_x, formatDateShort(attachment.expires!!)))
} else {
infos.add(context.getString(R.string.detail_item_download_info_download_failed))
}
}
return if (infos.size > 0) {
"$name\n${infos.joinToString(", ")}"

View file

@ -156,6 +156,8 @@
<string name="detail_item_download_info_deleted_expired">deleted, link expired</string>
<string name="detail_item_download_info_deleted_expires_x">deleted, link expires %1$s</string>
<string name="detail_item_download_info_download_failed">download failed</string>
<string name="detail_item_download_info_download_failed_expired">download failed, link expired</string>
<string name="detail_item_download_info_download_failed_expires_x">download failed, link expires %1$s</string>
<!-- Detail activity: Action bar -->
<string name="detail_menu_notifications_enabled">Notifications enabled</string>

View file

@ -1,3 +1,6 @@
Features:
* Dark theme: Improvements around style and contrast (#119, thanks @kzshantonu for reporting)
* Automatically delete notifications (#71, thanks @arjan-s for reporting)
Bug fixes:
* Do not attempt to download attachments if they are already expired (#135)