-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MIME types for camera pictures in file chooser (#60)
- Loading branch information
1 parent
090ff15
commit bc8dd64
Showing
7 changed files
with
203 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
android/racehorse/src/main/java/org/racehorse/utils/Files.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.racehorse.utils | ||
|
||
import java.io.DataInputStream | ||
import java.io.File | ||
|
||
/** | ||
* Map from a file signature to a corresponding MIME type. | ||
* | ||
* [List of file signatures](https://en.wikipedia.org/wiki/List_of_file_signatures) | ||
* [Binary signatures](https://www.den4b.com/wiki/ReNamer%3aBinary_Signatures) | ||
*/ | ||
val mimeTypeSignatureMap = arrayListOf( | ||
0xFF_D8_FF_00_00_00_00_00U to "image/jpeg", | ||
0x47_49_46_38_37_61_00_00U to "image/gif", | ||
0x47_49_46_38_39_61_00_00U to "image/gif", | ||
0x89_50_4E_47_0D_0A_1A_0AU to "image/png", | ||
0x52_49_46_46_00_00_00_00U to "image/webp", | ||
0x49_49_2A_00_00_00_00_00U to "image/tiff", | ||
0x4D_4D_00_2A_00_00_00_00U to "image/tiff", | ||
0x66_74_79_70_69_73_6F_6DU to "video/mp4", | ||
0x66_74_79_70_4D_53_4E_56U to "video/mp4", | ||
0x00_00_00_18_66_74_79_70U to "video/mp4", | ||
0x1A_45_DF_A3_00_00_00_00U to "video/webm", | ||
) | ||
|
||
/** | ||
* Returns MIME type of a file from its leading bytes. | ||
*/ | ||
fun File.getMimeTypeFromSignature(): String? = try { | ||
val signature = DataInputStream(inputStream()).use(DataInputStream::readLong).toULong() | ||
|
||
mimeTypeSignatureMap.find { (mask) -> signature and mask == mask }?.second | ||
} catch (_: Throwable) { | ||
null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.