-
Notifications
You must be signed in to change notification settings - Fork 18
feat(android): list folder contents in SAF documents provider #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/PB-5926-document-query-roots
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.internxt.cloud.documents | ||
|
|
||
| object DocumentId { | ||
|
|
||
| enum class Kind { FOLDER, FILE } | ||
|
|
||
| data class Decoded(val kind: Kind, val uuid: String) | ||
|
|
||
| private const val FOLDER_PREFIX = "f:" | ||
| private const val FILE_PREFIX = "d:" | ||
|
|
||
| fun encodeFolder(uuid: String): String = FOLDER_PREFIX + uuid | ||
| fun encodeFile(uuid: String): String = FILE_PREFIX + uuid | ||
|
|
||
| fun decode(id: String): Decoded? = when { | ||
| id.startsWith(FOLDER_PREFIX) -> Decoded(Kind.FOLDER, id.removePrefix(FOLDER_PREFIX)) | ||
| id.startsWith(FILE_PREFIX) -> Decoded(Kind.FILE, id.removePrefix(FILE_PREFIX)) | ||
| else -> null | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.internxt.cloud.documents | ||
|
|
||
| import android.provider.DocumentsContract.Document | ||
| import com.internxt.cloud.documents.api.model.DriveFile | ||
| import com.internxt.cloud.documents.api.model.DriveFolder | ||
| import java.time.Instant | ||
| import java.time.format.DateTimeParseException | ||
|
|
||
| object DocumentRowBuilder { | ||
|
|
||
| private const val FOLDER_FLAGS = Document.FLAG_DIR_SUPPORTS_CREATE | ||
| private const val FILE_FLAGS = 0 | ||
|
|
||
| fun folderRow(folder: DriveFolder): Map<String, Any?> = folderRow( | ||
| uuid = folder.uuid, | ||
| displayName = folder.plainName, | ||
| lastModified = parseIsoToMillis(folder.updatedAt), | ||
| ) | ||
|
|
||
| fun folderRow(uuid: String, displayName: String, lastModified: Long? = null): Map<String, Any?> = mapOf( | ||
| Document.COLUMN_DOCUMENT_ID to DocumentId.encodeFolder(uuid), | ||
| Document.COLUMN_MIME_TYPE to Document.MIME_TYPE_DIR, | ||
| Document.COLUMN_DISPLAY_NAME to displayName, | ||
| Document.COLUMN_LAST_MODIFIED to lastModified, | ||
| Document.COLUMN_FLAGS to FOLDER_FLAGS, | ||
| Document.COLUMN_SIZE to null, | ||
| ) | ||
|
|
||
| fun fileRow(file: DriveFile): Map<String, Any?> = mapOf( | ||
| Document.COLUMN_DOCUMENT_ID to DocumentId.encodeFile(file.uuid), | ||
| Document.COLUMN_MIME_TYPE to MimeTypes.fromExtension(file.type), | ||
| Document.COLUMN_DISPLAY_NAME to file.plainName, | ||
| Document.COLUMN_LAST_MODIFIED to parseIsoToMillis(file.updatedAt), | ||
| Document.COLUMN_FLAGS to FILE_FLAGS, | ||
| Document.COLUMN_SIZE to file.size, | ||
| ) | ||
|
|
||
| internal fun parseIsoToMillis(iso: String?): Long? { | ||
| if (iso.isNullOrBlank()) return null | ||
| return try { | ||
| Instant.parse(iso).toEpochMilli() | ||
| } catch (_: DateTimeParseException) { | ||
| null | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.internxt.cloud.documents | ||
|
|
||
| import android.webkit.MimeTypeMap | ||
|
|
||
| object MimeTypes { | ||
|
|
||
| const val DEFAULT = "application/octet-stream" | ||
|
|
||
| private val TABLE = mapOf( | ||
| "pdf" to "application/pdf", | ||
| "png" to "image/png", | ||
| "jpg" to "image/jpeg", | ||
| "jpeg" to "image/jpeg", | ||
| "gif" to "image/gif", | ||
| "webp" to "image/webp", | ||
| "mp4" to "video/mp4", | ||
| "mov" to "video/quicktime", | ||
| "mp3" to "audio/mpeg", | ||
| "wav" to "audio/wav", | ||
| "txt" to "text/plain", | ||
| "csv" to "text/csv", | ||
| "json" to "application/json", | ||
| "xml" to "application/xml", | ||
| "html" to "text/html", | ||
| "zip" to "application/zip", | ||
| "doc" to "application/msword", | ||
| "docx" to "application/vnd.openxmlformats-officedocument.wordprocessingml.document", | ||
| "xls" to "application/vnd.ms-excel", | ||
| "xlsx" to "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
| "ppt" to "application/vnd.ms-powerpoint", | ||
| "pptx" to "application/vnd.openxmlformats-officedocument.presentationml.presentation", | ||
| ) | ||
|
|
||
| fun fromExtension(type: String?): String { | ||
| val key = type?.trim()?.lowercase().orEmpty() | ||
| if (key.isEmpty()) return DEFAULT | ||
| return TABLE[key] ?: lookupSystem(key) ?: DEFAULT | ||
| } | ||
|
|
||
| private fun lookupSystem(extension: String): String? = try { | ||
| MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) | ||
| } catch (_: Throwable) { | ||
| null | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,16 @@ class InternxtApiClient( | |
| fun listFolderFiles(parentUuid: String, offset: Int = 0, limit: Int = DEFAULT_PAGE_SIZE): List<DriveFile> = | ||
| listChildren(parentUuid, kind = "files", jsonKey = "files", offset, limit, ::parseFile) | ||
|
|
||
| fun getFolder(uuid: String): DriveFolder? = getMeta("folders/$uuid/meta", ::parseFolder) | ||
|
|
||
| fun getFile(uuid: String): DriveFile? = getMeta("files/$uuid/meta", ::parseFile) | ||
|
|
||
| private fun <T> getMeta(path: String, parse: (JSONObject) -> T): T? = try { | ||
| parse(executeApiRequest(driveRequest(driveUrl(path)).get().build())) | ||
| } catch (_: InternxtApiException.NotFoundException) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens here if exceptions other than
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unauthorized, ApiError, NetworkException, or a JSONException from a malformed response, is passed back to the caller. That keeps “missing” separate from actual failures, so the provider can log, retry, or report |
||
| null | ||
| } | ||
|
|
||
| private fun <T> listChildren( | ||
| parentUuid: String, | ||
| kind: String, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say that the current implementation blocks the provider thread until all pages have been fetched, so the user sees nothing until the entire folder has been loaded. This works, but it isn’t scalable for large folders. The correct approach is to return the first page immediately, abd tgeb load the remaining pages in a background coroutine