179 lines
5.2 KiB
Plaintext
179 lines
5.2 KiB
Plaintext
import common from "@ohos.app.ability.common"
|
|
import Want from "@ohos.app.ability.Want"
|
|
import fileAccess from "@ohos.file.fileAccess"
|
|
import fs from '@ohos.file.fs'
|
|
import abilityAccessCtrl, { Permissions } from "@ohos.abilityAccessCtrl"
|
|
import promptAction from "@ohos.promptAction"
|
|
import { FileTag } from "../config"
|
|
|
|
const LOG_TAG = '[file utils]'
|
|
|
|
|
|
export default class FileUtils {
|
|
public destFile: string
|
|
public filePathFdObj: Record<string, fs.File> = {}
|
|
private context: common.UIAbilityContext
|
|
private absolutePath = '/mnt/hmdfs/100/account/device_view/local/files/duolun'
|
|
|
|
constructor(context: common.UIAbilityContext) {
|
|
this.context = context
|
|
this.requestPermission();
|
|
fs.mkdir(this.absolutePath)
|
|
}
|
|
|
|
async initFolder(folderPath: string): Promise<string> {
|
|
const folderList = folderPath.split('/').filter(folderName => folderName !== '');
|
|
let path = this.absolutePath
|
|
folderList.forEach(folderName => {
|
|
path += `/${folderName}`;
|
|
try {
|
|
const isExit = fs.accessSync(path);
|
|
if (!isExit) {
|
|
fs.mkdirSync(path)
|
|
}
|
|
} catch (e) {
|
|
console.info('初始化文件夹失败', path)
|
|
promptAction.showToast({
|
|
message: `初始化文件夹失败` + folderPath + JSON.stringify(e),
|
|
duration: 4000,
|
|
})
|
|
}
|
|
})
|
|
return path
|
|
}
|
|
|
|
async addFile(filePath: string, content: string) {
|
|
try {
|
|
//文件存在先删除
|
|
if (fs.accessSync(filePath)) {
|
|
fs.unlinkSync(filePath);
|
|
}
|
|
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
|
|
//追加写入文件
|
|
fs.writeSync(file.fd, content)
|
|
fs.closeSync(file)
|
|
console.error(LOG_TAG, '写入文件成功')
|
|
return true
|
|
} catch (e) {
|
|
promptAction.showToast({
|
|
message: `addFile文件失败` + filePath + JSON.stringify(e),
|
|
duration: 4000,
|
|
})
|
|
console.error(LOG_TAG, '写入失败', JSON.stringify(e))
|
|
}
|
|
}
|
|
|
|
async editFile(filePath: string, content: string, fd?: number) {
|
|
const thisFile: fs.File = this.filePathFdObj[filePath];
|
|
try {
|
|
if (thisFile) {
|
|
fs.writeSync(thisFile.fd, content + '\n')
|
|
return fd;
|
|
} else {
|
|
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.APPEND | fs.OpenMode.CREATE);
|
|
fs.writeSync(file.fd, content + '\n')
|
|
this.filePathFdObj[filePath] = file
|
|
return file.fd
|
|
}
|
|
} catch (e) {
|
|
promptAction.showToast({
|
|
message: `editFile文件失败` + filePath + JSON.stringify(e),
|
|
duration: 4000,
|
|
})
|
|
console.error(LOG_TAG, JSON.stringify(e))
|
|
}
|
|
}
|
|
|
|
async readFile(filePath: string): Promise<string> {
|
|
try {
|
|
const str = await fs.readText(filePath);
|
|
return str
|
|
} catch (e) {
|
|
promptAction.showToast({
|
|
message: `读取文件失败` + filePath + JSON.stringify(e),
|
|
duration: 4000,
|
|
})
|
|
console.log('readFile文件失败' + filePath + JSON.stringify(e))
|
|
return ''
|
|
}
|
|
}
|
|
|
|
async closeFile(filePath: string) {
|
|
const thisFile = this.filePathFdObj[filePath];
|
|
if (thisFile) {
|
|
fs.closeSync(thisFile);
|
|
console.info(FileTag, filePath + '文件关闭成功')
|
|
}
|
|
|
|
}
|
|
|
|
async getDeviceList(folderPath?: string) {
|
|
return this.getFilePathList(`${this.absolutePath}/${folderPath}`, false)
|
|
}
|
|
|
|
async getFilePathList(filePath: string, isSdCard: boolean): Promise<string[]> {
|
|
let fileName: string[] = [];
|
|
let sdCardFileName: string[] = [];
|
|
|
|
try {
|
|
const filenames = await fs.listFile(filePath);
|
|
for (let i = 0; i < filenames.length; i++) {
|
|
console.error(FileTag, `目录:${filePath}的子文件:${filenames[i]}`);
|
|
if (isSdCard) {
|
|
sdCardFileName.push(filenames[i])
|
|
} else {
|
|
fileName.push(filenames[i])
|
|
}
|
|
}
|
|
return isSdCard ? sdCardFileName : fileName
|
|
} catch (e) {
|
|
console.error(FileTag, JSON.stringify(e));
|
|
}
|
|
}
|
|
|
|
async deleteF(path: string, type: 1 | 2 | 3) {
|
|
if (type === 1) {
|
|
const fileList = await this.getFilePathList(`${this.absolutePath}/${path}`, false);
|
|
fileList.forEach((fileName) => {
|
|
fs.unlinkSync(`${this.absolutePath}/${path}/${fileName}`)
|
|
})
|
|
fs.rmdirSync(`${this.absolutePath}/${path}`);
|
|
return true
|
|
} else if (type === 2) {
|
|
fs.unlinkSync(`${this.absolutePath}/${path}`);
|
|
return true
|
|
} else {
|
|
fs.unlinkSync(`${path}`);
|
|
return true
|
|
}
|
|
}
|
|
|
|
getAbsolutePath(): string {
|
|
return this.absolutePath
|
|
}
|
|
|
|
getFileContent(filePath: string) {
|
|
const path = `${this.absolutePath}/${filePath}`
|
|
const str = fs.readTextSync(path);
|
|
return str
|
|
}
|
|
|
|
|
|
private async requestPermission() {
|
|
let permissions: Array<Permissions> = [
|
|
'ohos.permission.READ_MEDIA',
|
|
'ohos.permission.WRITE_MEDIA'
|
|
];
|
|
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager()
|
|
atManager.requestPermissionsFromUser(this.context, permissions, async (code, result) => {
|
|
const permissionRequest = result.authResults[0]
|
|
if (permissionRequest == -1) {
|
|
promptAction.showToast({
|
|
message: "请先授权",
|
|
duration: 3000,
|
|
})
|
|
return
|
|
}
|
|
})
|
|
}
|
|
} |