Skip to content

Commit

Permalink
Move type check of url to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlatsch committed Jan 12, 2025
1 parent b333e09 commit d9f99d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ enum class PhotoType(
* Used in converters.
*/
fun fromValue(value: Int) = values().first { it.value == value }

fun fromMimeType(mimeType: String?): PhotoType = when (mimeType) {
PNG.mimeType -> PNG
JPEG.mimeType -> JPEG
GIF.mimeType -> GIF
MP4.mimeType -> MP4
MPEG.mimeType -> MPEG
WEBM.mimeType -> WEBM
MOV.mimeType -> MOV
else -> UNDEFINED
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,10 @@ class PhotoRepository @Inject constructor(
* Returns re created uuid
*/
suspend fun safeImportPhoto(sourceUri: Uri): String {
val type = when (app.contentResolver.getType(sourceUri)) {
PhotoType.PNG.mimeType -> PhotoType.PNG
PhotoType.JPEG.mimeType -> PhotoType.JPEG
PhotoType.GIF.mimeType -> PhotoType.GIF
PhotoType.MP4.mimeType -> PhotoType.MP4
PhotoType.MPEG.mimeType -> PhotoType.MPEG
PhotoType.WEBM.mimeType -> PhotoType.WEBM
PhotoType.MOV.mimeType -> PhotoType.MOV
else -> return String.empty
}
val mimeType = app.contentResolver.getType(sourceUri)
val type = PhotoType.fromMimeType(mimeType)

if (type == PhotoType.UNDEFINED) return String.empty

val fileName =
getFileName(app.contentResolver, sourceUri) ?: UUID.randomUUID().toString()
Expand Down

0 comments on commit d9f99d7

Please sign in to comment.