合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.5 KiB

3 months ago
  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. import { Bool } from 'Swift';
  6. export function getResourcePath(filePath : string) : string {
  7. let path = filePath;
  8. if (path.startsWith("http") || path.startsWith("<svg") || path.startsWith("data:image/")) {
  9. return path
  10. }
  11. if (path.startsWith("file://")) {
  12. path = path.substring(7) //path.replace("file://", "")
  13. } else if (!path.startsWith("/var/")) {
  14. path = UTSiOS.getResourcePath(filePath);
  15. }
  16. return path
  17. }
  18. /**
  19. *
  20. */
  21. export function checkExistence(filePath : string):boolean[] {
  22. let path = getResourcePath(filePath)
  23. let isDirectory:ObjCBool = false
  24. const exists = FileManager.default.fileExists(atPath = path, isDirectory = UTSiOS.getPointer(isDirectory))
  25. return [exists,isDirectory.boolValue]
  26. }
  27. export function isFile(filePath : string):boolean {
  28. const result = checkExistence(filePath);
  29. return result[0] && !result[1]
  30. }
  31. export function isExists(filePath : string):boolean {
  32. const result = checkExistence(filePath);
  33. return result[0]
  34. }
  35. export function isDirectory(filePath : string):boolean {
  36. const result = checkExistence(filePath);
  37. return result[0] && result[1]
  38. }
  39. function getMimeType(filePath : string) : NullableString {
  40. let path = getResourcePath(filePath)
  41. if(!FileManager.default.fileExists(atPath = path)) return null
  42. const pathExtension = new URL(fileURLWithPath = path).pathExtension;
  43. const mimeType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, null)?.takeRetainedValue()
  44. if(mimeType == null) return null
  45. const mimeTypeString = UTTypeCopyPreferredTagWithClass(mimeType!, kUTTagClassMIMEType)?.takeRetainedValue();
  46. if(mimeTypeString == null) return null
  47. return mimeTypeString as string
  48. }
  49. export function fileToBase64(filePath : string) : NullableString {
  50. let path = getResourcePath(filePath)
  51. if(!FileManager.default.fileExists(atPath = path)) return null;
  52. const fileData = FileManager.default.contents(atPath = path);
  53. if(fileData == null) return null;
  54. return fileData!.base64EncodedString(options = NSData.Base64EncodingOptions.lineLength64Characters)//.replace(/\s+/g,'')
  55. }
  56. export function fileToDataURL(filePath : string) : NullableString {
  57. const base64 = fileToBase64(filePath)
  58. const mimeType = getMimeType(filePath)
  59. if(base64 == null || mimeType == null) return null
  60. return ("data:" + mimeType! + ";base64," + base64!)//.replace(/\s+/g,'');
  61. }
  62. function getFileExtensionFromDataURL(dataURL : string) : string {
  63. const commaIndex = dataURL.indexOf(",");
  64. const mimeType = dataURL.substring(0, commaIndex).replace("data:", "").replace(";base64", "");
  65. const mimeTypeParts = mimeType.split("/");
  66. return mimeTypeParts[1];
  67. }
  68. export function dataURLToFile(dataURL : string, filename : NullableString = null) : NullableString {
  69. const commaIndex = dataURL.indexOf(",");
  70. const base64 = dataURL.substring(commaIndex + 1);
  71. const data = new Data(base64Encoded = base64);
  72. // #ifdef UNI-APP-X
  73. const dataPath = UTSiOS.getDataPath();
  74. // #endif
  75. // #ifndef UNI-APP-X
  76. const dataPath = UTSiOS.getDataPath().replace(/data$/, "doc");
  77. // #endif
  78. const name = filename ?? `${Date.now()}.${getFileExtensionFromDataURL(dataURL)}`;
  79. if(data == null) return null
  80. let temporaryDirectoryURL = new URL(fileURLWithPath = dataPath);
  81. let fileURL = temporaryDirectoryURL.appendingPathComponent(name);
  82. try{
  83. UTSiOS.try(data!.write(to = fileURL))
  84. return `${dataPath}/${name}`
  85. }catch(e){
  86. return null
  87. }
  88. }
  89. export function processFile(options: ProcessFileOptions){
  90. if(options.type == 'toBase64'){
  91. const res = fileToBase64(options.path)
  92. const err = 'fileToBase64: 解析失败'
  93. if(res != null){
  94. options.success?.(res!)
  95. options.complete?.(res!)
  96. } else {
  97. options.complete?.(err)
  98. options.fail?.(err)
  99. }
  100. } else if(options.type == 'toDataURL'){
  101. const res = fileToDataURL(options.path)
  102. const err = 'fileToDataURL: 解析失败'
  103. if(res != null){
  104. options.success?.(res!)
  105. options.complete?.(res!)
  106. } else {
  107. options.complete?.(err)
  108. options.fail?.(err)
  109. }
  110. } else if(options.type == 'toFile'){
  111. const res = dataURLToFile(options.path, options.filename)
  112. const err = 'dataURLToFile: 解析失败'
  113. if(res != null){
  114. options.success?.(res!)
  115. options.complete?.(res!)
  116. } else {
  117. options.complete?.(err)
  118. options.fail?.(err)
  119. }
  120. }
  121. }