diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index cd373cc607da8316fa6c06fb67372c1f03b84b9b..4a466df54fa161a52ff8337a6305cc307083a26c 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -36,7 +36,11 @@ export * from './protocols/device/soterAuthentication' export * from './protocols/storage/storage' +export * from './protocols/file/saveFile' +export * from './protocols/file/getSavedFileList' +export * from './protocols/file/removeSavedFile' export * from './protocols/file/getFileInfo' +export * from './protocols/file/getSavedFileInfo' export * from './protocols/file/openDocument' export * from './protocols/keyboard/keyboard' diff --git a/packages/uni-api/src/protocols/file/getSavedFileInfo.ts b/packages/uni-api/src/protocols/file/getSavedFileInfo.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1b3ecacf053f3e36a93d1f3e9a6b8c40bbcef5d --- /dev/null +++ b/packages/uni-api/src/protocols/file/getSavedFileInfo.ts @@ -0,0 +1,21 @@ +import { getRealPath } from '@dcloudio/uni-platform' + +export const API_GET_SAVED_FILE_INFO = 'getSavedFileInfo' +export type API_TYPE_GET_SAVED_FILE_INFO = typeof uni.getSavedFileInfo + +export const GetSavedFileInfoOptions: ApiOptions = + { + formatArgs: { + filePath(filePath, params) { + params.filePath = getRealPath(filePath) + }, + }, + } + +export const GetSavedFileInfoProtocol: ApiProtocol = + { + filePath: { + type: String, + required: true, + }, + } diff --git a/packages/uni-api/src/protocols/file/getSavedFileList.ts b/packages/uni-api/src/protocols/file/getSavedFileList.ts new file mode 100644 index 0000000000000000000000000000000000000000..46348e8f4f2370472d14a2cfaa687d813fcba684 --- /dev/null +++ b/packages/uni-api/src/protocols/file/getSavedFileList.ts @@ -0,0 +1,2 @@ +export const API_GET_SAVED_LIST = 'getSavedFileList' +export type API_TYPE_GET_SAVED_LIST = typeof uni.getSavedFileList diff --git a/packages/uni-api/src/protocols/file/removeSavedFile.ts b/packages/uni-api/src/protocols/file/removeSavedFile.ts new file mode 100644 index 0000000000000000000000000000000000000000..01186196550432fcfa1fd4f6c8078f95390a345f --- /dev/null +++ b/packages/uni-api/src/protocols/file/removeSavedFile.ts @@ -0,0 +1,19 @@ +import { getRealPath } from '@dcloudio/uni-platform' +export const API_REMOVE_SAVED_FILE = 'removeSavedFile' +export type API_TYPE_REMOVE_SAVED_FILE = typeof uni.removeSavedFile + +export const RemoveSavedFileOptions: ApiOptions = { + formatArgs: { + filePath(filePath, params) { + params.filePath = getRealPath(filePath) + }, + }, +} + +export const RemoveSavedFileProtocol: ApiProtocol = + { + filePath: { + type: String, + required: true, + }, + } diff --git a/packages/uni-api/src/protocols/file/saveFile.ts b/packages/uni-api/src/protocols/file/saveFile.ts new file mode 100644 index 0000000000000000000000000000000000000000..056cc8fa23b0cea077af14b9bb274db2f0d8532f --- /dev/null +++ b/packages/uni-api/src/protocols/file/saveFile.ts @@ -0,0 +1,18 @@ +import { getRealPath } from '@dcloudio/uni-platform' +export const API_SAVE_FILE = 'saveFile' +export type API_TYPE_SAVE_FILE = typeof uni.saveFile + +export const SaveFileOptions: ApiOptions = { + formatArgs: { + tempFilePath(savedFilePath, params) { + params.tempFilePath = getRealPath(savedFilePath) + }, + }, +} + +export const SaveFileProtocol: ApiProtocol = { + tempFilePath: { + type: String, + required: true, + }, +} diff --git a/packages/uni-app-plus/src/service/api/file/getSavedFileInfo.ts b/packages/uni-app-plus/src/service/api/file/getSavedFileInfo.ts new file mode 100644 index 0000000000000000000000000000000000000000..869cb203c5b5828067db20c4e6ef11c22c5b69ea --- /dev/null +++ b/packages/uni-app-plus/src/service/api/file/getSavedFileInfo.ts @@ -0,0 +1,34 @@ +import { + defineAsyncApi, + API_GET_SAVED_FILE_INFO, + API_TYPE_GET_SAVED_FILE_INFO, + GetSavedFileInfoProtocol, + GetSavedFileInfoOptions, +} from '@dcloudio/uni-api' +import { warpPlusErrorCallback } from '../../../helpers/plus' + +export const getSavedFileInfo = defineAsyncApi( + API_GET_SAVED_FILE_INFO, + ({ filePath }, { resolve, reject }) => { + const errorCallback = warpPlusErrorCallback(reject) + + plus.io.resolveLocalFileSystemURL( + filePath, + (entry) => { + entry.getMetadata( + (meta) => { + resolve({ + createTime: meta.modificationTime!.getTime(), + size: meta.size!, + }) + }, + errorCallback, + false + ) + }, + errorCallback + ) + }, + GetSavedFileInfoProtocol, + GetSavedFileInfoOptions +) diff --git a/packages/uni-app-plus/src/service/api/file/getSavedFileList.ts b/packages/uni-app-plus/src/service/api/file/getSavedFileList.ts new file mode 100644 index 0000000000000000000000000000000000000000..2ba294a47e3ad122a32607adfacd8b9502705e91 --- /dev/null +++ b/packages/uni-app-plus/src/service/api/file/getSavedFileList.ts @@ -0,0 +1,72 @@ +import { + defineAsyncApi, + API_GET_SAVED_LIST, + API_TYPE_GET_SAVED_LIST, +} from '@dcloudio/uni-api' +import { warpPlusErrorCallback } from '../../../helpers/plus' + +const SAVED_DIR = 'uniapp_save' + +function getSavedFileDir( + success: (res: any) => void, + fail: (err: any) => void +) { + fail = fail || function () {} + plus.io.requestFileSystem( + plus.io.PRIVATE_DOC, + (fs) => { + // 请求_doc fs + fs.root!.getDirectory( + SAVED_DIR, + { + // 获取文件保存目录对象 + create: true, + }, + success, + fail + ) + }, + fail + ) +} + +export const getSavedFileList = ( + defineAsyncApi(API_GET_SAVED_LIST, (_, { resolve, reject }) => { + const errorCallback = warpPlusErrorCallback(reject) + + getSavedFileDir((entry) => { + var reader = entry.createReader() + + var fileList: object[] = [] + reader.readEntries((entries: any[]) => { + if (entries && entries.length) { + entries.forEach((entry) => { + entry.getMetadata( + (meta: { + modificationTime: { getTime: () => void } + size: any + }) => { + fileList.push({ + filePath: plus.io.convertAbsoluteFileSystem(entry.fullPath), + createTime: meta.modificationTime.getTime(), + size: meta.size, + }) + if (fileList.length === entries.length) { + resolve({ + fileList, + }) + } + }, + errorCallback, + false + ) + }) + } else { + resolve({ + fileList, + }) + } + }, errorCallback) + }, errorCallback) + }) +) diff --git a/packages/uni-app-plus/src/service/api/file/removeSavedFile.ts b/packages/uni-app-plus/src/service/api/file/removeSavedFile.ts new file mode 100644 index 0000000000000000000000000000000000000000..0bc6c73161e71d3daacb17062f680c7a7d6cf60d --- /dev/null +++ b/packages/uni-app-plus/src/service/api/file/removeSavedFile.ts @@ -0,0 +1,27 @@ +import { + defineAsyncApi, + API_REMOVE_SAVED_FILE, + API_TYPE_REMOVE_SAVED_FILE, + RemoveSavedFileProtocol, + RemoveSavedFileOptions, +} from '@dcloudio/uni-api' +import { warpPlusErrorCallback } from '../../../helpers/plus' + +export const removeSavedFile = defineAsyncApi( + API_REMOVE_SAVED_FILE, + ({ filePath }, { resolve, reject }) => { + const errorCallback = warpPlusErrorCallback(reject) + + plus.io.resolveLocalFileSystemURL( + filePath, + (entry) => { + entry.remove(() => { + resolve() + }, errorCallback) + }, + errorCallback + ) + }, + RemoveSavedFileProtocol, + RemoveSavedFileOptions +) diff --git a/packages/uni-app-plus/src/service/api/file/saveFile.ts b/packages/uni-app-plus/src/service/api/file/saveFile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d78c73bb272308fd89246c4a664e1c03a066faf --- /dev/null +++ b/packages/uni-app-plus/src/service/api/file/saveFile.ts @@ -0,0 +1,68 @@ +import { + defineAsyncApi, + API_SAVE_FILE, + API_TYPE_SAVE_FILE, + SaveFileProtocol, + SaveFileOptions, +} from '@dcloudio/uni-api' +import { warpPlusErrorCallback } from '../../../helpers/plus' +import { getExtName } from '../../../helpers/file' +let index = 0 + +const SAVED_DIR = 'uniapp_save' +const SAVE_PATH = `_doc/${SAVED_DIR}` + +function getSavedFileDir( + success: (res: any) => void, + fail: (err: any) => void +) { + fail = fail || function () {} + plus.io.requestFileSystem( + plus.io.PRIVATE_DOC, + (fs) => { + // 请求_doc fs + fs.root!.getDirectory( + SAVED_DIR, + { + // 获取文件保存目录对象 + create: true, + }, + success, + fail + ) + }, + fail + ) +} + +export const saveFile = defineAsyncApi( + API_SAVE_FILE, + ({ tempFilePath }, { resolve, reject }) => { + const errorCallback = warpPlusErrorCallback(reject) + const fileName = `${Date.now()}${index++}${getExtName(tempFilePath)}` + + plus.io.resolveLocalFileSystemURL( + tempFilePath, + (entry) => { + // 读取临时文件 FileEntry + getSavedFileDir((dir) => { + entry.copyTo( + dir, + fileName, + () => { + // 复制临时文件 FileEntry,为了避免把相册里的文件删除,使用 copy,微信中是要删除临时文件的 + const savedFilePath = SAVE_PATH + '/' + fileName + resolve({ + savedFilePath, + }) + }, + errorCallback + ) + }, errorCallback) + }, + errorCallback + ) + }, + SaveFileProtocol, + SaveFileOptions +)