120 lines
3.7 KiB
Plaintext
120 lines
3.7 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"
|
||
|
|
|
||
|
|
const LOG_TAG = '[file utils]'
|
||
|
|
|
||
|
|
|
||
|
|
export default class FileUtils {
|
||
|
|
private context: common.UIAbilityContext
|
||
|
|
private wantInfos: Want[]
|
||
|
|
private fileAccessHelper: fileAccess.FileAccessHelper
|
||
|
|
private absolutePath = '/mnt/hmdfs/100/account/device_view/local/files/duolun'
|
||
|
|
public destFile: string
|
||
|
|
public filePathFdObj: Record<string, fs.File> = {}
|
||
|
|
|
||
|
|
constructor(context: common.UIAbilityContext) {
|
||
|
|
this.context = context
|
||
|
|
this.requestPermission();
|
||
|
|
fs.mkdir(this.absolutePath)
|
||
|
|
}
|
||
|
|
|
||
|
|
private async requestPermission() {
|
||
|
|
let permissions: Array<Permissions> = [
|
||
|
|
'ohos.permission.READ_MEDIA',
|
||
|
|
'ohos.permission.WRITE_MEDIA'
|
||
|
|
];
|
||
|
|
this.wantInfos = await fileAccess.getFileAccessAbilityInfo();
|
||
|
|
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
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
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 ''
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|