You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
398 lines
12 KiB
398 lines
12 KiB
/*
|
|
*V1.0
|
|
*基于5+ IO 封装的基础文件操作库
|
|
*更新日期:2022/06/09
|
|
*/
|
|
|
|
|
|
let downloadTask;
|
|
|
|
|
|
async function readLocal(){
|
|
let fs = await requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
|
|
let mFileEntry = await getDirectory({
|
|
OriginPathEntry: fs.root,
|
|
newFileName: 'OXSyncConfig'
|
|
});
|
|
let mConfigEntry = await mGetFile(mFileEntry,"config.json")
|
|
let mReadResult = await mFile(mConfigEntry)
|
|
console.log("读取结果:",mReadResult);
|
|
return mReadResult
|
|
}
|
|
async function writeLocal(msg){
|
|
let fs = await requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
|
|
let mFileEntry = await getDirectory({
|
|
OriginPathEntry: fs.root,
|
|
newFileName: 'OXSyncConfig'
|
|
});
|
|
let mConfigEntry = await mGetFile(mFileEntry,"config.json")
|
|
// console.log(mConfigEntry.fullPath);
|
|
let mWriteResult = await mCreateWriter(mConfigEntry,msg)
|
|
console.log("写入结果:",mWriteResult);
|
|
return mWriteResult
|
|
}
|
|
|
|
async function writeLocalOld(msg){
|
|
//存储信息到本地,如没有文件,则创建, 有则读取存入
|
|
let fs = await requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
|
|
let mFileEntry = await getDirectory({
|
|
//在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync"
|
|
OriginPathEntry: fs.root,
|
|
newFileName: 'OXSyncConfig'
|
|
});
|
|
// let newNameFileEntry = await resolveLocalFileSystemURL(mFileEntry.fullPath + newName).catch(e => {
|
|
// console.log('目标文件可以操作', e);
|
|
// });
|
|
|
|
let savedFilePathEntry = await resolveLocalFileSystemURL(mFileEntry.fullPath + "config.json"); //获取下载后存储的文件Entry
|
|
|
|
let write_result = writeFile(mFileEntry,"config.json",msg)
|
|
|
|
console.log(9999,JSON.stringify(write_result))
|
|
}
|
|
async function readLocalOld(){
|
|
// 读取本地文件
|
|
console.log("readLocal begin")
|
|
let newName = "oxconfig.txt"
|
|
let fs = await requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
|
|
let mFileEntry = await getDirectory({
|
|
//在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync"
|
|
OriginPathEntry: fs.root,
|
|
newFileName: 'OXSyncConfig'
|
|
});
|
|
let newNameFileEntry = await resolveLocalFileSystemURL(mFileEntry.fullPath + newName).catch(e => {
|
|
console.log('目标文件可以操作', e);
|
|
});
|
|
if (newNameFileEntry) {
|
|
console.log("配置文件已存在准:",newNameFileEntry.fullPath)
|
|
// return newNameFileEntry.fullPath //返回已缓存文件路径
|
|
}
|
|
|
|
// mFileEntry是操作对象DirectoryEntry
|
|
let result = getFile(mFileEntry,"config.json")
|
|
|
|
console.log("result: "+JSON.stringify(result))
|
|
|
|
}
|
|
|
|
//下载数据,当有缓存时直接使用
|
|
//item.url 下载地址
|
|
//item.name 文件名
|
|
//item.forceDownload 当本地存在缓存时,是否强制删除后重新下载
|
|
//item.deleteFile 仅删除文件名为item.name 的文件
|
|
async function adDownloadFileAuto(item) {
|
|
let fs = await download.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
|
|
let url = item.url; //测试url---------
|
|
let newName = item.name;
|
|
let faceSyncEntry = await download.getDirectory({
|
|
//在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync"
|
|
OriginPathEntry: fs.root,
|
|
newFileName: 'ad'
|
|
});
|
|
let newNameFileEntry = await download.resolveLocalFileSystemURL(faceSyncEntry.fullPath + newName).catch(e => {
|
|
console.log('目标文件可以操作', e);
|
|
});
|
|
if (newNameFileEntry) {
|
|
console.log('存在缓存的同名文件:', newNameFileEntry.fullPath);
|
|
|
|
if(item.deleteFile){
|
|
let delResult = await download.removeFile(newNameFileEntry); //删除已存在的文件
|
|
console.log("删除同名文件结果:",delResult)
|
|
return delResult
|
|
}
|
|
if(item.forceDownload){//当本地存在缓存时,是否强制删除后重新下载
|
|
let delResult = await download.removeFile(newNameFileEntry); //删除已存在的文件
|
|
}else{
|
|
return newNameFileEntry.fullPath //返回已缓存文件路径
|
|
}
|
|
|
|
}
|
|
|
|
let onProgresCallBack = function(e) {
|
|
//预留下载进度封装拓展
|
|
// console.log('下载中...', url, e);
|
|
};
|
|
let tempDownloadFilePath = await download.getDownloadFile({ url, onProgresCallBack }); //获取下载文件临时路径
|
|
let savedFilePath = await download.getSaveFile(tempDownloadFilePath); //将文件临时路径长久存储并清除
|
|
// let newName = item.user_id + '.' + (adType); //命名与android约定的文件名: [user_id].jpg------------
|
|
// newName = item.user_id + this.getName(url);
|
|
console.log(tempDownloadFilePath, 123456, savedFilePath.split('.')[1], savedFilePath);
|
|
let savedFilePathEntry = await download.resolveLocalFileSystemURL(savedFilePath); //获取下载后存储的文件Entry
|
|
|
|
let reNamePathEntry = await download.moveFileTo(savedFilePathEntry, faceSyncEntry, newName); //重命名文件到人脸库同步文件夹
|
|
// console.log(666,savedFilePath,newName,reNamePathEntry.fullPath)
|
|
return reNamePathEntry.fullPath;
|
|
}
|
|
function getDownloadFile({
|
|
url,
|
|
onProgresCallBack
|
|
}) {
|
|
return new Promise((rs, rj) => {
|
|
if (downloadTask) downloadTask.abort();
|
|
downloadTask = uni.downloadFile({
|
|
url: url,
|
|
timeout:60000,
|
|
// header:{
|
|
// "content-type":"image/png"
|
|
// },
|
|
success: res => {
|
|
if (res.statusCode == 200) {
|
|
console.log(url+" downloadFile:"+JSON.stringify(res))
|
|
rs(res.tempFilePath)
|
|
} else {
|
|
console.warn('下载失败--->', res);
|
|
console.warn('下载失败链接--->', url);
|
|
rj(res)
|
|
}
|
|
},
|
|
fail: failRes => {
|
|
console.warn('下载失败--->', failRes);
|
|
console.warn('下载失败链接--->', url);
|
|
rj(failRes)
|
|
}
|
|
})
|
|
downloadTask.onProgressUpdate(res => {
|
|
onProgresCallBack && onProgresCallBack(res);
|
|
})
|
|
})
|
|
}
|
|
//从临时路径文件获取持久文件
|
|
function getSaveFile(url) {
|
|
return new Promise((rs, rj) => {
|
|
uni.saveFile({
|
|
tempFilePath: url,
|
|
success: function(res_save) {
|
|
var savedFilePath = res_save.savedFilePath; //相对路径
|
|
// var absFilePath = plus.io.convertLocalFileSystemURL(savedFilePath); //绝对路径
|
|
// console.log('相对路径: ' + savedFilePath);
|
|
// console.log("绝对路径: " + absFilePath);
|
|
rs(savedFilePath)
|
|
// uni.getSavedFileList({
|
|
// success: function (res) {
|
|
// console.log(res.fileList);
|
|
// }
|
|
// });
|
|
},
|
|
fail: function(e) {
|
|
rj(e);
|
|
}
|
|
});
|
|
})
|
|
|
|
}
|
|
//根据本地文件url转换为fileEntry
|
|
function resolveLocalFileSystemURL(FilefullPath) {
|
|
return new Promise((rs, rj) => {
|
|
plus.io.resolveLocalFileSystemURL(FilefullPath,
|
|
function(fs) {
|
|
rs(fs)
|
|
},
|
|
function(err) {
|
|
rj(err)
|
|
});
|
|
})
|
|
}
|
|
// 请求本地文件系统对象Entry
|
|
// type:
|
|
// 应用私有资源目录,对应常量plus.io.PRIVATE_WWW,仅应用自身可读
|
|
// 应用私有文档目录,对应常量plus.io.PRIVATE_DOC,仅应用自身可读写
|
|
// 应用公共文档目录,对应常量plus.io.PUBLIC_DOCUMENTS,多应用时都可读写,常用于保存应用间共享文件
|
|
// 应用公共下载目录,对应常量plus.io.PUBLIC_DOWNLOADS,多应用时都可读写,常用于保存下载文件
|
|
//fs.root 为相应Entry
|
|
function requestFileSystem(type) {
|
|
return new Promise((rs, rj) => {
|
|
plus.io.requestFileSystem(type,
|
|
function(fs) {
|
|
rs(fs)
|
|
},
|
|
function(err) {
|
|
rj(err)
|
|
});
|
|
})
|
|
}
|
|
//移动文件(重命名)
|
|
function moveFileTo(fromPathEntry, toPathEntry, newFileName) {
|
|
return new Promise((rs, rj) => {
|
|
// remove this directory
|
|
// entry.remove( function ( entry ) {
|
|
// plus.console.log( "Remove succeeded" );
|
|
// }, function ( e ) {
|
|
// alert( e.message );
|
|
// } );
|
|
|
|
fromPathEntry.moveTo(toPathEntry, newFileName, function(entry) {
|
|
// console.log("新文件路径: " + entry.fullPath);
|
|
rs(entry)
|
|
}, function(e) {
|
|
console.log(e.message);
|
|
rj(e);
|
|
});
|
|
})
|
|
}
|
|
// 删除文件
|
|
function removeFile(entry){
|
|
// return new Promise(rs=>entry.remove( rs, rs ))
|
|
return new Promise((rs,rj)=>{
|
|
entry.remove( function ( entry ) {
|
|
console.log(entry)
|
|
rs( "Remove succeeded: "+entry.name );
|
|
}, function ( e ) {
|
|
rj( e.message );
|
|
} );
|
|
})
|
|
}
|
|
// 递归删除目录
|
|
function removeFileAll(entry){
|
|
return new Promise(rs=>entry.removeRecursively( rs, rs ))
|
|
}
|
|
//创建,读取文件
|
|
// 废弃
|
|
// function getFile(mFileEntry,path){
|
|
// // entry.getFile( path, flag, succesCB, errorCB );
|
|
// mFileEntry.getFile(path,{create:true}, function(fileEntry){
|
|
// fileEntry.file( function(file){
|
|
// var fileReader = new plus.io.FileReader();
|
|
// console.log("getFile:" + JSON.stringify(file));
|
|
// fileReader.readAsText(file, 'utf-8');
|
|
// fileReader.onloadend = function(evt) {
|
|
// console.log("evt:" + evt);
|
|
// console.log("evt.target" + evt.target);
|
|
// console.log(evt.target.result);
|
|
// rs(evt.target.result)
|
|
// }
|
|
// console.log( ">>>>>>>文件信息:"+file.name + ' : ' + file.size +" Kb <<<<<<<<");
|
|
// } );
|
|
// });
|
|
// }
|
|
// //废弃
|
|
// function writeFile(mFileEntry,path,msg){
|
|
// // Write data to file
|
|
// mFileEntry.getFile(path,{create:true}, function(fileEntry){
|
|
// fileEntry.createWriter( function ( writer ) {
|
|
// writer.onwrite = function ( e ) {
|
|
// console.log( "Write data success!" );
|
|
// rs(true)
|
|
// };
|
|
// // Write data to the end of file.
|
|
// // writer.seek( writer.length );
|
|
// writer.write(JSON.stringify(msg));
|
|
// }, function ( e ) {
|
|
// console.log( e.message );
|
|
// rj(false)
|
|
// } );
|
|
// });
|
|
// }
|
|
//获取file Entry
|
|
function mGetFile(mFileEntry,path){
|
|
return new Promise((rs,rj)=>{
|
|
mFileEntry.getFile(path,{create:true}, function(fileEntry){
|
|
rs(fileEntry)
|
|
});
|
|
})
|
|
}
|
|
//获取操作file
|
|
function mFile(fileEntry){
|
|
return new Promise((rs,rj)=>{
|
|
fileEntry.file( function(file){
|
|
var fileReader = new plus.io.FileReader();
|
|
// console.log("getFile:" + JSON.stringify(file));
|
|
fileReader.readAsText(file, 'utf-8');
|
|
fileReader.onloadend = function(evt) {
|
|
// console.log("evt:" + evt);
|
|
// console.log("evt.target" + evt.target);
|
|
// console.log(evt.target.result);
|
|
rs(evt.target.result)
|
|
}
|
|
console.log( ">>>>>>>文件信息:"+file.name + ' : ' + file.size +" B <<<<<<<<"+ JSON.stringify(file));
|
|
} );
|
|
})
|
|
}
|
|
//读取文件
|
|
function mCreateReader(fileEntry){
|
|
return new Promise((rs,rj)=>{
|
|
fileEntry.createWriter( function ( writer ) {
|
|
writer.onwrite = function ( e ) {
|
|
console.log( "Write data success!" );
|
|
rs("success")
|
|
};
|
|
// Write data to the end of file.
|
|
// writer.seek( writer.length );
|
|
writer.write(JSON.stringify(msg));
|
|
}, function ( e ) {
|
|
console.log( e.message );
|
|
rj(e.message)
|
|
} );
|
|
|
|
})
|
|
}
|
|
//写入文件
|
|
function mCreateWriter(fileEntry,msg){
|
|
return new Promise((rs,rj)=>{
|
|
fileEntry.createWriter( function ( writer ) {
|
|
writer.onwrite = function ( e ) {
|
|
console.log( "Write data success!" );
|
|
rs("success")
|
|
};
|
|
// Write data to the end of file.
|
|
// writer.seek( writer.length );
|
|
writer.write(JSON.stringify(msg));
|
|
}, function ( e ) {
|
|
console.log( e.message );
|
|
rj(e.message)
|
|
} );
|
|
|
|
})
|
|
}
|
|
|
|
// 从父目录下创建或打开子目录
|
|
function getDirectory({
|
|
OriginPathEntry,
|
|
newFileName
|
|
}) {
|
|
return new Promise((rs, rj) => {
|
|
// Retrieve an existing directory, or create it if it does not already exist
|
|
OriginPathEntry.getDirectory(newFileName, {
|
|
create: true,
|
|
exclusive: false
|
|
}, function(dir) {
|
|
// console.log("Directory Entry Name: " + dir.name);
|
|
rs(dir)
|
|
}, function(e) {
|
|
console.log(dir.name,e.message);
|
|
rj(e)
|
|
});
|
|
})
|
|
}
|
|
//获取该DirectoryEntry下所有文件和子目录
|
|
function getFileChildrenList(DirectoryEntry){
|
|
return new Promise((rs, rj) => {
|
|
// 创建读取目录信息对象
|
|
var directoryReader = DirectoryEntry.createReader();
|
|
directoryReader.readEntries( function( entries ){
|
|
var i,res=[];
|
|
for( i=0; i < entries.length; i++ ) {
|
|
// console.log( entries[i].name );
|
|
res.push( entries[i].name)
|
|
}
|
|
rs(res)
|
|
}, function ( e ) {
|
|
console.log( "Read entries failed: " + e.message );
|
|
rj(e.message)
|
|
} );
|
|
|
|
})
|
|
}
|
|
|
|
module.exports = {
|
|
getSaveFile,
|
|
getDownloadFile,
|
|
requestFileSystem,
|
|
resolveLocalFileSystemURL,
|
|
moveFileTo,
|
|
getDirectory,
|
|
removeFile,
|
|
removeFileAll,
|
|
readLocal,
|
|
writeLocal,
|
|
getFileChildrenList
|
|
}
|