Skip to content

Commit d918e11

Browse files
committed
Fix various issues with optimized media.
1 parent 6680e74 commit d918e11

File tree

13 files changed

+123
-29
lines changed

13 files changed

+123
-29
lines changed

app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ object BackupRepository {
589589

590590
@JvmStatic
591591
fun maybeFixAnyDanglingUploadProgress() {
592+
if (SignalStore.account.isLinkedDevice) {
593+
return
594+
}
595+
592596
if (SignalStore.backup.archiveUploadState?.backupPhase == ArchiveUploadProgressState.BackupPhase.Message && AppDependencies.jobManager.find { it.factoryKey == BackupMessagesJob.KEY }.isEmpty()) {
593597
SignalStore.backup.archiveUploadState = null
594598
BackupMessagesJob.enqueue()

app/src/main/java/org/thoughtcrime/securesms/components/ThumbnailView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public ListenableFuture<Boolean> setImageResource(@NonNull RequestManager reques
403403
}
404404

405405
if (hasSameContents(this.slide, slide)) {
406-
Log.i(TAG, "Not re-loading slide " + slide.asAttachment().getUri());
406+
Log.i(TAG, "Not re-loading slide " + slide.asAttachment().getDisplayUri());
407407
return new SettableFuture<>(false);
408408
}
409409

app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ class AttachmentTable(
869869
"""
870870
${buildAttachmentsThatNeedUploadQuery("$ARCHIVE_THUMBNAIL_TRANSFER_STATE IN (${ArchiveTransferState.NONE.value}, ${ArchiveTransferState.TEMPORARY_FAILURE.value})")} AND
871871
$QUOTE = 0 AND
872+
$STICKER_ID = -1 AND
872873
($CONTENT_TYPE LIKE 'image/%' OR $CONTENT_TYPE LIKE 'video/%') AND
873874
$CONTENT_TYPE != 'image/svg+xml' AND
874875
$MESSAGE_ID != $WALLPAPER_MESSAGE_ID
@@ -888,6 +889,7 @@ class AttachmentTable(
888889
"""
889890
${buildAttachmentsThatNeedUploadQuery("$ARCHIVE_THUMBNAIL_TRANSFER_STATE IN (${ArchiveTransferState.NONE.value}, ${ArchiveTransferState.TEMPORARY_FAILURE.value})")} AND
890891
$QUOTE = 0 AND
892+
$STICKER_ID = -1 AND
891893
($CONTENT_TYPE LIKE 'image/%' OR $CONTENT_TYPE LIKE 'video/%') AND
892894
$CONTENT_TYPE != 'image/svg+xml' AND
893895
$MESSAGE_ID != $WALLPAPER_MESSAGE_ID
@@ -1122,31 +1124,36 @@ class AttachmentTable(
11221124
$TABLE_NAME.$OFFLOAD_RESTORED_AT < ${now - 7.days.inWholeMilliseconds} AND
11231125
$TABLE_NAME.$TRANSFER_STATE = $TRANSFER_PROGRESS_DONE AND
11241126
$TABLE_NAME.$ARCHIVE_TRANSFER_STATE = ${ArchiveTransferState.FINISHED.value} AND
1127+
$TABLE_NAME.$DATA_FILE IS NOT NULL AND
1128+
$TABLE_NAME.$STICKER_ID = -1 AND
1129+
$TABLE_NAME.$REMOTE_KEY IS NOT NULL AND
1130+
$TABLE_NAME.$DATA_HASH_END IS NOT NULL AND
11251131
(
11261132
$TABLE_NAME.$THUMBNAIL_FILE IS NOT NULL OR
1127-
NOT ($TABLE_NAME.$CONTENT_TYPE like 'image/%' OR $TABLE_NAME.$CONTENT_TYPE like 'video/%')
1128-
) AND
1129-
$TABLE_NAME.$DATA_FILE IS NOT NULL
1133+
NOT ($TABLE_NAME.$CONTENT_TYPE LIKE 'image/%' OR $TABLE_NAME.$CONTENT_TYPE LIKE 'video/%') OR
1134+
$TABLE_NAME.$CONTENT_TYPE = 'image/svg+xml'
1135+
)
11301136
)
11311137
AND
11321138
(
11331139
${MessageTable.TABLE_NAME}.${MessageTable.DATE_RECEIVED} < ${now - 30.days.inWholeMilliseconds}
11341140
)
11351141
"""
11361142

1137-
writableDatabase
1143+
val count = writableDatabase
11381144
.update(TABLE_NAME)
11391145
.values(
11401146
TRANSFER_STATE to TRANSFER_RESTORE_OFFLOADED,
11411147
DATA_FILE to null,
11421148
DATA_RANDOM to null,
11431149
TRANSFORM_PROPERTIES to null,
11441150
DATA_HASH_START to null,
1145-
DATA_HASH_END to null,
11461151
OFFLOAD_RESTORED_AT to 0
11471152
)
11481153
.where("$ID in ($subSelect)")
11491154
.run()
1155+
1156+
Log.i(TAG, "Marked $count attachments as optimized")
11501157
}
11511158

11521159
/**

app/src/main/java/org/thoughtcrime/securesms/jobs/OptimizeMediaJob.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class OptimizeMediaJob private constructor(parameters: Parameters) : Job(paramet
5454
SignalDatabase.attachments.markEligibleAttachmentsAsOptimized()
5555

5656
Log.i(TAG, "Deleting abandoned attachment files")
57-
SignalDatabase.attachments.deleteAbandonedAttachmentFiles()
57+
val count = SignalDatabase.attachments.deleteAbandonedAttachmentFiles()
58+
Log.i(TAG, "Deleted $count attachments")
5859

5960
return Result.success()
6061
}

app/src/main/java/org/thoughtcrime/securesms/jobs/RestoreAttachmentJob.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class RestoreAttachmentJob private constructor(
164164
messageId = attachment.mmsId,
165165
attachmentId = attachment.attachmentId,
166166
manual = true,
167-
queue = Queues.MANUAL_RESTORE.random(),
167+
queue = Queues.random(Queues.MANUAL_RESTORE, attachment.dataHash?.hashCode() ?: attachment.remoteKey?.hashCode()),
168168
priority = Parameters.PRIORITY_DEFAULT
169169
)
170170

@@ -410,7 +410,7 @@ class RestoreAttachmentJob private constructor(
410410
inputStream = input,
411411
offloadRestoredAt = if (manual) System.currentTimeMillis().milliseconds else null,
412412
archiveRestore = true,
413-
notify = false
413+
notify = manual
414414
)
415415
ArchiveDatabaseExecutor.throttledNotifyAttachmentAndChatListObservers()
416416
}

app/src/main/java/org/thoughtcrime/securesms/mms/Slide.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,13 @@ public boolean equals(Object other) {
251251
this.hasImage() == that.hasImage() &&
252252
this.hasVideo() == that.hasVideo() &&
253253
this.getTransferState() == that.getTransferState() &&
254-
Util.equals(this.getUri(), that.getUri());
254+
Util.equals(this.getUri(), that.getUri()) &&
255+
Util.equals(this.getThumbnailUri(), that.getThumbnailUri());
255256
}
256257

257258
@Override
258259
public int hashCode() {
259260
return Util.hashCode(getContentType(), hasAudio(), hasImage(),
260-
hasVideo(), getUri(), getTransferState());
261+
hasVideo(), getUri(), getTransferState(), getThumbnailUri());
261262
}
262263
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 Signal Messenger, LLC
3+
* SPDX-License-Identifier: AGPL-3.0-only
4+
*/
5+
6+
package org.thoughtcrime.securesms
7+
8+
import okio.IOException
9+
import org.signal.spinner.Plugin
10+
import org.signal.spinner.PluginResult
11+
import org.thoughtcrime.securesms.attachments.AttachmentId
12+
import org.thoughtcrime.securesms.database.AttachmentTable
13+
import org.thoughtcrime.securesms.database.SignalDatabase
14+
15+
class AttachmentPlugin : Plugin {
16+
companion object {
17+
const val PATH = "/attachment"
18+
}
19+
20+
override val name: String = "Attachment"
21+
override val path: String = PATH
22+
23+
override fun get(parameters: Map<String, List<String>>): PluginResult {
24+
var errorContent = ""
25+
26+
parameters["attachment_id"]?.firstOrNull()?.let { id ->
27+
val attachmentId = id.toLongOrNull()?.let { AttachmentId(it) }
28+
if (attachmentId != null) {
29+
try {
30+
val attachment = SignalDatabase.attachments.getAttachment(attachmentId)
31+
if (attachment != null) {
32+
val inputStream = if (attachment.transferState == AttachmentTable.TRANSFER_PROGRESS_DONE) {
33+
SignalDatabase.attachments.getAttachmentStream(attachmentId, 0)
34+
} else {
35+
SignalDatabase.attachments.getAttachmentThumbnailStream(attachmentId, 0)
36+
}
37+
return PluginResult.RawFileResult(attachment.size, inputStream, attachment.contentType ?: "application/octet-stream")
38+
} else {
39+
throw IOException("Missing attachment, not found for: $attachmentId")
40+
}
41+
} catch (e: IOException) {
42+
errorContent = "${e.javaClass}: ${e.message}"
43+
}
44+
}
45+
}
46+
47+
val formContent = """
48+
<form action="$PATH" method="GET">
49+
<label for="number">Enter an attachment_id:</label>
50+
<input type="number" id="attachment_id" name="attachment_id" required>
51+
<button type="submit">Submit</button>
52+
</form>
53+
""".trimIndent()
54+
55+
return PluginResult.RawHtmlResult("$formContent<br>$errorContent")
56+
}
57+
}

app/src/spinner/java/org/thoughtcrime/securesms/SpinnerApplicationContext.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class SpinnerApplicationContext : ApplicationContext() {
8585
)
8686
),
8787
linkedMapOf(
88-
StorageServicePlugin.PATH to StorageServicePlugin()
88+
StorageServicePlugin.PATH to StorageServicePlugin(),
89+
AttachmentPlugin.PATH to AttachmentPlugin()
8990
)
9091
)
9192

app/src/spinner/java/org/thoughtcrime/securesms/StorageServicePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class StorageServicePlugin : Plugin {
1111
override val name: String = "Storage"
1212
override val path: String = PATH
1313

14-
override fun get(): PluginResult {
14+
override fun get(parameters: Map<String, List<String>>): PluginResult {
1515
val columns = listOf("Type", "Id", "Data")
1616
val rows = mutableListOf<List<String>>()
1717

spinner/lib/src/main/assets/plugin.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
{{#if (eq "string" pluginResult.type)}}
2727
<p>{{pluginResult.text}}</p>
2828
{{/if}}
29+
{{#if (eq "html" pluginResult.type)}}
30+
{{{pluginResult.html}}}
31+
{{/if}}
2932
{{> partials/suffix }}
3033
</body>
3134
</html>

0 commit comments

Comments
 (0)