index.uts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import Base64 from "android.util.Base64";
  2. import MimeTypeMap from "android.webkit.MimeTypeMap";
  3. import ByteArrayOutputStream from 'java.io.ByteArrayOutputStream';
  4. import File from "java.io.File";
  5. import FileInputStream from "java.io.FileInputStream";
  6. import FileOutputStream from "java.io.FileOutputStream";
  7. import InputStream from 'java.io.InputStream';
  8. // import IOException from "java.io.IOException";
  9. import {ProcessFileOptions, NullableString} from '../interface'
  10. type NullByteArray = ByteArray | null
  11. function inputStreamToArray(inputStream : InputStream) : NullByteArray {
  12. try {
  13. let bos : ByteArrayOutputStream = new ByteArrayOutputStream()
  14. let bytes : ByteArray = new ByteArray(1024)
  15. do {
  16. let length = inputStream.read(bytes)
  17. if (length != -1) {
  18. bos.write(bytes, 0, length)
  19. } else {
  20. break
  21. }
  22. } while (true)
  23. bos.close()
  24. return bos.toByteArray()
  25. } catch (e : Throwable) {
  26. return null;
  27. }
  28. }
  29. function getMimeType(filePath : string):NullableString {
  30. const extension = MimeTypeMap.getFileExtensionFromUrl(filePath);
  31. if(extension == null) return null
  32. return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  33. }
  34. export function getResourcePath(path: string): string|null {
  35. let uri = path
  36. if (uri.startsWith("http") || uri.startsWith("<svg") || uri.startsWith("data:image/")) {
  37. return uri
  38. }
  39. if (uri.startsWith("file://")) {
  40. uri = uri.substring("file://".length)
  41. } else if (uri.startsWith("unifile://")) {
  42. uri = UTSAndroid.convert2AbsFullPath(uri)
  43. } else {
  44. uri = UTSAndroid.convert2AbsFullPath(uri)
  45. if (uri.startsWith("/android_asset/")){
  46. try {
  47. const context = UTSAndroid.getUniActivity()!;
  48. const inputStream = context.getResources()!.getAssets().open(path.replace('/android_asset/',''))
  49. inputStream.close();
  50. return uri
  51. } catch(e) {
  52. return null
  53. }
  54. }
  55. }
  56. const file = new File(uri)
  57. if(file.exists()){
  58. return uri
  59. }
  60. return null
  61. }
  62. export function fileToBase64(filePath : string) : NullableString {
  63. try {
  64. const context = UTSAndroid.getUniActivity()!;
  65. let path = filePath;
  66. let imageBytes : NullByteArray = null
  67. if (path.startsWith("file://")) {
  68. path = path.replace("file://", "")
  69. } else{
  70. // if(!path.startsWith("/storage") && !path.startsWith("/android_asset/"))
  71. // path = UTSAndroid.getResourcePath(path)
  72. path = UTSAndroid.convert2AbsFullPath(path)
  73. }
  74. if (path.startsWith("/android_asset/")) {
  75. imageBytes = inputStreamToArray(context.getResources()!.getAssets().open(path.replace('/android_asset/','')))
  76. } else {
  77. const file = new File(path)
  78. if(file.exists()){
  79. let fis: FileInputStream = new FileInputStream(file);
  80. imageBytes = inputStreamToArray(fis);
  81. fis.close();
  82. }
  83. }
  84. if(imageBytes == null) return null
  85. return Base64.encodeToString(imageBytes, Base64.DEFAULT)
  86. } catch (e) {
  87. return null
  88. }
  89. }
  90. export function fileToDataURL(filePath : string) : NullableString {
  91. const base64 = fileToBase64(filePath)
  92. const mimeType = getMimeType(filePath);
  93. if(base64 == null || mimeType == null) return null;
  94. return "data:" + mimeType + ";base64," + base64;
  95. }
  96. function getFileExtensionFromDataURL(dataURL: string): string{
  97. const commaIndex = dataURL.indexOf(",");
  98. const mimeType = dataURL.substring(0, commaIndex).replace("data:", "").replace(";base64", "");
  99. const mimeTypeParts = mimeType.split("/");
  100. return mimeTypeParts[1];
  101. }
  102. function dataURLToBytes(dataURL: string): ByteArray{
  103. const commaIndex = dataURL.indexOf(",");
  104. const base64 = dataURL.substring(commaIndex + 1);
  105. return Base64.decode(base64, Base64.DEFAULT);
  106. }
  107. export function dataURLToFile(dataURL: string, filename: NullableString = null):NullableString{
  108. try {
  109. const bytes = dataURLToBytes(dataURL);
  110. const name = filename ?? `${Date.now()}.${getFileExtensionFromDataURL(dataURL)}`;
  111. const cacheDir = UTSAndroid.getAppCachePath();
  112. const destFile = new File(cacheDir, name);
  113. const fos = new FileOutputStream(destFile)
  114. fos.write(bytes);
  115. fos.close();
  116. return `${cacheDir}${name}`
  117. } catch(e) {
  118. return null
  119. }
  120. }
  121. export function processFile(options: ProcessFileOptions){
  122. if(options.type == 'toBase64'){
  123. const res = fileToBase64(options.path)
  124. const err = 'fileToBase64: 解析失败'
  125. if(res != null){
  126. options.success?.(res)
  127. options.complete?.(res)
  128. } else {
  129. options.complete?.(err)
  130. options.fail?.(err)
  131. }
  132. } else if(options.type == 'toDataURL'){
  133. const res = fileToDataURL(options.path)
  134. const err = 'fileToDataURL: 解析失败'
  135. if(res != null){
  136. options.success?.(res)
  137. options.complete?.(res)
  138. } else {
  139. options.complete?.(err)
  140. options.fail?.(err)
  141. }
  142. } else if(options.type == 'toFile'){
  143. const res = dataURLToFile(options.path, options.filename)
  144. const err = 'dataURLToFile: 解析失败'
  145. if(res != null){
  146. options.success?.(res)
  147. options.complete?.(res)
  148. } else {
  149. options.complete?.(err)
  150. options.fail?.(err)
  151. }
  152. }
  153. }