Guard getMedia finalize path from ids=M0 and make uploaded status save resilient on retry

This commit is contained in:
Sonal Yadav 2025-08-22 07:47:47 +05:30
parent 5eeddbd9a3
commit 7f0aabf368
2 changed files with 26 additions and 3 deletions

View file

@ -206,14 +206,26 @@ class MediaClient
private fun mediaFromPageAndEntity(pages: List<MwQueryPage>): Single<List<Media>> =
if (pages.isEmpty()) {
Single.just(emptyList())
} else {
// If any pageId is invalid (0 or negative), avoid fetching entities like M0
// and convert using a blank entity to prevent wbgetentities no-such-entity errors.
if (pages.any { it.pageId() <= 0 }) {
Single.just(
pages.mapNotNull { page ->
page.imageInfo()?.let { imageInfo ->
mediaConverter.convert(page, Entities.Entity(), imageInfo)
}
},
)
} else {
getEntities(pages.map { "$PAGE_ID_PREFIX${it.pageId()}" })
.map {
pages
.zip(it.entities().values)
.mapNotNull { (page, entity) ->
page.imageInfo()?.let {
mediaConverter.convert(page, entity, it)
page.imageInfo()?.let { imageInfo ->
mediaConverter.convert(page, entity, imageInfo)
}
}
}
}

View file

@ -618,8 +618,16 @@ class UploadWorker(
private fun saveIntoUploadedStatus(contribution: Contribution) {
contribution.contentUri?.let {
val imageSha1 = contribution.imageSHA1.toString()
val modifiedSha1 = fileUtilsWrapper.getSHA1(fileUtilsWrapper.getFileInputStream(contribution.localUri?.path))
val modifiedSha1 = try {
fileUtilsWrapper.getSHA1(
fileUtilsWrapper.getFileInputStream(contribution.localUri?.path),
)
} catch (e: Exception) {
Timber.w(e, "UploadedStatus: modified file missing/unreadable; falling back to original SHA1")
imageSha1
}
CoroutineScope(Dispatchers.IO).launch {
try {
uploadedStatusDao.insertUploaded(
UploadedStatus(
imageSha1,
@ -628,6 +636,9 @@ class UploadWorker(
true,
),
)
} catch (e: Exception) {
Timber.w(e, "UploadedStatus: insert failed; continuing")
}
}
}
}