fix/handle-shared-files-and-error-handling

This commit is contained in:
Sujal-Gupta-SG 2025-01-19 03:38:53 +05:30
parent f84f524207
commit aa6d1091e1
4 changed files with 36 additions and 20 deletions

View file

@ -261,6 +261,7 @@ class UploadClient
fileKey: String?,
): Observable<UploadResult?> =
try {
Timber.d( "Step 1")
uploadInterface
.uploadFileFromStash(
csrfTokenClient.getTokenBlocking(),
@ -269,12 +270,25 @@ class UploadClient
uniqueFileName!!,
fileKey!!,
).map { uploadResponse: JsonObject? ->
Timber.d("Step 2")
Timber.d(uploadResponse.toString())
val uploadResult = gson.fromJson(uploadResponse, UploadResponse::class.java)
Timber.d("Step 3: Upload result - ${uploadResult.upload}")
if (uploadResult.upload == null) {
// Parse the error to MwException
val exception = gson.fromJson(uploadResponse, MwException::class.java)
Timber.e(exception, "Error in uploading file from stash")
throw Exception(exception.errorCode)
// Handle abuse filter warning explicitly
if (exception.errorCode?.contains("abusefilter-warning") == true) {
Timber.w("Abuse filter triggered: ${exception.errorCode}")
throw Exception("Abuse filter warning: ${exception.errorCode}. Please verify file content or metadata.")
} else {
throw Exception(exception.errorCode)
}
}
Timber.d("Step 4")
uploadResult.upload
}
} catch (throwable: Throwable) {

View file

@ -28,10 +28,10 @@ interface UploadInterface {
@POST(WikidataConstants.MW_API_PREFIX + "action=upload&ignorewarnings=1")
@FormUrlEncoded
fun uploadFileFromStash(
@Field("token") token: String,
@Field("text") text: String,
@Field("comment") comment: String,
@Field("filename") filename: String,
@Field("filekey") filekey: String,
@Field("token") token: String?,
@Field("text") text: String?,
@Field("comment") comment: String?,
@Field("filename") filename: String?,
@Field("filekey") filekey: String?,
): Observable<JsonObject>
}

View file

@ -78,21 +78,22 @@ class WikiBaseClient
.toLong()
}
fun addLabelsToWikidata(
fileEntityId: Long,
languageCode: String?,
captionValue: String?,
): Observable<MwPostResponse> =
csrfToken().switchMap { editToken ->
wikiBaseInterface.addLabelstoWikidata(
PAGE_ID_PREFIX + fileEntityId,
editToken,
languageCode,
captionValue,
)
}
fun addLabelsToWikidata(
fileEntityId: Long,
languageCode: String?,
captionValue: String?
): Observable<MwPostResponse> {
return csrfToken().switchMap { editToken ->
wikiBaseInterface.addLabelstoWikidata(
PAGE_ID_PREFIX + fileEntityId,
editToken,
languageCode,
captionValue
)
}
}
private fun csrfToken(): Observable<String> =
private fun csrfToken(): Observable<String> =
Observable.fromCallable {
csrfTokenClient.getTokenBlocking()
}

View file

@ -235,6 +235,7 @@ class WikidataEditService @Inject constructor(
}
}
@SuppressLint("NewApi")
private fun captionEdits(contribution: Contribution, fileEntityId: Long): Observable<Boolean> {
return Observable.fromIterable(contribution.media.captions.entries)
.concatMap { addCaption(fileEntityId, it.key, it.value) }