From c4df98c26cde5e7e4da0bf0b72e019a01a2386c7 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Sat, 16 Jul 2022 18:55:43 +0800 Subject: [PATCH] 实现图片文件管理 java 通讯 --- common/Bridge.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 94 insertions(+), 0 deletions(-) diff --git a/common/Bridge.js b/common/Bridge.js index 1f644ea..8514c83 100644 --- a/common/Bridge.js +++ b/common/Bridge.js @@ -24,6 +24,7 @@ import { Fetcher } from '@components/bases/Fetcher'; import { Tools } from '@components/common/Tools'; +import { $fileTrans } from '@components/common/FileTransform'; export class Bridge { @@ -218,6 +219,99 @@ return Fetcher.prototype.transKeyName(type, json); } + /* ----- 文件系统 ----- */ + + /** + * 保存文件 base64 到 java + * @param objUrl + * @param callback + */ + fileSave(objUrl, callback) { + // 分段存储 + const saveFileChunk = (baseData, index) => { + const writeData = { + fileName: baseData.fileName, + currentIdx: index, + totalIdx: baseData.total, + data: baseData.baseArr[index], + }; + this.invoking('img_write', writeData, res => { + if (res.result === false) { + Tools.toast(res.msg); + return; + } + // 按分段递归保存 + if (index < baseData.total - 1) { + setTimeout(() => { + saveFileChunk(baseData, index + 1); + }, 10); + } + // 已完成 + else { + callback && callback('bridge:' + baseData.fileName); + } + }); + }; + $fileTrans.transObjUrlToBaseData(objUrl, baseData => { + saveFileChunk(baseData, 0); + }); + } + + /** + * 从 java 读取文件 base64 + * @param fileName + * @param callback + */ + fileLoad(fileName, callback) { + if (fileName.indexOf('bridge:') < 0) { + callback(''); + return; + } + fileName = fileName.split(':')[1]; + const chunkSize = $fileTrans.getChunkSize(); + const baseArr = []; + let totalSize = 0; + let totalCount = 0; + const loadFileChunk = (index) => { + const loadData = { + fileName, + offset: chunkSize * index, + length: chunkSize, + }; + this.invoking('img_read', loadData, res => { + if (totalSize === 0) { + totalSize = res.totalSize; + totalCount = Math.ceil(res.totalSize / chunkSize); + } + baseArr.push(res.data); + // 按分段递归读取 + if (totalCount > 1 && totalCount - 1 > index) { + loadFileChunk(index + 1); + } + // 读取完成 + else { + const baseData = { + baseArr, + fileName, + }; + $fileTrans.transBaseDataToObjUrl(baseData, objUrl => { + callback && callback(objUrl); + }); + } + }); + }; + loadFileChunk(0); + } + + fileRemove(fileName) { + if (fileName.indexOf('bridge:') < 0) { + callback(''); + return; + } + fileName = fileName.split(':')[1]; + this.invoking('img_del', { fileName }); + } + } // 全局服务实例 -- Gitblit v1.9.1