@tusi-ai/plugins 0.0.0-stage → 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -2
- package/dist/calendar/index.cjs +119 -0
- package/dist/calendar/index.d.mts +30 -0
- package/dist/calendar/index.d.ts +30 -0
- package/dist/calendar/index.js +117 -0
- package/dist/camera/index.cjs +351 -0
- package/dist/camera/index.d.mts +98 -0
- package/dist/camera/index.d.ts +98 -0
- package/dist/camera/index.js +349 -0
- package/dist/clipboard/index.cjs +139 -0
- package/dist/clipboard/index.d.mts +41 -0
- package/dist/clipboard/index.d.ts +41 -0
- package/dist/clipboard/index.js +137 -0
- package/dist/contact-picker/index.cjs +100 -0
- package/dist/contact-picker/index.d.mts +37 -0
- package/dist/contact-picker/index.d.ts +37 -0
- package/dist/contact-picker/index.js +98 -0
- package/dist/filesystem/index.cjs +447 -0
- package/dist/filesystem/index.d.mts +202 -0
- package/dist/filesystem/index.d.ts +202 -0
- package/dist/filesystem/index.js +445 -0
- package/dist/geolocation/index.cjs +114 -0
- package/dist/geolocation/index.d.mts +49 -0
- package/dist/geolocation/index.d.ts +49 -0
- package/dist/geolocation/index.js +112 -0
- package/dist/index.cjs +40 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +38 -0
- package/dist/local-notifications/index.cjs +152 -0
- package/dist/local-notifications/index.d.mts +84 -0
- package/dist/local-notifications/index.d.ts +84 -0
- package/dist/local-notifications/index.js +150 -0
- package/dist/share/index.cjs +145 -0
- package/dist/share/index.d.mts +45 -0
- package/dist/share/index.d.ts +45 -0
- package/dist/share/index.js +143 -0
- package/package.json +123 -3
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/** `pickMedia` 能选择的种类。`all` 只包含图片和视频。 */
|
|
2
|
+
type MediaType = 'all' | 'image' | 'video' | 'audio' | 'file';
|
|
3
|
+
/** 选中文件的种类。 */
|
|
4
|
+
type MediaFileType = 'image' | 'video' | 'audio' | 'file';
|
|
5
|
+
/**
|
|
6
|
+
* {@link Filesystem.pickMedia} 的入参。
|
|
7
|
+
*/
|
|
8
|
+
interface PickMediaOptions {
|
|
9
|
+
/** 要打开的选择器。不传时为 `all`。 */
|
|
10
|
+
mediaType?: MediaType;
|
|
11
|
+
/** 最多选择的个数。不传时为 9。 */
|
|
12
|
+
maxCount?: number;
|
|
13
|
+
}
|
|
14
|
+
/** 一次选择得到的文件。`previewUrl` 必须能被当前页面加载。 */
|
|
15
|
+
interface MediaFile {
|
|
16
|
+
/** 端侧路径。浏览器里与 {@link MediaFile.previewUrl} 相同。 */
|
|
17
|
+
path: string;
|
|
18
|
+
/** 文件名。 */
|
|
19
|
+
name: string;
|
|
20
|
+
/** MIME 类型。 */
|
|
21
|
+
mimeType: string;
|
|
22
|
+
/** 字节大小。 */
|
|
23
|
+
size: number;
|
|
24
|
+
/** 文件种类。 */
|
|
25
|
+
type: MediaFileType;
|
|
26
|
+
/** 时长,单位秒。图片、音频和普通文件为 0。 */
|
|
27
|
+
duration: number;
|
|
28
|
+
/** 宽度,单位像素。读不到时为 0。 */
|
|
29
|
+
width: number;
|
|
30
|
+
/** 高度,单位像素。读不到时为 0。 */
|
|
31
|
+
height: number;
|
|
32
|
+
/** 当前页面可加载的预览地址。 */
|
|
33
|
+
previewUrl: string;
|
|
34
|
+
}
|
|
35
|
+
/** {@link Filesystem.pickMedia} 的成功结果。用户取消时 `files` 为空数组。 */
|
|
36
|
+
interface PickMediaResult {
|
|
37
|
+
/** 选中的文件。 */
|
|
38
|
+
files: MediaFile[];
|
|
39
|
+
}
|
|
40
|
+
/** `saveFile` 的文件归类。 */
|
|
41
|
+
type SaveFileType = 'image' | 'video' | 'audio' | 'document' | 'other';
|
|
42
|
+
/** `saveFile` 的数据来源。 */
|
|
43
|
+
type SaveFileSource = 'url' | 'base64' | 'path';
|
|
44
|
+
/**
|
|
45
|
+
* {@link Filesystem.saveFile} 的入参。
|
|
46
|
+
*/
|
|
47
|
+
interface SaveFileOptions {
|
|
48
|
+
/** 决定保存目录和默认扩展名。 */
|
|
49
|
+
fileType: SaveFileType;
|
|
50
|
+
/** `data` 的形态。 */
|
|
51
|
+
sourceType: SaveFileSource;
|
|
52
|
+
/**
|
|
53
|
+
* 文件内容。
|
|
54
|
+
* `url` 是 http(s) 地址,`base64` 是纯 base64 或 data URL,`path` 是端上的本地路径。
|
|
55
|
+
*/
|
|
56
|
+
data: string;
|
|
57
|
+
/** 文件名。不传时按 `fileType` 补默认扩展名。 */
|
|
58
|
+
fileName?: string;
|
|
59
|
+
/** 同名时是否覆盖。`false` 时端上追加 `_1`、`_2`。 */
|
|
60
|
+
overwrite?: boolean;
|
|
61
|
+
/** 超时,单位毫秒。只在端上生效。 */
|
|
62
|
+
timeout?: number;
|
|
63
|
+
}
|
|
64
|
+
/** {@link Filesystem.saveFile} 的结果。可预期的失败用 `success: false`,不抛错。 */
|
|
65
|
+
interface SaveFileResult {
|
|
66
|
+
/** 是否已保存。 */
|
|
67
|
+
success: boolean;
|
|
68
|
+
/** 保存后的路径。浏览器下载时是文件名。 */
|
|
69
|
+
filePath: string;
|
|
70
|
+
/** 实际使用的文件名。 */
|
|
71
|
+
fileName?: string;
|
|
72
|
+
/** 字节大小。 */
|
|
73
|
+
size?: number;
|
|
74
|
+
/** `success` 为 false 时的原因。 */
|
|
75
|
+
errMsg?: string;
|
|
76
|
+
}
|
|
77
|
+
/** 应用目录。`EXTERNAL_STORAGE` 不在其中。 */
|
|
78
|
+
type FilesystemDirectory = 'DOCUMENTS' | 'DATA' | 'CACHE' | 'EXTERNAL';
|
|
79
|
+
/** 文本编码。`ascii` 和 `utf16` 只在 Android 接受。 */
|
|
80
|
+
type FilesystemEncoding = 'utf8' | 'ascii' | 'utf16';
|
|
81
|
+
/** 带目录的路径参数。不传 `directory` 时端上使用 `DOCUMENTS`。 */
|
|
82
|
+
interface DirectoryPathOptions {
|
|
83
|
+
/** 目录内的相对路径。 */
|
|
84
|
+
path: string;
|
|
85
|
+
/** 应用目录。 */
|
|
86
|
+
directory?: FilesystemDirectory;
|
|
87
|
+
}
|
|
88
|
+
/** {@link Filesystem.readFile} 的入参。 */
|
|
89
|
+
interface ReadFileOptions extends DirectoryPathOptions {
|
|
90
|
+
/** 文本编码。不传时端上按 base64 返回。 */
|
|
91
|
+
encoding?: FilesystemEncoding;
|
|
92
|
+
}
|
|
93
|
+
/** {@link Filesystem.writeFile} 的入参。 */
|
|
94
|
+
interface WriteFileOptions extends ReadFileOptions {
|
|
95
|
+
/** 要写入的内容。 */
|
|
96
|
+
data: string;
|
|
97
|
+
/** 父目录不存在时是否创建。 */
|
|
98
|
+
recursive?: boolean;
|
|
99
|
+
}
|
|
100
|
+
/** {@link Filesystem.appendFile} 的入参。 */
|
|
101
|
+
interface AppendFileOptions extends ReadFileOptions {
|
|
102
|
+
/** 追加的内容。 */
|
|
103
|
+
data: string;
|
|
104
|
+
}
|
|
105
|
+
/** 删除、建目录、列目录、取地址和 stat 共用的路径参数。 */
|
|
106
|
+
interface FilesystemPathOptions extends DirectoryPathOptions {
|
|
107
|
+
/** 父目录不存在时是否一并处理。只对 `mkdir` 和 `rmdir` 有意义。 */
|
|
108
|
+
recursive?: boolean;
|
|
109
|
+
}
|
|
110
|
+
/** 在应用目录之间移动或复制。 */
|
|
111
|
+
interface TransferOptions {
|
|
112
|
+
/** 源相对路径。 */
|
|
113
|
+
from: string;
|
|
114
|
+
/** 目标相对路径。 */
|
|
115
|
+
to: string;
|
|
116
|
+
/** 源目录。 */
|
|
117
|
+
directory?: FilesystemDirectory;
|
|
118
|
+
/** 目标目录。不传时与源目录相同。 */
|
|
119
|
+
toDirectory?: FilesystemDirectory;
|
|
120
|
+
}
|
|
121
|
+
/** 目录项或文件的元数据。 */
|
|
122
|
+
interface FilesystemFileInfo {
|
|
123
|
+
/** 名称,不含父路径。 */
|
|
124
|
+
name: string;
|
|
125
|
+
/** 是文件还是目录。 */
|
|
126
|
+
type: 'directory' | 'file';
|
|
127
|
+
/** 字节大小。 */
|
|
128
|
+
size: number;
|
|
129
|
+
/** 修改时间,Unix 毫秒。 */
|
|
130
|
+
mtime: number;
|
|
131
|
+
/** 当前页面可加载的地址。 */
|
|
132
|
+
uri: string;
|
|
133
|
+
/** 创建时间,Unix 毫秒。 */
|
|
134
|
+
ctime?: number;
|
|
135
|
+
}
|
|
136
|
+
/** {@link Filesystem.stat} 的结果。 */
|
|
137
|
+
interface FilesystemStat {
|
|
138
|
+
/** 是文件还是目录。 */
|
|
139
|
+
type: 'directory' | 'file';
|
|
140
|
+
/** 字节大小。 */
|
|
141
|
+
size: number;
|
|
142
|
+
/** 修改时间,Unix 毫秒。 */
|
|
143
|
+
mtime: number;
|
|
144
|
+
/** 当前页面可加载的地址。 */
|
|
145
|
+
uri: string;
|
|
146
|
+
/** 创建时间,Unix 毫秒。 */
|
|
147
|
+
ctime?: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 文件插件。选择和保存,以及应用目录读写,都从这里进入。
|
|
152
|
+
*/
|
|
153
|
+
declare const Filesystem: {
|
|
154
|
+
/**
|
|
155
|
+
* 打开系统选择器。
|
|
156
|
+
*
|
|
157
|
+
* @param options - 种类和数量。不传时选择图片或视频,最多 9 个
|
|
158
|
+
* @returns 选中的文件。用户取消时 `files` 为空数组
|
|
159
|
+
* @throws {@link TusiError} {@link ErrorCode.InvalidParams} 参数不合法
|
|
160
|
+
*/
|
|
161
|
+
pickMedia(options?: PickMediaOptions): Promise<PickMediaResult>;
|
|
162
|
+
/**
|
|
163
|
+
* 按文件类型保存。端上写入 `Download/YYB/`,浏览器触发下载。
|
|
164
|
+
*
|
|
165
|
+
* @param options - 类型、来源和内容
|
|
166
|
+
* @returns 保存结果。可预期的失败在 `success` 里,不抛错
|
|
167
|
+
* @throws {@link TusiError} {@link ErrorCode.InvalidParams} 参数不合法
|
|
168
|
+
*/
|
|
169
|
+
saveFile(options: SaveFileOptions): Promise<SaveFileResult>;
|
|
170
|
+
/** 读取应用目录里的文件。浏览器不提供这些目录。 */
|
|
171
|
+
readFile(options: ReadFileOptions): Promise<{
|
|
172
|
+
data: string;
|
|
173
|
+
}>;
|
|
174
|
+
/** 写入应用目录。 */
|
|
175
|
+
writeFile(options: WriteFileOptions): Promise<{
|
|
176
|
+
uri: string;
|
|
177
|
+
}>;
|
|
178
|
+
/** 向已有文件追加内容。 */
|
|
179
|
+
appendFile(options: AppendFileOptions): Promise<void>;
|
|
180
|
+
/** 删除应用目录里的文件。 */
|
|
181
|
+
deleteFile(options: FilesystemPathOptions): Promise<void>;
|
|
182
|
+
/** 创建目录。 */
|
|
183
|
+
mkdir(options: FilesystemPathOptions): Promise<void>;
|
|
184
|
+
/** 删除目录。 */
|
|
185
|
+
rmdir(options: FilesystemPathOptions): Promise<void>;
|
|
186
|
+
/** 列出目录。 */
|
|
187
|
+
readdir(options: FilesystemPathOptions): Promise<{
|
|
188
|
+
files: FilesystemFileInfo[];
|
|
189
|
+
}>;
|
|
190
|
+
/** 取当前页面可加载的地址。 */
|
|
191
|
+
getUri(options: FilesystemPathOptions): Promise<{
|
|
192
|
+
uri: string;
|
|
193
|
+
}>;
|
|
194
|
+
/** 读取大小和修改时间。 */
|
|
195
|
+
stat(options: FilesystemPathOptions): Promise<FilesystemStat>;
|
|
196
|
+
/** 改名或移动。 */
|
|
197
|
+
rename(options: TransferOptions): Promise<void>;
|
|
198
|
+
/** 复制。 */
|
|
199
|
+
copy(options: TransferOptions): Promise<void>;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export { type AppendFileOptions, Filesystem, type FilesystemDirectory, type FilesystemEncoding, type FilesystemFileInfo, type FilesystemPathOptions, type FilesystemStat, type MediaFile, type MediaFileType, type MediaType, type PickMediaOptions, type PickMediaResult, type ReadFileOptions, type SaveFileOptions, type SaveFileResult, type SaveFileSource, type SaveFileType, type TransferOptions, type WriteFileOptions };
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { getRuntimeEnv, RuntimeEnv, TusiError, ErrorCode, bridge } from '@tusi-ai/core';
|
|
2
|
+
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var BaseAdapter = class {
|
|
24
|
+
/**
|
|
25
|
+
* 绑定插件名,后续原生调用会拼成 `pluginName.method`。
|
|
26
|
+
*
|
|
27
|
+
* @param pluginName - JSB 插件名,例如 `Camera`
|
|
28
|
+
*/
|
|
29
|
+
constructor(pluginName) {
|
|
30
|
+
this.pluginName = pluginName;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 调用 `pluginName.method`。只给原生适配器使用。
|
|
34
|
+
*
|
|
35
|
+
* @param method - 插件内的方法名,例如 `getPhoto`
|
|
36
|
+
* @param params - 原样交给 {@link bridge}
|
|
37
|
+
* @returns 客户端响应的 `data`
|
|
38
|
+
*/
|
|
39
|
+
callNative(method, params) {
|
|
40
|
+
return bridge.call(`${this.pluginName}.${method}`, params);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 当前环境没有这个能力。在进入 bridge 之前抛出。
|
|
44
|
+
*
|
|
45
|
+
* @param method - 插件内的方法名
|
|
46
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
47
|
+
*/
|
|
48
|
+
unsupported(method) {
|
|
49
|
+
throw new TusiError(
|
|
50
|
+
ErrorCode.Unsupported,
|
|
51
|
+
`${this.pluginName}.${method} is not supported`,
|
|
52
|
+
`${this.pluginName}.${method}`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/filesystem/adapter.ts
|
|
58
|
+
var FilesystemAdapter = class extends BaseAdapter {
|
|
59
|
+
constructor() {
|
|
60
|
+
super("Filesystem");
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/filesystem/browser.ts
|
|
65
|
+
var ACCEPT = {
|
|
66
|
+
all: "image/*,video/*",
|
|
67
|
+
image: "image/*",
|
|
68
|
+
video: "video/*",
|
|
69
|
+
audio: "audio/*",
|
|
70
|
+
file: "*/*"
|
|
71
|
+
};
|
|
72
|
+
var DEFAULT_EXTENSION = {
|
|
73
|
+
image: ".jpg",
|
|
74
|
+
video: ".mp4",
|
|
75
|
+
audio: ".mp3",
|
|
76
|
+
document: ".pdf",
|
|
77
|
+
other: ""
|
|
78
|
+
};
|
|
79
|
+
var BrowserFilesystemAdapter = class extends FilesystemAdapter {
|
|
80
|
+
pickMedia(options) {
|
|
81
|
+
var _a, _b;
|
|
82
|
+
const mediaType = (_a = options == null ? void 0 : options.mediaType) != null ? _a : "all";
|
|
83
|
+
const maxCount = (_b = options == null ? void 0 : options.maxCount) != null ? _b : 9;
|
|
84
|
+
return openFileInput(mediaType, maxCount);
|
|
85
|
+
}
|
|
86
|
+
saveFile(options) {
|
|
87
|
+
return downloadFile(options);
|
|
88
|
+
}
|
|
89
|
+
readFile(_options) {
|
|
90
|
+
this.unsupported("readFile");
|
|
91
|
+
}
|
|
92
|
+
writeFile(_options) {
|
|
93
|
+
this.unsupported("writeFile");
|
|
94
|
+
}
|
|
95
|
+
appendFile(_options) {
|
|
96
|
+
this.unsupported("appendFile");
|
|
97
|
+
}
|
|
98
|
+
deleteFile(_options) {
|
|
99
|
+
this.unsupported("deleteFile");
|
|
100
|
+
}
|
|
101
|
+
mkdir(_options) {
|
|
102
|
+
this.unsupported("mkdir");
|
|
103
|
+
}
|
|
104
|
+
rmdir(_options) {
|
|
105
|
+
this.unsupported("rmdir");
|
|
106
|
+
}
|
|
107
|
+
readdir(_options) {
|
|
108
|
+
this.unsupported("readdir");
|
|
109
|
+
}
|
|
110
|
+
getUri(_options) {
|
|
111
|
+
this.unsupported("getUri");
|
|
112
|
+
}
|
|
113
|
+
stat(_options) {
|
|
114
|
+
this.unsupported("stat");
|
|
115
|
+
}
|
|
116
|
+
rename(_options) {
|
|
117
|
+
this.unsupported("rename");
|
|
118
|
+
}
|
|
119
|
+
copy(_options) {
|
|
120
|
+
this.unsupported("copy");
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
function openFileInput(mediaType, maxCount) {
|
|
124
|
+
return new Promise((resolve) => {
|
|
125
|
+
const input = document.createElement("input");
|
|
126
|
+
let settled = false;
|
|
127
|
+
const finish = (result) => {
|
|
128
|
+
if (settled) return;
|
|
129
|
+
settled = true;
|
|
130
|
+
input.remove();
|
|
131
|
+
resolve(result);
|
|
132
|
+
};
|
|
133
|
+
input.type = "file";
|
|
134
|
+
input.accept = ACCEPT[mediaType];
|
|
135
|
+
input.multiple = maxCount > 1;
|
|
136
|
+
input.style.display = "none";
|
|
137
|
+
input.addEventListener("change", () => {
|
|
138
|
+
var _a;
|
|
139
|
+
const selected = Array.from((_a = input.files) != null ? _a : []).slice(0, maxCount);
|
|
140
|
+
void mapFiles(selected, mediaType).then((files) => finish({ files }));
|
|
141
|
+
});
|
|
142
|
+
input.addEventListener("cancel", () => finish({ files: [] }));
|
|
143
|
+
document.body.appendChild(input);
|
|
144
|
+
input.click();
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function mapFiles(files, mediaType) {
|
|
148
|
+
return __async(this, null, function* () {
|
|
149
|
+
return Promise.all(files.map((file) => __async(null, null, function* () {
|
|
150
|
+
const previewUrl = URL.createObjectURL(file);
|
|
151
|
+
const size = yield readImageSize(previewUrl, file.type);
|
|
152
|
+
return {
|
|
153
|
+
path: previewUrl,
|
|
154
|
+
name: file.name,
|
|
155
|
+
mimeType: file.type,
|
|
156
|
+
size: file.size,
|
|
157
|
+
type: kindOf(file, mediaType),
|
|
158
|
+
duration: 0,
|
|
159
|
+
width: size.width,
|
|
160
|
+
height: size.height,
|
|
161
|
+
previewUrl
|
|
162
|
+
};
|
|
163
|
+
})));
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
function kindOf(file, mediaType) {
|
|
167
|
+
if (file.type.startsWith("image/")) return "image";
|
|
168
|
+
if (file.type.startsWith("video/")) return "video";
|
|
169
|
+
if (file.type.startsWith("audio/")) return "audio";
|
|
170
|
+
if (mediaType === "image" || mediaType === "video" || mediaType === "audio") return mediaType;
|
|
171
|
+
return "file";
|
|
172
|
+
}
|
|
173
|
+
function readImageSize(url, mimeType) {
|
|
174
|
+
if (typeof Image === "undefined" || !mimeType.startsWith("image/")) {
|
|
175
|
+
return Promise.resolve({ width: 0, height: 0 });
|
|
176
|
+
}
|
|
177
|
+
return new Promise((resolve) => {
|
|
178
|
+
const image = new Image();
|
|
179
|
+
image.onload = () => resolve({ width: image.naturalWidth, height: image.naturalHeight });
|
|
180
|
+
image.onerror = () => resolve({ width: 0, height: 0 });
|
|
181
|
+
image.src = url;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function downloadFile(options) {
|
|
185
|
+
return __async(this, null, function* () {
|
|
186
|
+
try {
|
|
187
|
+
const blob = yield toBlob(options);
|
|
188
|
+
const fileName = resolveFileName(options);
|
|
189
|
+
const url = URL.createObjectURL(blob);
|
|
190
|
+
const anchor = document.createElement("a");
|
|
191
|
+
anchor.href = url;
|
|
192
|
+
anchor.download = fileName;
|
|
193
|
+
anchor.click();
|
|
194
|
+
URL.revokeObjectURL(url);
|
|
195
|
+
return {
|
|
196
|
+
success: true,
|
|
197
|
+
filePath: fileName,
|
|
198
|
+
fileName,
|
|
199
|
+
size: blob.size
|
|
200
|
+
};
|
|
201
|
+
} catch (error) {
|
|
202
|
+
return {
|
|
203
|
+
success: false,
|
|
204
|
+
filePath: "",
|
|
205
|
+
errMsg: error instanceof Error ? error.message : String(error)
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
function toBlob(options) {
|
|
211
|
+
return __async(this, null, function* () {
|
|
212
|
+
if (options.sourceType === "base64") {
|
|
213
|
+
return decodeBase64(options.data);
|
|
214
|
+
}
|
|
215
|
+
if (options.sourceType === "url" || /^https?:|^blob:/.test(options.data)) {
|
|
216
|
+
const response = yield fetch(options.data);
|
|
217
|
+
if (!response.ok) {
|
|
218
|
+
throw new Error(`Download failed: ${response.status}`);
|
|
219
|
+
}
|
|
220
|
+
return response.blob();
|
|
221
|
+
}
|
|
222
|
+
throw new Error("Local path is not readable in the browser");
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
function decodeBase64(data) {
|
|
226
|
+
const payload = data.includes(",") ? data.slice(data.indexOf(",") + 1) : data;
|
|
227
|
+
const binary = atob(payload);
|
|
228
|
+
const bytes = new Uint8Array(binary.length);
|
|
229
|
+
for (let index = 0; index < binary.length; index += 1) {
|
|
230
|
+
bytes[index] = binary.charCodeAt(index);
|
|
231
|
+
}
|
|
232
|
+
return new Blob([bytes.buffer.slice(0)]);
|
|
233
|
+
}
|
|
234
|
+
function resolveFileName(options) {
|
|
235
|
+
const extension = DEFAULT_EXTENSION[options.fileType];
|
|
236
|
+
const fallback = `yyb_${Date.now()}${extension}`;
|
|
237
|
+
if (!options.fileName) return fallback;
|
|
238
|
+
const cleaned = options.fileName.replace(/[/\\]/g, "_").replace(/^\.+/, "");
|
|
239
|
+
return cleaned || fallback;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/filesystem/native.ts
|
|
243
|
+
var NativeFilesystemAdapter = class extends FilesystemAdapter {
|
|
244
|
+
pickMedia(options) {
|
|
245
|
+
return this.callNative("pickMedia", options);
|
|
246
|
+
}
|
|
247
|
+
saveFile(options) {
|
|
248
|
+
return this.callNative("saveFile", options);
|
|
249
|
+
}
|
|
250
|
+
readFile(options) {
|
|
251
|
+
return this.callNative("readFile", options);
|
|
252
|
+
}
|
|
253
|
+
writeFile(options) {
|
|
254
|
+
return this.callNative("writeFile", options);
|
|
255
|
+
}
|
|
256
|
+
appendFile(options) {
|
|
257
|
+
return this.callNative("appendFile", options);
|
|
258
|
+
}
|
|
259
|
+
deleteFile(options) {
|
|
260
|
+
return this.callNative("deleteFile", options);
|
|
261
|
+
}
|
|
262
|
+
mkdir(options) {
|
|
263
|
+
return this.callNative("mkdir", options);
|
|
264
|
+
}
|
|
265
|
+
rmdir(options) {
|
|
266
|
+
return this.callNative("rmdir", options);
|
|
267
|
+
}
|
|
268
|
+
readdir(options) {
|
|
269
|
+
return this.callNative("readdir", options);
|
|
270
|
+
}
|
|
271
|
+
getUri(options) {
|
|
272
|
+
return this.callNative("getUri", options);
|
|
273
|
+
}
|
|
274
|
+
stat(options) {
|
|
275
|
+
return this.callNative("stat", options);
|
|
276
|
+
}
|
|
277
|
+
rename(options) {
|
|
278
|
+
return this.callNative("rename", options);
|
|
279
|
+
}
|
|
280
|
+
copy(options) {
|
|
281
|
+
return this.callNative("copy", options);
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// src/filesystem/index.ts
|
|
286
|
+
var MEDIA_TYPES = ["all", "image", "video", "audio", "file"];
|
|
287
|
+
var FILE_TYPES = ["image", "video", "audio", "document", "other"];
|
|
288
|
+
var SOURCE_TYPES = ["url", "base64", "path"];
|
|
289
|
+
var DIRECTORIES = ["DOCUMENTS", "DATA", "CACHE", "EXTERNAL"];
|
|
290
|
+
var adapter;
|
|
291
|
+
function getAdapter() {
|
|
292
|
+
adapter != null ? adapter : adapter = getRuntimeEnv() === RuntimeEnv.Browser ? new BrowserFilesystemAdapter() : new NativeFilesystemAdapter();
|
|
293
|
+
return adapter;
|
|
294
|
+
}
|
|
295
|
+
function invalid(method, message) {
|
|
296
|
+
throw new TusiError(ErrorCode.InvalidParams, message, method);
|
|
297
|
+
}
|
|
298
|
+
function assertMedia(options) {
|
|
299
|
+
if ((options == null ? void 0 : options.mediaType) !== void 0 && !MEDIA_TYPES.includes(options.mediaType)) {
|
|
300
|
+
invalid("Filesystem.pickMedia", "mediaType is not allowed");
|
|
301
|
+
}
|
|
302
|
+
if ((options == null ? void 0 : options.maxCount) !== void 0 && (!Number.isInteger(options.maxCount) || options.maxCount < 1)) {
|
|
303
|
+
invalid("Filesystem.pickMedia", "maxCount must be a positive integer");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function assertSave(options) {
|
|
307
|
+
if (!options || !FILE_TYPES.includes(options.fileType)) {
|
|
308
|
+
invalid("Filesystem.saveFile", "fileType is not allowed");
|
|
309
|
+
}
|
|
310
|
+
if (!SOURCE_TYPES.includes(options.sourceType)) {
|
|
311
|
+
invalid("Filesystem.saveFile", "sourceType is not allowed");
|
|
312
|
+
}
|
|
313
|
+
if (!options.data) {
|
|
314
|
+
invalid("Filesystem.saveFile", "data is required");
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function assertDirectory(method, directory) {
|
|
318
|
+
if (directory !== void 0 && !DIRECTORIES.includes(directory)) {
|
|
319
|
+
invalid(method, "directory is not allowed");
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function assertRelative(method, value, label) {
|
|
323
|
+
if (!value || value.startsWith("/") || value.startsWith("\\") || value.split(/[/\\]/).includes("..")) {
|
|
324
|
+
invalid(method, `${label} must be a relative path`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function assertPath(method, options) {
|
|
328
|
+
assertDirectory(method, options == null ? void 0 : options.directory);
|
|
329
|
+
assertRelative(method, options == null ? void 0 : options.path, "path");
|
|
330
|
+
}
|
|
331
|
+
function assertTransfer(method, options) {
|
|
332
|
+
assertDirectory(method, options == null ? void 0 : options.directory);
|
|
333
|
+
assertDirectory(method, options == null ? void 0 : options.toDirectory);
|
|
334
|
+
assertRelative(method, options == null ? void 0 : options.from, "from");
|
|
335
|
+
assertRelative(method, options == null ? void 0 : options.to, "to");
|
|
336
|
+
}
|
|
337
|
+
var Filesystem = {
|
|
338
|
+
/**
|
|
339
|
+
* 打开系统选择器。
|
|
340
|
+
*
|
|
341
|
+
* @param options - 种类和数量。不传时选择图片或视频,最多 9 个
|
|
342
|
+
* @returns 选中的文件。用户取消时 `files` 为空数组
|
|
343
|
+
* @throws {@link TusiError} {@link ErrorCode.InvalidParams} 参数不合法
|
|
344
|
+
*/
|
|
345
|
+
pickMedia(options) {
|
|
346
|
+
return __async(this, null, function* () {
|
|
347
|
+
assertMedia(options);
|
|
348
|
+
return getAdapter().pickMedia(options);
|
|
349
|
+
});
|
|
350
|
+
},
|
|
351
|
+
/**
|
|
352
|
+
* 按文件类型保存。端上写入 `Download/YYB/`,浏览器触发下载。
|
|
353
|
+
*
|
|
354
|
+
* @param options - 类型、来源和内容
|
|
355
|
+
* @returns 保存结果。可预期的失败在 `success` 里,不抛错
|
|
356
|
+
* @throws {@link TusiError} {@link ErrorCode.InvalidParams} 参数不合法
|
|
357
|
+
*/
|
|
358
|
+
saveFile(options) {
|
|
359
|
+
return __async(this, null, function* () {
|
|
360
|
+
assertSave(options);
|
|
361
|
+
return getAdapter().saveFile(options);
|
|
362
|
+
});
|
|
363
|
+
},
|
|
364
|
+
/** 读取应用目录里的文件。浏览器不提供这些目录。 */
|
|
365
|
+
readFile(options) {
|
|
366
|
+
return __async(this, null, function* () {
|
|
367
|
+
assertPath("Filesystem.readFile", options);
|
|
368
|
+
return getAdapter().readFile(options);
|
|
369
|
+
});
|
|
370
|
+
},
|
|
371
|
+
/** 写入应用目录。 */
|
|
372
|
+
writeFile(options) {
|
|
373
|
+
return __async(this, null, function* () {
|
|
374
|
+
assertPath("Filesystem.writeFile", options);
|
|
375
|
+
if (options.data === void 0) invalid("Filesystem.writeFile", "data is required");
|
|
376
|
+
return getAdapter().writeFile(options);
|
|
377
|
+
});
|
|
378
|
+
},
|
|
379
|
+
/** 向已有文件追加内容。 */
|
|
380
|
+
appendFile(options) {
|
|
381
|
+
return __async(this, null, function* () {
|
|
382
|
+
assertPath("Filesystem.appendFile", options);
|
|
383
|
+
if (options.data === void 0) invalid("Filesystem.appendFile", "data is required");
|
|
384
|
+
return getAdapter().appendFile(options);
|
|
385
|
+
});
|
|
386
|
+
},
|
|
387
|
+
/** 删除应用目录里的文件。 */
|
|
388
|
+
deleteFile(options) {
|
|
389
|
+
return __async(this, null, function* () {
|
|
390
|
+
assertPath("Filesystem.deleteFile", options);
|
|
391
|
+
return getAdapter().deleteFile(options);
|
|
392
|
+
});
|
|
393
|
+
},
|
|
394
|
+
/** 创建目录。 */
|
|
395
|
+
mkdir(options) {
|
|
396
|
+
return __async(this, null, function* () {
|
|
397
|
+
assertPath("Filesystem.mkdir", options);
|
|
398
|
+
return getAdapter().mkdir(options);
|
|
399
|
+
});
|
|
400
|
+
},
|
|
401
|
+
/** 删除目录。 */
|
|
402
|
+
rmdir(options) {
|
|
403
|
+
return __async(this, null, function* () {
|
|
404
|
+
assertPath("Filesystem.rmdir", options);
|
|
405
|
+
return getAdapter().rmdir(options);
|
|
406
|
+
});
|
|
407
|
+
},
|
|
408
|
+
/** 列出目录。 */
|
|
409
|
+
readdir(options) {
|
|
410
|
+
return __async(this, null, function* () {
|
|
411
|
+
assertPath("Filesystem.readdir", options);
|
|
412
|
+
return getAdapter().readdir(options);
|
|
413
|
+
});
|
|
414
|
+
},
|
|
415
|
+
/** 取当前页面可加载的地址。 */
|
|
416
|
+
getUri(options) {
|
|
417
|
+
return __async(this, null, function* () {
|
|
418
|
+
assertPath("Filesystem.getUri", options);
|
|
419
|
+
return getAdapter().getUri(options);
|
|
420
|
+
});
|
|
421
|
+
},
|
|
422
|
+
/** 读取大小和修改时间。 */
|
|
423
|
+
stat(options) {
|
|
424
|
+
return __async(this, null, function* () {
|
|
425
|
+
assertPath("Filesystem.stat", options);
|
|
426
|
+
return getAdapter().stat(options);
|
|
427
|
+
});
|
|
428
|
+
},
|
|
429
|
+
/** 改名或移动。 */
|
|
430
|
+
rename(options) {
|
|
431
|
+
return __async(this, null, function* () {
|
|
432
|
+
assertTransfer("Filesystem.rename", options);
|
|
433
|
+
return getAdapter().rename(options);
|
|
434
|
+
});
|
|
435
|
+
},
|
|
436
|
+
/** 复制。 */
|
|
437
|
+
copy(options) {
|
|
438
|
+
return __async(this, null, function* () {
|
|
439
|
+
assertTransfer("Filesystem.copy", options);
|
|
440
|
+
return getAdapter().copy(options);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
export { Filesystem };
|