index.uts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { UTSiOS } from "DCloudUTSFoundation"
  2. import { URL, FileManager, NSData, Data } from 'Foundation';
  3. import { UTTypeCreatePreferredIdentifierForTag, kUTTagClassFilenameExtension, UTTypeCopyPreferredTagWithClass, kUTTagClassMIMEType } from "MobileCoreServices"
  4. import { ProcessFileOptions, NullableString } from '../interface'
  5. export function getResourcePath(filePath : string) : string {
  6. let path = filePath;
  7. if (uri.startsWith("http") || uri.startsWith("<svg") || uri.startsWith("data:image/")) {
  8. return uri
  9. }
  10. if (path.startsWith("file://")) {
  11. path = path.substring(7) //path.replace("file://", "")
  12. } else if (!path.startsWith("/var/")) {
  13. path = UTSiOS.getResourcePath(filePath);
  14. }
  15. return path
  16. }
  17. function getMimeType(filePath : string) : NullableString {
  18. let path = getResourcePath(filePath)
  19. if(!FileManager.default.fileExists(atPath = path)) return null
  20. const pathExtension = new URL(fileURLWithPath = path).pathExtension;
  21. const mimeType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, null)?.takeRetainedValue()
  22. if(mimeType == null) return null
  23. const mimeTypeString = UTTypeCopyPreferredTagWithClass(mimeType!, kUTTagClassMIMEType)?.takeRetainedValue();
  24. if(mimeTypeString == null) return null
  25. return mimeTypeString as string
  26. }
  27. export function fileToBase64(filePath : string) : NullableString {
  28. let path = getResourcePath(filePath)
  29. if(!FileManager.default.fileExists(atPath = path)) return null;
  30. const fileData = FileManager.default.contents(atPath = path);
  31. if(fileData == null) return null;
  32. return fileData!.base64EncodedString(options = NSData.Base64EncodingOptions.lineLength64Characters)
  33. }
  34. export function fileToDataURL(filePath : string) : NullableString {
  35. const base64 = fileToBase64(filePath)
  36. const mimeType = getMimeType(filePath)
  37. if(base64 == null || mimeType == null) return null
  38. return "data:" + mimeType! + ";base64," + base64!;
  39. }
  40. function getFileExtensionFromDataURL(dataURL : string) : string {
  41. const commaIndex = dataURL.indexOf(",");
  42. const mimeType = dataURL.substring(0, commaIndex).replace("data:", "").replace(";base64", "");
  43. const mimeTypeParts = mimeType.split("/");
  44. return mimeTypeParts[1];
  45. }
  46. export function dataURLToFile(dataURL : string, filename : NullableString = null) : NullableString {
  47. const commaIndex = dataURL.indexOf(",");
  48. const base64 = dataURL.substring(commaIndex + 1);
  49. const data = new Data(base64Encoded = base64);
  50. const dataPath = UTSiOS.getDataPath();
  51. const name = filename ?? `${Date.now()}.${getFileExtensionFromDataURL(dataURL)}`;
  52. if(data == null) return null
  53. let temporaryDirectoryURL = new URL(fileURLWithPath = dataPath);
  54. let fileURL = temporaryDirectoryURL.appendingPathComponent(name);
  55. try{
  56. UTSiOS.try(data!.write(to = fileURL))
  57. return `${dataPath}${name}`
  58. }catch(e){
  59. return null
  60. }
  61. }
  62. export function processFile(options: ProcessFileOptions){
  63. if(options.type == 'toBase64'){
  64. const res = fileToBase64(options.path)
  65. const err = 'fileToBase64: 解析失败'
  66. if(res != null){
  67. options.success?.(res!)
  68. options.complete?.(res!)
  69. } else {
  70. options.complete?.(err)
  71. options.fail?.(err)
  72. }
  73. } else if(options.type == 'toDataURL'){
  74. const res = fileToDataURL(options.path)
  75. const err = 'fileToDataURL: 解析失败'
  76. if(res != null){
  77. options.success?.(res!)
  78. options.complete?.(res!)
  79. } else {
  80. options.complete?.(err)
  81. options.fail?.(err)
  82. }
  83. } else if(options.type == 'toFile'){
  84. const res = dataURLToFile(options.path, options.filename)
  85. const err = 'dataURLToFile: 解析失败'
  86. if(res != null){
  87. options.success?.(res!)
  88. options.complete?.(res!)
  89. } else {
  90. options.complete?.(err)
  91. options.fail?.(err)
  92. }
  93. }
  94. }