258 lines
7.2 KiB
TypeScript
Raw Normal View History

2024-01-05 11:11:15 +08:00
import Want from '@ohos.app.ability.Want'
import promptAction from '@ohos.promptAction'
import fileAccess from '@ohos.file.fileAccess'
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'
import common from '@ohos.app.ability.common'
import fs from '@ohos.file.fs'
2024-05-09 13:42:56 +08:00
2024-05-16 11:05:19 +08:00
const LOGTAG = 'LOGTAG'
2024-12-31 10:21:19 +08:00
export default class FileUtil {
2024-01-05 11:11:15 +08:00
private context: common.UIAbilityContext
private wantInfos: Want[]
private fileAccessHelper: fileAccess.FileAccessHelper
//后续文件路径待替换
2024-05-16 11:05:19 +08:00
private absolutePath = '/mnt/hmdfs/100/account/device_view/local/files'
2024-01-05 11:11:15 +08:00
2024-12-31 10:21:19 +08:00
public destFile: string
2024-01-05 11:11:15 +08:00
constructor(wantInfos) {
const {requestPermission} = this;
this.wantInfos = wantInfos;
requestPermission();
}
/*
2024-01-31 14:42:14 +08:00
* @desc
2024-01-05 11:11:15 +08:00
*
*/
2024-12-31 10:21:19 +08:00
public initFolder = async (folderPath: string) => {
2024-01-05 11:11:15 +08:00
const {absolutePath} = this;
const folderList = folderPath.split('/').filter(folderName => folderName !== '');
let path = absolutePath
2024-12-31 10:21:19 +08:00
folderList.forEach((folderName => {
2024-01-05 11:11:15 +08:00
path += `/${folderName}`;
2024-08-08 20:12:19 +08:00
try {
const isExit = fs.accessSync(path);
2024-12-31 10:21:19 +08:00
if (!isExit) {
2024-08-08 20:12:19 +08:00
fs.mkdirSync(path)
}
} catch (e) {
2024-12-31 10:21:19 +08:00
console.info('初始化文件夹失败', path)
2024-08-08 20:12:19 +08:00
promptAction.showToast({
2024-12-31 10:21:19 +08:00
message: `初始化文件夹失败` + JSON.stringify(e),
duration: 4000,
2024-08-08 20:12:19 +08:00
})
2024-01-05 11:11:15 +08:00
}
2024-12-31 10:21:19 +08:00
2024-01-05 11:11:15 +08:00
}));
return path;
}
/*
* @desc
*
*/
2024-12-31 10:21:19 +08:00
public addFile = async (filePath: string, content: string, type?: string, fd?) => {
const { READ_WRITE,CREATE,APPEND } = fs.OpenMode
2024-07-09 11:11:31 +08:00
const isExit = fs.accessSync(filePath);
//文件存在先删除
2024-12-31 10:21:19 +08:00
if (isExit) {
2024-07-09 11:11:31 +08:00
fs.unlinkSync(filePath);
}
2024-01-05 11:11:15 +08:00
try {
2024-12-31 10:21:19 +08:00
let file
if (!fd) {
file = fs.openSync(filePath, READ_WRITE | CREATE);
}
2024-07-09 11:11:31 +08:00
//追加写入文件
2024-12-31 10:21:19 +08:00
fs.writeSync(file.fd, content)
2024-01-05 11:11:15 +08:00
fs.closeSync(file)
2024-12-31 10:21:19 +08:00
return file.fd
2024-01-05 11:11:15 +08:00
2024-12-31 10:21:19 +08:00
} catch (e) {
console.error(LOGTAG, '写入失败', JSON.stringify(e))
2024-01-05 11:11:15 +08:00
}
}
2024-12-31 10:21:19 +08:00
public openFileSync = async (filePath) => {
const { READ_WRITE,CREATE,APPEND } = fs.OpenMode
let file = fs.openSync(filePath, READ_WRITE | APPEND | CREATE);
return file
}
public editFileWidthOutOpen(file, content) {
const newStr = content + '\n'
fs.writeSync(file.fd, newStr)
fs.closeSync(file)
console.error(LOGTAG, '写入文件成功')
}
2024-01-05 11:11:15 +08:00
/*
* @desc
*
*/
2024-12-31 10:21:19 +08:00
public editFile = async (filePath: string, content: string, fd?: number) => {
const { READ_WRITE,CREATE,APPEND } = fs.OpenMode
2024-05-16 11:05:19 +08:00
try {
2024-12-31 10:21:19 +08:00
const newStr = content + '\n'
if (fd !== undefined) {
fs.writeSync(fd, newStr)
return fd
} else {
let file = fs.openSync(filePath, READ_WRITE | APPEND | CREATE);
fs.writeSync(file.fd, newStr)
return file.fd
}
2024-12-31 10:21:19 +08:00
2024-12-29 21:43:12 +08:00
} catch (e) {
2024-12-31 10:21:19 +08:00
console.error(LOGTAG, JSON.stringify(e))
2024-05-16 11:05:19 +08:00
}
2024-01-05 11:11:15 +08:00
}
/*
* @desc
*
**/
2024-12-31 10:21:19 +08:00
public readFile = async (filePath: string) => {
try {
console.log('strrr', filePath)
2024-05-16 11:05:19 +08:00
const str = await fs.readText(filePath);
2024-01-05 11:11:15 +08:00
return str
2024-12-31 10:21:19 +08:00
} catch (err) {
2024-01-05 11:11:15 +08:00
return ''
}
}
/*
* @desc获取系统目录里的文件列表
*/
2024-12-31 10:21:19 +08:00
public getDeviceList = async (folderPath?: string) => {
2024-01-05 11:11:15 +08:00
const {absolutePath,getFilePathList} = this;
2024-12-31 10:21:19 +08:00
return getFilePathList(`${absolutePath}/${folderPath}`, false)
2024-01-05 11:11:15 +08:00
};
// 删除文件夹或者文件
/*
* @desc
* @param{{type}} 1:文件夹 2 3
**/
2024-12-31 10:21:19 +08:00
public deleteF = async (path: string, type: 1 | 2 | 3) => {
2024-01-05 11:11:15 +08:00
const {getFilePathList,absolutePath} = this
2024-12-31 10:21:19 +08:00
if (type === 1) {
const fileList = await getFilePathList(`${absolutePath}/${path}`, false);
fileList.forEach(filePath => {
2024-01-05 11:11:15 +08:00
fs.unlinkSync(`${absolutePath}/${path}/${filePath}`);
})
fs.rmdirSync(`${absolutePath}/${path}`);
return true
}
2024-12-31 10:21:19 +08:00
if (type === 2) {
2024-01-05 11:11:15 +08:00
fs.unlinkSync(`${absolutePath}/${path}`);
return true
}
2024-12-31 10:21:19 +08:00
if (type === 3) {
2024-01-05 11:11:15 +08:00
fs.unlinkSync(`${path}`);
return true
}
}
// 获取系统文件绝对路径
2024-12-31 10:21:19 +08:00
public getAbsolutePath = () => {
2024-01-05 11:11:15 +08:00
const {absolutePath} = this;
return absolutePath
}
// 检索文件列表
public getSdCardPathList = async () => {
this.wantInfos = await fileAccess.getFileAccessAbilityInfo();
const {wantInfos,context} = this;
2024-12-31 10:21:19 +08:00
const fileAccessHelper = fileAccess.createFileAccessHelper(this.context, this.wantInfos);
2024-01-05 11:11:15 +08:00
this.fileAccessHelper = fileAccessHelper;
let isDone = false;
let rootIterator = null;
try {
rootIterator = await fileAccessHelper.getRoots();
while (!isDone) {
let isDones = false;
let rootInfo = rootIterator.next();
isDone = rootInfo.done; //返回true结束
console.error(LOGTAG, "根目录迭代器对象 next result = " + JSON.stringify(rootInfo));
if (!isDone) {
let deviceType = rootInfo.value.deviceType;
let displayName = rootInfo.value.displayName;
2024-12-31 10:21:19 +08:00
let uri: string = rootInfo.value.uri;
2024-01-05 11:11:15 +08:00
let deviceFlags = rootInfo.value.deviceFlags;
2024-12-31 10:21:19 +08:00
console.error(LOGTAG, `设备类型:${deviceType},设备名称:${displayName}设备根目录Uri${uri},设备支持的能力:${deviceFlags}`);
2024-01-05 11:11:15 +08:00
2024-12-31 10:21:19 +08:00
if (uri.indexOf('/mnt/external/') > 0) {
2024-01-05 11:11:15 +08:00
// if('vol-8-1' ==displayName){
2024-12-31 10:21:19 +08:00
this.destFile = uri.split('ExternalFileManager')[1] + '/'
console.error(LOGTAG, `外置存储路径:` + this.destFile);
this.getFilePathList(this.destFile, true)
2024-01-05 11:11:15 +08:00
}
}
}
} catch (error) {
2024-12-31 10:21:19 +08:00
console.error(LOGTAG, "getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
2024-01-05 11:11:15 +08:00
}
}
2024-12-31 10:21:19 +08:00
public getFileContent = (filePath: string) => {
2024-01-05 11:11:15 +08:00
const {absolutePath} = this;
2024-12-31 10:21:19 +08:00
const { READ_WRITE } = fs.OpenMode
2024-01-05 11:11:15 +08:00
const path = `${absolutePath}/${filePath}`
const str = fs.readTextSync(path);
return str
}
2024-12-31 10:21:19 +08:00
private getFilePathList = async (filePath: string, isSdcard: boolean) => {
let fileName = [], sdCardFileName = [];
2024-01-05 11:11:15 +08:00
try {
const filenames = await fs.listFile(filePath);
for (let i = 0; i < filenames.length; i++) {
2024-12-31 10:21:19 +08:00
console.error(LOGTAG, `目录:${filePath}的子文件:${filenames[i]}`);
if (isSdcard) {
2024-01-05 11:11:15 +08:00
sdCardFileName.push(filenames[i])
2024-12-31 10:21:19 +08:00
} else {
2024-01-05 11:11:15 +08:00
fileName.push(filenames[i])
}
}
return isSdcard ? sdCardFileName : fileName
2024-12-31 10:21:19 +08:00
} catch (e) {
console.error(LOGTAG, JSON.stringify(e));
2024-01-05 11:11:15 +08:00
}
}
// 文件系统初始化
private requestPermission = async () => {
const {context,absolutePath} = this;
let permissions: Array<Permissions> = [
'ohos.permission.READ_MEDIA',
'ohos.permission.WRITE_MEDIA'
];
this.wantInfos = await fileAccess.getFileAccessAbilityInfo();
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager()
2024-12-31 10:21:19 +08:00
atManager.requestPermissionsFromUser(context, permissions, async (code, result) => {
2024-01-05 11:11:15 +08:00
const permissionRequest = result.authResults[0]
2024-12-31 10:21:19 +08:00
if (permissionRequest == -1) {
2024-01-05 11:11:15 +08:00
promptAction.showToast({
message: "请先授权",
2024-12-31 10:21:19 +08:00
duration: 3000,
2024-01-05 11:11:15 +08:00
})
return
}
})
}
}