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 TAG = "NtfyDownloadManager"
private const val DOWNLOAD_WORK_NAME_PREFIX = "io.heckel.ntfy.DOWNLOAD_FILE_" 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 workManager = WorkManager.getInstance(context)
val workName = DOWNLOAD_WORK_NAME_PREFIX + id val workName = DOWNLOAD_WORK_NAME_PREFIX + notificationId
Log.d(TAG,"Enqueuing work to download attachment for notification $id, work: $workName") Log.d(TAG,"Enqueuing work to download attachment for notification $notificationId, work: $workName")
val workRequest = OneTimeWorkRequest.Builder(DownloadWorker::class.java) val workRequest = OneTimeWorkRequest.Builder(DownloadWorker::class.java)
.setInputData(workDataOf( .setInputData(workDataOf(
DownloadWorker.INPUT_DATA_ID to id, DownloadWorker.INPUT_DATA_ID to notificationId,
DownloadWorker.INPUT_DATA_USER_ACTION to userAction DownloadWorker.INPUT_DATA_USER_ACTION to userAction
)) ))
.build() .build()

View file

@ -49,15 +49,20 @@ class NotificationDispatcher(val context: Context, val repository: Repository) {
if (notification.attachment == null) { if (notification.attachment == null) {
return false 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() val maxAutoDownloadSize = repository.getAutoDownloadMaxSize()
when (maxAutoDownloadSize) { when (maxAutoDownloadSize) {
Repository.AUTO_DOWNLOAD_ALWAYS -> return true Repository.AUTO_DOWNLOAD_ALWAYS -> return true
Repository.AUTO_DOWNLOAD_NEVER -> return false Repository.AUTO_DOWNLOAD_NEVER -> return false
else -> { else -> {
if (notification.attachment.size == null) { if (attachment.size == null) {
return true // DownloadWorker will bail out if attachment is too large! return true // DownloadWorker will bail out if attachment is too large!
} }
return notification.attachment.size <= maxAutoDownloadSize return attachment.size <= maxAutoDownloadSize
} }
} }
} }

View file

@ -305,8 +305,14 @@ class DetailAdapter(private val activity: Activity, private val repository: Repo
infos.add(context.getString(R.string.detail_item_download_info_deleted)) infos.add(context.getString(R.string.detail_item_download_info_deleted))
} }
} else if (failed) { } else if (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)) infos.add(context.getString(R.string.detail_item_download_info_download_failed))
} }
}
return if (infos.size > 0) { return if (infos.size > 0) {
"$name\n${infos.joinToString(", ")}" "$name\n${infos.joinToString(", ")}"
} else { } else {

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_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_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">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 --> <!-- Detail activity: Action bar -->
<string name="detail_menu_notifications_enabled">Notifications enabled</string> <string name="detail_menu_notifications_enabled">Notifications enabled</string>

View file

@ -1,3 +1,6 @@
Features: Features:
* Dark theme: Improvements around style and contrast (#119, thanks @kzshantonu for reporting) * Dark theme: Improvements around style and contrast (#119, thanks @kzshantonu for reporting)
* Automatically delete notifications (#71, thanks @arjan-s 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)