|
|
import downloadImg from "./download.js"
/* *V1.0 *基于5+ IO 封装的基础文件操作库 业务函数 为人脸识别操作提供文件处理功能 *使用范围:(7in,8in)Android 11 Pad *更新日期:2022/06/09 */
/* fileName: 需要查询的目录名 */ async function getFileChildrenList(fileName){ let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
let faceSyncEntry = await downloadImg.getDirectory({ //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync" Success-Import
OriginPathEntry: fs.root, newFileName: fileName }); return downloadImg.getFileChildrenList(faceSyncEntry).then((res)=>{ return res }).catch((e)=>{ console.log("获取文件列表失败:",e) }) } /* fileName: 需要删除的文件名 */ async function deleteAndroidFaceImage(fileName){ console.log("deleteAndroidFaceImage:",fileName) let item = { "deleteFile" : true, "name": fileName } let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
let url = item.url; //测试url---------
let newName = item.name; let faceSyncEntry = await downloadImg.getDirectory({ //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync" Success-Import
OriginPathEntry: fs.root, newFileName: 'Face-Sync' }); let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath + newName).catch(e => { console.log('目标文件可以操作', e); }); if (newNameFileEntry) { console.log('存在缓存的同名文件:', newNameFileEntry.fullPath); if(item.deleteFile){ let delResult = await downloadImg.removeFile(newNameFileEntry); //删除已存在的文件
console.log("删除同名文件结果:",delResult) return delResult } if(item.forceDownload){//当本地存在缓存时,是否强制删除后重新下载
let delResult = await downloadImg.removeFile(newNameFileEntry); //删除已存在的文件
}else{ return newNameFileEntry.fullPath //返回已缓存文件路径
} }else{ console.log("不存在同名文件:"+fileName) } }
/* 直接处理接口数据,下载列表并返回下载结果 并同时处理request_time,检测确保当完成列表下载后,本地再存储request_time */ async function downloadFileList(res){ // await asyncSetTimeOut(3000) //延时,手动断网,模拟下载失败
let clearUNI = await clearUniDownloadFace(); console.log("清空doc结果:",clearUNI) return new Promise(async(rs,rj)=>{ let {list,request_time,total} = res console.log(request_time,total); let downloadSuccessList = [] for (var i=0; i < list.length; i++) { try{ await checkNetworkUsable() console.log("当前第"+(i+1)+"张, 总需下载:"+(list.length)) let local_path = await downloadFileImg(list[i]) downloadSuccessList.push(local_path); list[i]['local_path'] = local_path }catch(e){ //TODO handle the exception
// util.showNone(e)
console.error("下载失败:",e); list[i]['error'] = e continue } } res.list = list checkRequestTime(res) rs(list) }) } /*检测下载完成后列表,全部下载完成时,设置下载时间到本地 */ function checkRequestTime(res){ if(res.list.every(e=>e.local_path)){ setRequestTime(res) } } /*传入downloadFileList 结果,返回plugin要求的成功图片user_id拼接string 如: 15,17 */ function getDataForPlugin(list){ return list.map(e=>{ if(e.local_path)return e.user_id }).join(",") } /*检测当前网络是否可用 */ function checkNetworkUsable(){ return new Promise((rs,rj)=>{ uni.getNetworkType({ success: function (res) { console.log("checkNetworkUsable 当前可用网络:",res.networkType); rs(true) }, fail: function (err) { console.log(err); rj(false) } }); }) } /*存储请求时间到本地 后台约定,人脸更新机制为带时间请求 */ function setRequestTime(res){ if(res.request_time){ uni.setStorageSync("request_time",res.request_time) } } /*返回请求时间戳 */ function getRequestTime(){ return new Promise((rs,rj)=>{ let _time = "" try{ _time = uni.getStorageSync("request_time") }catch(e){ console.warn("请求时间戳异常:",e); } rs(_time) }) }
async function asyncSetTimeOut(time){ const res = await new Promise(resolve => { setTimeout(() => resolve("asyncSetTimeOut"), time||1000); }); return res }
/* //下载文件,转存后,按user_id重命名,并返回最终文件本地路径.
//当有重复文件时,覆盖
item:{ pic_url:"",user_id:"",update_time:"" } */ async function downloadFileImg(item) { let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
let url = item.pic_url; //测试url---------
let onProgresCallBack = function(e) { //预留下载进度封装拓展
console.log('下载中...user_id:'+item.user_id, e); }; let tempDownloadFilePath = await downloadImg.getDownloadFile({ url, onProgresCallBack }); //获取下载文件临时路径
let savedFilePath = await downloadImg.getSaveFile(tempDownloadFilePath); //将文件临时路径长久存储并清除
let newName = item.user_id + '.' + (savedFilePath.split('.')[1] || 'jpg'); //命名与android约定的文件名: [user_id].jpg------------
newName = item.user_id + getName(url); // console.log(tempDownloadFilePath, savedFilePath.split('.')[1], savedFilePath);
let savedFilePathEntry = await downloadImg.resolveLocalFileSystemURL(savedFilePath); //获取下载后存储的文件Entry
let faceSyncEntry = await downloadImg.getDirectory({ //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync"
OriginPathEntry: fs.root, newFileName: 'Face-Sync' }); let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath + newName).catch( e => { console.log('目标文件可以操作', e); }); if (newNameFileEntry) { console.log('删除存在的同名文件:', newNameFileEntry.fullPath); let delResult = await downloadImg.removeFile(newNameFileEntry); //删除已存在的文件
} let reNamePathEntry = await downloadImg.moveFileTo(savedFilePathEntry, faceSyncEntry, newName); //重命名文件到人脸库同步文件夹
console.log("重命名后:",savedFilePath,newName,reNamePathEntry.fullPath) return reNamePathEntry.fullPath; }
function getName(url) { let end = '.'; let urlarr = url.split('.'); let endname = urlarr[urlarr.length]; if (endname != 'jpg' || endname != 'png' || endname != 'jpeg' || endname != 'PNG' || endname != 'JPG' || endname != 'JPEG') endname = 'jpg'; return (end += endname); }
//递归清空本地同步文件目录及子目录
async function clearDownloadFace(fileName){ let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
let faceSyncEntry = await downloadImg.getDirectory({ //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹[fileName]
OriginPathEntry: fs.root, newFileName: fileName }); let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath).catch( e => { console.log('操作目标文件', e); }); await downloadImg.removeFileAll(newNameFileEntry).then(e=>{ console.log(fileName,"删除:",e); }) } //递归清空本地临时下载目录及子目录
async function clearUniDownloadFace(){ let fs = await downloadImg.requestFileSystem(plus.io.PRIVATE_DOC); //获取所要操作根目录File System
// let faceSyncEntry = await downloadImg.getDirectory({
// //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹[fileName]
// OriginPathEntry: fs.root,
// newFileName: fileName
// });
// let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath).catch(
// e => {
// console.log('操作目标文件', e);
// });
await downloadImg.removeFileAll(fs).then(e=>{ console.log("删除uni _Doc缓存文件:",e); }) }
module.exports = { downloadFileImg, clearDownloadFace, downloadFileList, getRequestTime, getDataForPlugin, asyncSetTimeOut, deleteAndroidFaceImage, getFileChildrenList }
|