filecache

This commit is contained in:
cansnow
2025-12-27 07:08:30 +08:00
parent 974d149d25
commit 09c7889525
54 changed files with 10485 additions and 164 deletions
+101 -14
View File
@@ -3,6 +3,10 @@
import base from '@/common/config';
//import store from "@/store";
import IMSDK from "openim-uniapp-polyfill";
import md5 from "md5";
import {downloadFile} from "@/uni_modules/network-manage";
const isString = (v)=> {
return typeof v === 'string' || v instanceof String;
},
@@ -160,14 +164,14 @@ const scan = ()=>{
const fileExsit = async(fn)=>{
return await new Promise((resolve) => {
plus.io.resolveLocalFileSystemURL("_doc/"+fn, function(entry) {
plus.io.resolveLocalFileSystemURL(fn, function(entry) {
resolve(true);
}, function() {
resolve(false);
});
})
}
const downloadFile = (url, savepath, successCb, errorCb, progressCb) => {
const downloadFile1 = (url, savepath, successCb, errorCb, progressCb) => {
const root_dir = "_doc/";
if (!url) {
errorCb && errorCb.call(this,new Error('empty url'));
@@ -223,20 +227,100 @@ const downloadFile = (url, savepath, successCb, errorCb, progressCb) => {
startDownload();
});
}
const cacheFile = async (url, savepath, successCb, errorCb, progressCb) => {
//plus.downloader.clear();
//return ;
const coverExists = await fileExsit(savepath);
if(coverExists){
successCb && successCb.call(this,"_doc/"+savepath);
}else{
downloadFile(url, savepath, successCb, errorCb, progressCb)
}
const get_absolute_path = (fn)=>{
return plus.io.convertLocalFileSystemURL(fn);
}
const pendingDownloads = new Map();
const cacheFile = (url, saveDir,progressCallback) => {
let cacheDir = plus.io.convertLocalFileSystemURL(`_doc/{{dir}}/{{key}}.{{ext}}`);
cacheDir = cacheDir.replace('apps/'+plus.runtime.appid+'/doc','cache');
return new Promise(async (resolve, reject) => {
try {
if(!url || !url.startsWith('http')){
resolve(url);
return;
}
const key = md5(url);
var ext = "png"
if(url.toLowerCase().indexOf('.mp4')!==-1){
ext = "mp4";
}
const cacheFilePath = cacheDir.replace('{{dir}}',saveDir)
.replace('{{key}}',key)
.replace('{{ext}}',ext);
//console.error('cacheDir:',cacheDir);
// 如果缓存存在且文件存在,直接返回缓存路径
if (cacheFilePath) {
//console.error('cacheFilePath:', cacheFilePath);
const coverExists = await fileExsit(cacheFilePath);
//console.log("coverExists" ,coverExists);
if (coverExists) {
//console.log("已缓存为:" , cacheFilePath,url);
resolve(cacheFilePath);
return;
}
}
// 2. 检查是否已经有相同的 URL 正在下载
if (pendingDownloads.has(url)) {
// 如果已经有相同的下载在进行,则添加到等待列表
pendingDownloads.get(url).promises.push({ resolve, reject });
return;
}
// 3. 如果没有正在下载,则创建新的下载任务
pendingDownloads.set(url, {
promises: [{ resolve, reject }],
completed: false,
savedPath: saveDir
});
// 4. 开始下载文件
const task = downloadFile({
url:url,
timeout: 30000,
filePath:cacheFilePath,
success(res) {
// 下载成功,解决所有等待的 Promise
const info = pendingDownloads.get(url);
if (info) {
info.completed = true;
info.promises.forEach(p => p.resolve(res.tempFilePath));
pendingDownloads.delete(url);
}
},
fail(e){
// 下载失败,拒绝所有等待的 Promise
const info = pendingDownloads.get(url);
if (info) {
info.promises.forEach(p => p.reject(error));
pendingDownloads.delete(url);
}
},
complate(){
task = null;
}
});
//console.error('task:', task);
task.onProgressUpdate(({progress,totalBytesWritten,totalBytesExpectedToWrite})=>{
//console.error('pres:', progress,totalBytesWritten,totalBytesExpectedToWrite);
progressCallback && progressCallback.call(this,progress,totalBytesWritten,totalBytesExpectedToWrite)
});
} catch (error) {
console.error('cacheFile 发生错误:', error);
resolve(url); // 返回原始URL作为fallback
}
});
};
export default{
fileExsit,
downloadFile,
cacheFile,
fileExsit,
get_absolute_path,
downloadFile,
isString :isString,
isNumber :isNumber,
isInteger :isInteger,
@@ -258,7 +342,10 @@ export default{
v= v || "";
v = v.replace(/\\/ig,"/").replace('/\/\/ig',"/");
//console.log(v);
if(isString(v)){
if(v && isString(v)){
if(v.startsWith('//')){
return 'http:'+v;
}
if(v.startsWith('blob:') || v.startsWith('file://') || v.startsWith('http')){
return v;
}