Skip to content

fix:修复writeStream和writeFile目录不存在时未自动生成目录并报错问题 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import statvfs from "@ohos.file.statvfs"
import {filePreview} from "@kit.PreviewKit";
import fileUri from "@ohos.file.fileuri";

const FILE_OR_DIR_NOT_EXIST: number = 13900002;

export default class ReactNativeBlobUtilFS {

private context: common.UIAbilityContext | undefined = undefined;
Expand Down Expand Up @@ -141,30 +143,40 @@ export default class ReactNativeBlobUtilFS {
}
}



writeFile(path: string,encoding: string,data: string ,transformFile: boolean, append: boolean):Promise<number> {
return new Promise((resolve,reject) => {
writeFile(path: string, encoding: string, data: string, transformFile: boolean, append: boolean): Promise<number> {
return new Promise((resolve, reject) => {
try {
let accessRes = fs.accessSync(path);
let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
if (append && accessRes) {
file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.APPEND);
}
let writeLen = fs.writeSync(file.fd,data);
if(writeLen==-1){
console.log("write data to file succeed and size is:" + writeLen)
}else{
console.log('success')
let writeLen = fs.writeSync(file.fd, data);
if (writeLen === -1) {
console.log("write data to file succeed and size is:" + writeLen);
} else {
console.log('success');
}
fs.closeSync(file);
resolve(writeLen)
resolve(writeLen);
} catch (err) {
let errMsg = "writeFile failed with error message: " + err.message + ", error code: " + err.code;
console.error(errMsg);
reject(errMsg);
if (err.code === FILE_OR_DIR_NOT_EXIST) {
try {
fs.mkdirSync(path.substring(0, path.lastIndexOf('/')), true);
this.writeFile(path, encoding, data, transformFile, append)
.then(length => resolve(length))
.catch(err => reject(err));
} catch (e) {
let errMsg = "writeFile failed with error message: " + err.message + ", error code: " + err.code;
reject(errMsg);
}
} else {
let errMsg = "writeFile failed with error message: " + err.message + ", error code: " + err.code;
console.error(errMsg);
reject(errMsg);
}
}
})
});
}

writeFileArray(path:string,data:Array<any>,append:boolean):Promise<number> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { buffer } from '@kit.ArkTS';

export type BlobUtilViewDescriptor = Descriptor<"RNCBlobUtil">
export const FAST_BLOB_UTIL = "BlobUtil"
const FILE_OR_DIR_NOT_EXIST: number = 13900002;

export type Encoding = "utf8" | "ascii" | "base64";
export type Stream = {encoding:string,stream:fs.Stream | undefined}

export interface ReactNativeBlobUtilReadStream {
path: string;
encoding: Encoding;
Expand Down Expand Up @@ -38,25 +40,35 @@ export default class ReactNativeBlobUtilStream {
}


async writeStream(filePath: string, encoding: string, append: boolean, callback: (errCode, errMsg, streamId?: string) => void) {
if (!fs.accessSync(filePath)) {
callback("ENOENT", "File '" + filePath + "' does not exist and could not be created");
return
async writeStream(filePath: string, encoding: string, append: boolean,
callback: (errCode, errMsg, streamId?: string) => void) {
let accessRes = fs.accessSync(filePath);
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
if (append && accessRes) {
file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.APPEND);
}
let file = fs.openSync(filePath);
this.encoding = encoding;
try {
let stream = await fs.createStreamSync(filePath, "a+")
let stream = await fs.createStreamSync(filePath, "a+");
let uuid = util.generateRandomUUID(true);
ReactNativeBlobUtilStream.fileStreams.set(uuid, {
stream: stream,
encoding: encoding
})
this.stream = stream
});
this.stream = stream;
fs.closeSync(file);
callback(null, null, uuid)
callback(null, null, uuid);
} catch (err) {
callback("EUNSPECIFIED", "Failed to create write stream at path `" + filePath + "`; " + err.message);
if (err.code === FILE_OR_DIR_NOT_EXIST) {
try {
fs.mkdirSync(filePath.substring(0, filePath.lastIndexOf('/')), true);
await this.writeStream(filePath, encoding, append, callback);
} catch (e) {
callback("EUNSPECIFIED", "Failed to create write stream at path `" + filePath + "`; " + err.code);
}
} else {
callback("EUNSPECIFIED", "Failed to create write stream at path `" + filePath + "`; " + err.code);
}
}
}

Expand Down