Update index.js

main
catvod 2 years ago committed by GitHub
parent 7f3f87b24f
commit df369d4a03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 62
      open/wrapper/index.js

@ -301,6 +301,7 @@ globalThis.JSProxyStream = function () {
this.error = async function (err) {}; this.error = async function (err) {};
}; };
/** /**
* Creates a new JSFile object with the specified path. * Creates a new JSFile object with the specified path.
* *
@ -327,6 +328,12 @@ globalThis.JSFile = function (path) {
this.open = async function (mode) { this.open = async function (mode) {
const file = this; const file = this;
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
if (mode == 'w' || mode == 'a') {
const directoryPath = dirname(file._path);
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
}
fs.open(file._path, mode, null, (e, f) => { fs.open(file._path, mode, null, (e, f) => {
if (!e) file.fd = f; if (!e) file.fd = f;
if (file.fd) resolve(true); if (file.fd) resolve(true);
@ -373,6 +380,13 @@ globalThis.JSFile = function (path) {
}); });
}; };
/**
* Flush buffers to disk.
*/
this.flush = async function () {
return;
};
/** /**
* Closes the file descriptor. * Closes the file descriptor.
* *
@ -391,7 +405,7 @@ globalThis.JSFile = function (path) {
* Moves the file to a new path. * Moves the file to a new path.
* *
* @param {string} newPath - The new path where the file will be moved. * @param {string} newPath - The new path where the file will be moved.
* @return {boolean} Returns true if the file was successfully moved, otherwise returns false. * @return {Promise<boolean>} A promise that resolves with `true` if the file was successfully moved, otherwise returns false.
*/ */
this.move = async function (newPath) { this.move = async function (newPath) {
const file = this; const file = this;
@ -403,6 +417,22 @@ globalThis.JSFile = function (path) {
}); });
}; };
/**
* Copies the file to a new path.
*
* @param {string} newPath - The path of the new location where the file will be copied.
* @return {Promise<boolean>} A promise that resolves with `true` if the file is successfully copied, and `false` otherwise.
*/
this.copy = async function (newPath) {
const file = this;
return await new Promise((resolve, reject) => {
fs.copyFile(file._path, newPath, (err) => {
if (!err) resolve(true);
else resolve(false);
});
});
};
/** /**
* Deletes the file associated with this object. * Deletes the file associated with this object.
* *
@ -415,6 +445,36 @@ globalThis.JSFile = function (path) {
}); });
}); });
}; };
/**
* Checks if the file exists.
*
* @return {Promise<boolean>} A promise that resolves to a boolean value indicating whether the file exists or not.
*/
this.exist = async function () {
const file = this;
return await new Promise((resolve, reject) => {
fs.exists(file._path, (stat) => {
resolve(stat);
});
});
};
/**
* @returns the file length
*/
this.size = async function () {
const file = this;
return await new Promise((resolve, reject) => {
fs.stat(file._path, (err, stat) => {
if (err) {
resolve(0);
} else {
resolve(stat.size);
}
});
});
};
}; };
globalThis.url2Proxy = async function (type, url, headers) { globalThis.url2Proxy = async function (type, url, headers) {

Loading…
Cancel
Save