Skip to content

Commit

Permalink
Robustness: If possible, do not chock on 0-lenght resources
Browse files Browse the repository at this point in the history
remove unused return value of saveFileContents
  • Loading branch information
mtotschnig committed Dec 18, 2024
1 parent 869d8bf commit a2bf79e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class GoogleDriveBackendProvider internal constructor(
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): File {
) {
val base = if (toAccountDir) accountFolder else baseFolder
val driveFolder = if (folder == null) base else {
getResInAccountDir(folder) ?: driveServiceHelper.createFolder(
Expand All @@ -125,7 +125,6 @@ class GoogleDriveBackendProvider internal constructor(
)
}
saveFileContents(driveFolder, fileName, fileContents, mimeType, maybeEncrypt)
return driveFolder
}

@Throws(IOException::class)
Expand Down Expand Up @@ -239,7 +238,10 @@ class GoogleDriveBackendProvider internal constructor(
}

override fun childrenForCollection(folder: File?) =
driveServiceHelper.listChildren(folder ?: accountFolder)
driveServiceHelper.listChildren(folder ?: accountFolder).filter {
@Suppress("UsePropertyAccessSyntax")
it.getSize() > 0
}

override fun nameForResource(resource: File): String? = resource.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class DropboxBackendProvider internal constructor(context: Context, folderName:
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): Metadata {
) {
val base = if (toAccountDir) accountPath else basePath
val path = if (folder == null) {
base
Expand All @@ -251,7 +251,7 @@ class DropboxBackendProvider internal constructor(context: Context, folderName:
requireFolder(it)
}
}
return saveInputStream("$path/$fileName", toInputStream(fileContents, maybeEncrypt))
saveInputStream("$path/$fileName", toInputStream(fileContents, maybeEncrypt))
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ abstract class AbstractSyncBackendProvider<Res>(protected val context: Context)
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): Res
)

protected abstract fun readFileContents(
fromAccountDir: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class StorageAccessFrameworkBackendProvider internal constructor(context: Contex
baseDir.getFolder(collectionName, require)

override fun childrenForCollection(folder: DocumentFile?) =
(folder ?: accountDir).listFiles().asList()
(folder ?: accountDir).listFiles().filter { it.length() > 0 }

override fun nameForResource(resource: DocumentFile) = resource.name

Expand Down Expand Up @@ -139,10 +139,10 @@ class StorageAccessFrameworkBackendProvider internal constructor(context: Contex
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): DocumentFile {
) {
val base = if (toAccountDir) accountDir else baseDir
val dir = if (folder == null) base else base.getFolder(folder)!!
return saveFileContents(dir, fileName, fileContents, mimeType, maybeEncrypt)
saveFileContents(dir, fileName, fileContents, mimeType, maybeEncrypt)
}

private fun saveFileContents(
Expand All @@ -151,11 +151,10 @@ class StorageAccessFrameworkBackendProvider internal constructor(context: Contex
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): DocumentFile {
) {
val file = (folder.findFile(fileName) ?: folder.createFile(mimeType, fileName)
?: throw IOException())
saveFileContents(file, fileContents, maybeEncrypt)
return file
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ class OneDriveBackendProvider internal constructor(context: Context, folderName:
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): DriveItem {
) {
val base = if (toAccountDir) accountPath else basePath
val driveFolder = if (folder == null) base else {
getFolderRequestBuilder(base, folder, true)
base.appendPath(folder)
}
return saveInputStream(
saveInputStream(
itemWithPath(driveFolder.appendPath(fileName)),
toInputStream(fileContents, maybeEncrypt)
)
Expand Down Expand Up @@ -330,7 +330,7 @@ class OneDriveBackendProvider internal constructor(context: Context, folderName:
override fun childrenForCollection(folder: DriveItem?): Collection<DriveItem> {
return (folder ?: accountRes).let {
itemWithId(it.id!!).children().safeGet()?.getAll()
} ?: emptyList()
}?.filter { it.size?.compareTo(0) != 0 } ?: emptyList()
}

override fun getInputStream(resource: DriveItem) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class WebDavBackendProvider @SuppressLint("MissingPermission") internal construc
fileContents: String,
mimeType: String,
maybeEncrypt: Boolean
): DavResource {
) {
val base = if (toAccountDir) accountRes else webDavClient.base
val parent = if (folder != null) {
webDavClient.mkCol(folder, base)
Expand All @@ -239,18 +239,16 @@ class WebDavBackendProvider @SuppressLint("MissingPermission") internal construc
}
}
} else base
return saveFileContents(fileName, fileContents, mimeType, maybeEncrypt, parent)
saveFileContents(fileName, fileContents, mimeType, maybeEncrypt, parent)
}

private fun transform(e: HttpException): IOException? {
return if (e.cause is IOException) e.cause as IOException? else IOException(e)
}
private fun transform(e: HttpException) = e.cause as? IOException ?: IOException(e)

@Throws(IOException::class)
private fun saveFileContents(
fileName: String, fileContents: String, mimeType: String,
maybeEncrypt: Boolean, parent: DavResource
): DavResource {
) {
val encrypt = isEncrypted && maybeEncrypt
val mediaType: MediaType? = "$mimeType; charset=utf-8".toMediaTypeOrNull()
val requestBody: RequestBody = if (encrypt) object : RequestBody() {
Expand All @@ -269,9 +267,9 @@ class WebDavBackendProvider @SuppressLint("MissingPermission") internal construc
}
} else fileContents.toRequestBody(mediaType)
try {
return webDavClient.upload(fileName, requestBody, parent)
webDavClient.upload(fileName, requestBody, parent)
} catch (e: HttpException) {
throw transform(e)!!
throw transform(e)
}
}

Expand Down

0 comments on commit a2bf79e

Please sign in to comment.