@ayden-fc2/riffle-bridge-web 1.0.5 → 1.0.6
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 +10 -8
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +30 -3
- package/dist/index.mjs +30 -3
- package/package.json +1 -1
- package/src/controllers/index.ts +41 -3
package/README.md
CHANGED
|
@@ -85,6 +85,8 @@ const bridge = new RiffleBridge();
|
|
|
85
85
|
|
|
86
86
|
针对RiffleBridge,分为震动模块、传感器模块、音频模块、相机模块、麦克风模块、文件存储模块、设备信息模块,各自的使用方法与注意事项已经分别说明,根据web应用所需模块,按需阅读使用即可。
|
|
87
87
|
|
|
88
|
+
**注意音频模块、相机模块、麦克风模块部分依赖于文件存储模块,可以先阅读文件存储模块的代码,辅助理解这三个模块中的文件存储操作**
|
|
89
|
+
|
|
88
90
|
### 3.1 震动模块
|
|
89
91
|
|
|
90
92
|
```
|
|
@@ -313,23 +315,23 @@ const base64 = await bridge.fileStorage.readAsBase64('local://photos/aydens-demo
|
|
|
313
315
|
|
|
314
316
|
将图片或视频保存到用户的系统相册(会请求系统相册写入权限)
|
|
315
317
|
|
|
318
|
+
`saveToGallery` 支持自动解析多种 URI 格式,web 应用无需关心传入的是真实路径还是虚拟映射键:
|
|
319
|
+
- `file://...` 真实文件路径:直接使用
|
|
320
|
+
- `local://...` 虚拟映射键:自动从 MAP 查询实际路径
|
|
321
|
+
- `http(s)://...` 网络 URL:自动查找缓存文件
|
|
322
|
+
|
|
316
323
|
```
|
|
317
|
-
//
|
|
324
|
+
// 拍照后保存到系统相册(直接使用 file:// 路径为例)
|
|
318
325
|
const photo = await bridge.fileStorage.takePhoto();
|
|
319
326
|
if (!photo?.cancelled) {
|
|
320
|
-
//
|
|
327
|
+
// 默认存入系统相册Riffle
|
|
321
328
|
const result = await bridge.fileStorage.saveToGallery(photo.uri);
|
|
322
329
|
console.log('保存成功:', result.filename);
|
|
323
330
|
|
|
324
|
-
//
|
|
331
|
+
// 指定自定义相册名
|
|
325
332
|
const result2 = await bridge.fileStorage.saveToGallery(photo.uri, 'MyApp相册');
|
|
326
333
|
}
|
|
327
334
|
|
|
328
|
-
// 也可以保存已缓存的文件到系统相册
|
|
329
|
-
const cached = await bridge.fileStorage.getLocalFileByUrl('local://photos/aydens-demo-photo');
|
|
330
|
-
if (cached?.exists && cached.uri) {
|
|
331
|
-
await bridge.fileStorage.saveToGallery(cached.uri);
|
|
332
|
-
}
|
|
333
335
|
```
|
|
334
336
|
|
|
335
337
|
返回值类型:
|
package/dist/index.d.mts
CHANGED
|
@@ -405,10 +405,16 @@ declare class FileStorageController {
|
|
|
405
405
|
added: boolean;
|
|
406
406
|
}>;
|
|
407
407
|
readAsBase64(uriOrKey: string): Promise<Base64Result>;
|
|
408
|
+
/**
|
|
409
|
+
* 解析 URI,返回实际的 file:// 路径
|
|
410
|
+
* @param uri 支持 file://、local:// 或其他映射键
|
|
411
|
+
*/
|
|
412
|
+
private resolveUri;
|
|
408
413
|
/**
|
|
409
414
|
* 保存图片/视频到系统相册
|
|
410
|
-
*
|
|
411
|
-
* @param
|
|
415
|
+
* 自动解析 URI:支持 file:// 真实路径、local:// 虚拟映射键、http(s):// 缓存 URL
|
|
416
|
+
* @param uri 文件 URI(支持 file://、local:// 或 http(s):// 路径)
|
|
417
|
+
* @param albumName 相册名称(默认 'Riffle')
|
|
412
418
|
*/
|
|
413
419
|
saveToGallery(uri: string, albumName?: string): Promise<{
|
|
414
420
|
success: boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -405,10 +405,16 @@ declare class FileStorageController {
|
|
|
405
405
|
added: boolean;
|
|
406
406
|
}>;
|
|
407
407
|
readAsBase64(uriOrKey: string): Promise<Base64Result>;
|
|
408
|
+
/**
|
|
409
|
+
* 解析 URI,返回实际的 file:// 路径
|
|
410
|
+
* @param uri 支持 file://、local:// 或其他映射键
|
|
411
|
+
*/
|
|
412
|
+
private resolveUri;
|
|
408
413
|
/**
|
|
409
414
|
* 保存图片/视频到系统相册
|
|
410
|
-
*
|
|
411
|
-
* @param
|
|
415
|
+
* 自动解析 URI:支持 file:// 真实路径、local:// 虚拟映射键、http(s):// 缓存 URL
|
|
416
|
+
* @param uri 文件 URI(支持 file://、local:// 或 http(s):// 路径)
|
|
417
|
+
* @param albumName 相册名称(默认 'Riffle')
|
|
412
418
|
*/
|
|
413
419
|
saveToGallery(uri: string, albumName?: string): Promise<{
|
|
414
420
|
success: boolean;
|
package/dist/index.js
CHANGED
|
@@ -419,14 +419,41 @@ var FileStorageController = class {
|
|
|
419
419
|
async readAsBase64(uriOrKey) {
|
|
420
420
|
return this.core.send("fileStorage", "readAsBase64", { uriOrKey });
|
|
421
421
|
}
|
|
422
|
+
/**
|
|
423
|
+
* 解析 URI,返回实际的 file:// 路径
|
|
424
|
+
* @param uri 支持 file://、local:// 或其他映射键
|
|
425
|
+
*/
|
|
426
|
+
async resolveUri(uri) {
|
|
427
|
+
if (!uri) throw new Error("URI is required");
|
|
428
|
+
if (uri.startsWith("file://")) {
|
|
429
|
+
return uri;
|
|
430
|
+
}
|
|
431
|
+
if (!uri.startsWith("http://") && !uri.startsWith("https://")) {
|
|
432
|
+
const cached = await this.getLocalFileByUrl(uri);
|
|
433
|
+
if (cached?.exists && cached.uri) {
|
|
434
|
+
return cached.uri;
|
|
435
|
+
}
|
|
436
|
+
throw new Error(`File mapping not found: ${uri}`);
|
|
437
|
+
}
|
|
438
|
+
try {
|
|
439
|
+
const cached = await this.getLocalFileByUrl(uri);
|
|
440
|
+
if (cached?.exists && cached.uri) {
|
|
441
|
+
return cached.uri;
|
|
442
|
+
}
|
|
443
|
+
} catch {
|
|
444
|
+
}
|
|
445
|
+
return uri;
|
|
446
|
+
}
|
|
422
447
|
/**
|
|
423
448
|
* 保存图片/视频到系统相册
|
|
424
|
-
*
|
|
425
|
-
* @param
|
|
449
|
+
* 自动解析 URI:支持 file:// 真实路径、local:// 虚拟映射键、http(s):// 缓存 URL
|
|
450
|
+
* @param uri 文件 URI(支持 file://、local:// 或 http(s):// 路径)
|
|
451
|
+
* @param albumName 相册名称(默认 'Riffle')
|
|
426
452
|
*/
|
|
427
453
|
async saveToGallery(uri, albumName = "Riffle") {
|
|
454
|
+
const resolvedUri = await this.resolveUri(uri);
|
|
428
455
|
return this.core.send("fileStorage", "saveToGallery", {
|
|
429
|
-
saveUri:
|
|
456
|
+
saveUri: resolvedUri,
|
|
430
457
|
albumName
|
|
431
458
|
});
|
|
432
459
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -377,14 +377,41 @@ var FileStorageController = class {
|
|
|
377
377
|
async readAsBase64(uriOrKey) {
|
|
378
378
|
return this.core.send("fileStorage", "readAsBase64", { uriOrKey });
|
|
379
379
|
}
|
|
380
|
+
/**
|
|
381
|
+
* 解析 URI,返回实际的 file:// 路径
|
|
382
|
+
* @param uri 支持 file://、local:// 或其他映射键
|
|
383
|
+
*/
|
|
384
|
+
async resolveUri(uri) {
|
|
385
|
+
if (!uri) throw new Error("URI is required");
|
|
386
|
+
if (uri.startsWith("file://")) {
|
|
387
|
+
return uri;
|
|
388
|
+
}
|
|
389
|
+
if (!uri.startsWith("http://") && !uri.startsWith("https://")) {
|
|
390
|
+
const cached = await this.getLocalFileByUrl(uri);
|
|
391
|
+
if (cached?.exists && cached.uri) {
|
|
392
|
+
return cached.uri;
|
|
393
|
+
}
|
|
394
|
+
throw new Error(`File mapping not found: ${uri}`);
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
const cached = await this.getLocalFileByUrl(uri);
|
|
398
|
+
if (cached?.exists && cached.uri) {
|
|
399
|
+
return cached.uri;
|
|
400
|
+
}
|
|
401
|
+
} catch {
|
|
402
|
+
}
|
|
403
|
+
return uri;
|
|
404
|
+
}
|
|
380
405
|
/**
|
|
381
406
|
* 保存图片/视频到系统相册
|
|
382
|
-
*
|
|
383
|
-
* @param
|
|
407
|
+
* 自动解析 URI:支持 file:// 真实路径、local:// 虚拟映射键、http(s):// 缓存 URL
|
|
408
|
+
* @param uri 文件 URI(支持 file://、local:// 或 http(s):// 路径)
|
|
409
|
+
* @param albumName 相册名称(默认 'Riffle')
|
|
384
410
|
*/
|
|
385
411
|
async saveToGallery(uri, albumName = "Riffle") {
|
|
412
|
+
const resolvedUri = await this.resolveUri(uri);
|
|
386
413
|
return this.core.send("fileStorage", "saveToGallery", {
|
|
387
|
-
saveUri:
|
|
414
|
+
saveUri: resolvedUri,
|
|
388
415
|
albumName
|
|
389
416
|
});
|
|
390
417
|
}
|
package/package.json
CHANGED
package/src/controllers/index.ts
CHANGED
|
@@ -302,10 +302,45 @@ export class FileStorageController {
|
|
|
302
302
|
return this.core.send<Base64Result>('fileStorage', 'readAsBase64', { uriOrKey });
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
+
/**
|
|
306
|
+
* 解析 URI,返回实际的 file:// 路径
|
|
307
|
+
* @param uri 支持 file://、local:// 或其他映射键
|
|
308
|
+
*/
|
|
309
|
+
private async resolveUri(uri: string): Promise<string> {
|
|
310
|
+
if (!uri) throw new Error('URI is required');
|
|
311
|
+
|
|
312
|
+
// file:// 协议直接使用
|
|
313
|
+
if (uri.startsWith('file://')) {
|
|
314
|
+
return uri;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// 非 http/https 协议(如 local://),从 MAP 查询
|
|
318
|
+
if (!uri.startsWith('http://') && !uri.startsWith('https://')) {
|
|
319
|
+
const cached = await this.getLocalFileByUrl(uri);
|
|
320
|
+
if (cached?.exists && cached.uri) {
|
|
321
|
+
return cached.uri;
|
|
322
|
+
}
|
|
323
|
+
throw new Error(`File mapping not found: ${uri}`);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// http/https URL,先查缓存
|
|
327
|
+
try {
|
|
328
|
+
const cached = await this.getLocalFileByUrl(uri);
|
|
329
|
+
if (cached?.exists && cached.uri) {
|
|
330
|
+
return cached.uri;
|
|
331
|
+
}
|
|
332
|
+
} catch {
|
|
333
|
+
// 查询失败,继续使用原始 URL
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return uri;
|
|
337
|
+
}
|
|
338
|
+
|
|
305
339
|
/**
|
|
306
340
|
* 保存图片/视频到系统相册
|
|
307
|
-
*
|
|
308
|
-
* @param
|
|
341
|
+
* 自动解析 URI:支持 file:// 真实路径、local:// 虚拟映射键、http(s):// 缓存 URL
|
|
342
|
+
* @param uri 文件 URI(支持 file://、local:// 或 http(s):// 路径)
|
|
343
|
+
* @param albumName 相册名称(默认 'Riffle')
|
|
309
344
|
*/
|
|
310
345
|
async saveToGallery(uri: string, albumName = 'Riffle'): Promise<{
|
|
311
346
|
success: boolean;
|
|
@@ -317,8 +352,11 @@ export class FileStorageController {
|
|
|
317
352
|
height?: number;
|
|
318
353
|
duration?: number;
|
|
319
354
|
}> {
|
|
355
|
+
// 先解析为实际的 file:// 路径
|
|
356
|
+
const resolvedUri = await this.resolveUri(uri);
|
|
357
|
+
|
|
320
358
|
return this.core.send('fileStorage', 'saveToGallery', {
|
|
321
|
-
saveUri:
|
|
359
|
+
saveUri: resolvedUri,
|
|
322
360
|
albumName,
|
|
323
361
|
});
|
|
324
362
|
}
|