Skip to content

Commit

Permalink
feat: Handle protected file
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd authored and KevinBoulongne committed Oct 18, 2023
1 parent 7859f31 commit 779d7be
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class PreviewPDFFragment : PreviewFragment() {
showPdf(pdfCore)
} ?: run {
downloadProgress.isGone = true
previewDescription.setText(R.string.previewNoPreview)
previewDescription.setText(apiResponse.translatedError)
bigOpenWithButton.isVisible = true
}
previewSliderViewModel.pdfIsDownloading.value = false
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/com/infomaniak/drive/utils/PreviewPDFUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.infomaniak.drive.utils

import android.content.Context
import com.google.gson.JsonParser
import com.infomaniak.drive.R
import com.infomaniak.drive.data.api.ApiRoutes
import com.infomaniak.drive.data.models.File
Expand All @@ -39,7 +40,7 @@ object PreviewPDFUtils {
userDrive: UserDrive,
onProgress: (progress: Int) -> Unit
): ApiResponse<PdfCore> {
return try {
return runCatching {
val outputFile = when {
file.isOnlyOfficePreview() -> file.getConvertedPdfCache(context, userDrive)
file.isOffline -> file.getOfflineFile(context, userDrive.userId)!!
Expand All @@ -55,9 +56,17 @@ object PreviewPDFUtils {
}

ApiResponse(ApiResponse.Status.SUCCESS, PdfCore(context, outputFile))
} catch (e: Exception) {
e.printStackTrace()
ApiResponse(ApiResponse.Status.ERROR, null, translatedError = R.string.anErrorHasOccurred)
}.getOrElse { exception ->
exception.printStackTrace()
val error = when (exception) {
is PasswordProtectedException -> R.string.previewFileProtectedError
else -> R.string.previewNoPreview
}
ApiResponse(
result = ApiResponse.Status.ERROR,
data = null,
translatedError = error
)
}
}

Expand All @@ -75,7 +84,10 @@ object PreviewPDFUtils {
.newCall(request).execute()

response.use {
if (!it.isSuccessful) throw Exception("Download error ")
if (!it.isSuccessful) {
val errorCode = JsonParser.parseString(it.body?.string()).asJsonObject.getAsJsonPrimitive("error").asString
if (errorCode == "password_protected_error") throw PasswordProtectedException() else throw Exception("Download error")
}
when (it.body?.contentType()?.toString()) {
"application/pdf" -> createTempPdfFile(it, externalOutputFile)
else -> throw UnsupportedOperationException("File not supported")
Expand All @@ -90,4 +102,6 @@ object PreviewPDFUtils {
}
}
}

private class PasswordProtectedException : Exception()
}
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
<item quantity="other">Vorbereitung auf den Import von %d Dateien</item>
</plurals>
<string name="previewDownloadIndication">Wird geladen, bitte warten…</string>
<string name="previewFileProtectedError">Keine Vorschau verfügbar, diese Datei ist passwortgeschützt.</string>
<string name="previewLoadError">Vorschau kann nicht geladen werden</string>
<string name="previewNoPreview">Es scheint, als ob diese Datei keine Vorschau hat.</string>
<string name="previewPdfPages">%1$d von %2$d</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
<item quantity="other">Preparando la importación de %d archivos</item>
</plurals>
<string name="previewDownloadIndication">Cambio en curso, espera…</string>
<string name="previewFileProtectedError">No hay vista previa disponible, este archivo está protegido por contraseña.</string>
<string name="previewLoadError">No se puede cargar la vista previa</string>
<string name="previewNoPreview">Parece que este archivo no tiene vista previa.</string>
<string name="previewPdfPages">%1$d de %2$d</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
<item quantity="other">Préparation de l’importation de %d fichiers</item>
</plurals>
<string name="previewDownloadIndication">Chargement en cours, merci de patienter…</string>
<string name="previewFileProtectedError">Pas d\'aperçu disponible, ce fichier est protégé par un mot de passe.</string>
<string name="previewLoadError">Impossible de charger l’aperçu</string>
<string name="previewNoPreview">Il semblerait que ce fichier n’ait pas d’aperçu.</string>
<string name="previewPdfPages">%1$d sur %2$d</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
<item quantity="other">Preparazione all’importazione di %d file</item>
</plurals>
<string name="previewDownloadIndication">Modifica in corso, attendere prego…</string>
<string name="previewFileProtectedError">Non è disponibile un\'anteprima, questo file è protetto da password.</string>
<string name="previewLoadError">Impossibile caricare l’anteprima</string>
<string name="previewNoPreview">Sembra che il file non abbia anteprima.</string>
<string name="previewPdfPages">%1$d di %2$d</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@
<item quantity="other">Preparing to upload %d files</item>
</plurals>
<string name="previewDownloadIndication">Loading in progress, please wait…</string>
<string name="previewFileProtectedError">No preview available, this file is password protected.</string>
<string name="previewLoadError">Unable to load preview</string>
<string name="previewNoPreview">This file appears to have no preview.</string>
<string name="previewPdfPages">%1$d out of %2$d</string>
Expand Down

0 comments on commit 779d7be

Please sign in to comment.