Separate file/directory delete functions
This commit is contained in:
parent
2188c22425
commit
4bed9f726c
|
@ -37,11 +37,9 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
import fs from 'fs';
|
||||
import { Utils } from 'common';
|
||||
import { remote } from 'electron';
|
||||
import { Utils, FileUtils, ClientIPC } from 'common';
|
||||
import { E2EE } from 'builtin';
|
||||
import { DiscordApi, Security, WebpackModules } from 'modules';
|
||||
import { DiscordApi, WebpackModules } from 'modules';
|
||||
import { Toasts } from 'ui';
|
||||
import { MiLock, MiImagePlus, MiIcVpnKey } from '../ui/components/common/MaterialIcon';
|
||||
|
||||
|
@ -59,12 +57,12 @@
|
|||
},
|
||||
methods: {
|
||||
async showUploadDialog() {
|
||||
const dialogResult = remote.dialog.showOpenDialog({ properties: ['openFile'] });
|
||||
if (!dialogResult) return;
|
||||
const dialogResult = await ClientIPC.send('bd-native-open', {properties: ['openFile']});
|
||||
if (!dialogResult || !dialogResult.length) return;
|
||||
|
||||
const readFile = fs.readFileSync(dialogResult[0]);
|
||||
const FileActions = WebpackModules.getModuleByProps(["makeFile"]);
|
||||
const Uploader = WebpackModules.getModuleByProps(["instantBatchUpload"]);
|
||||
const readFile = await FileUtils.readFileBuffer(dialogResult[0]);
|
||||
const FileActions = WebpackModules.getModuleByProps(['makeFile']);
|
||||
const Uploader = WebpackModules.getModuleByProps(['instantBatchUpload']);
|
||||
|
||||
const img = await Utils.getImageFromBuffer(readFile);
|
||||
|
||||
|
|
|
@ -266,7 +266,7 @@ export default class {
|
|||
if (!content) throw {message: `Could not find a ${this.contentType} from ${content}.`};
|
||||
|
||||
try {
|
||||
await Modals.confirm(`Delete ${this.contentType} ?`, `Are you sure you want to delete ${content.info.name} ?`, 'Delete').promise;
|
||||
await Modals.confirm(`Delete ${this.contentType}?`, `Are you sure you want to delete ${content.info.name} ?`, 'Delete').promise;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
|
@ -277,8 +277,7 @@ export default class {
|
|||
if (!force)
|
||||
await unload;
|
||||
|
||||
await FileUtils.directoryExists(content.paths.contentPath);
|
||||
FileUtils.deleteDirectory(content.paths.contentPath);
|
||||
await FileUtils.recursiveDeleteDirectory(content.paths.contentPath);
|
||||
return true;
|
||||
} catch (err) {
|
||||
Logger.err(this.moduleName, err);
|
||||
|
|
|
@ -496,30 +496,52 @@ export class FileUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Delete a directory
|
||||
* Deletes a file.
|
||||
* @param {String} path The file's path
|
||||
* @return {Promise}
|
||||
*/
|
||||
static async deleteFile(path) {
|
||||
await this.fileExists(path);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.unlink(path, (err, files) => {
|
||||
if (err) reject(err);
|
||||
else resolve(files);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a directory.
|
||||
* @param {String} path The directory's path
|
||||
* @return {Promise}
|
||||
*/
|
||||
static async deleteDirectory(pathToDir) {
|
||||
try {
|
||||
await this.directoryExists(pathToDir);
|
||||
const files = await this.listDirectory(pathToDir);
|
||||
static async deleteDirectory(path) {
|
||||
await this.directoryExists(path);
|
||||
|
||||
for (const file of files) {
|
||||
const pathToFile = path.join(pathToDir, file);
|
||||
try {
|
||||
await this.directoryExists(pathToFile);
|
||||
await this.deleteDirectory(pathToFile);
|
||||
} catch (err) {
|
||||
fs.unlinkSync(pathToFile);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.rmdir(path, (err, files) => {
|
||||
if (err) reject(err);
|
||||
else resolve(files);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a directory and it's contents.
|
||||
* @param {String} path The directory's path
|
||||
* @return {Promise}
|
||||
*/
|
||||
static async recursiveDeleteDirectory(pathToDir) {
|
||||
for (const file of await this.listDirectory(pathToDir)) {
|
||||
const pathToFile = path.join(pathToDir, file);
|
||||
try {
|
||||
await this.recursiveDeleteDirectory(pathToFile);
|
||||
} catch (err) {
|
||||
await this.deleteFile(pathToFile);
|
||||
}
|
||||
|
||||
fs.rmdirSync(pathToDir);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
await this.deleteDirectory(pathToDir);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue