Separate file/directory delete functions

This commit is contained in:
Samuel Elliott 2018-08-22 14:27:06 +01:00
parent 2188c22425
commit 4bed9f726c
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
3 changed files with 50 additions and 31 deletions

View File

@ -37,11 +37,9 @@
</template> </template>
<script> <script>
import fs from 'fs'; import { Utils, FileUtils, ClientIPC } from 'common';
import { Utils } from 'common';
import { remote } from 'electron';
import { E2EE } from 'builtin'; import { E2EE } from 'builtin';
import { DiscordApi, Security, WebpackModules } from 'modules'; import { DiscordApi, WebpackModules } from 'modules';
import { Toasts } from 'ui'; import { Toasts } from 'ui';
import { MiLock, MiImagePlus, MiIcVpnKey } from '../ui/components/common/MaterialIcon'; import { MiLock, MiImagePlus, MiIcVpnKey } from '../ui/components/common/MaterialIcon';
@ -59,12 +57,12 @@
}, },
methods: { methods: {
async showUploadDialog() { async showUploadDialog() {
const dialogResult = remote.dialog.showOpenDialog({ properties: ['openFile'] }); const dialogResult = await ClientIPC.send('bd-native-open', {properties: ['openFile']});
if (!dialogResult) return; if (!dialogResult || !dialogResult.length) return;
const readFile = fs.readFileSync(dialogResult[0]); const readFile = await FileUtils.readFileBuffer(dialogResult[0]);
const FileActions = WebpackModules.getModuleByProps(["makeFile"]); const FileActions = WebpackModules.getModuleByProps(['makeFile']);
const Uploader = WebpackModules.getModuleByProps(["instantBatchUpload"]); const Uploader = WebpackModules.getModuleByProps(['instantBatchUpload']);
const img = await Utils.getImageFromBuffer(readFile); const img = await Utils.getImageFromBuffer(readFile);

View File

@ -266,7 +266,7 @@ export default class {
if (!content) throw {message: `Could not find a ${this.contentType} from ${content}.`}; if (!content) throw {message: `Could not find a ${this.contentType} from ${content}.`};
try { 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) { } catch (err) {
return false; return false;
} }
@ -277,8 +277,7 @@ export default class {
if (!force) if (!force)
await unload; await unload;
await FileUtils.directoryExists(content.paths.contentPath); await FileUtils.recursiveDeleteDirectory(content.paths.contentPath);
FileUtils.deleteDirectory(content.paths.contentPath);
return true; return true;
} catch (err) { } catch (err) {
Logger.err(this.moduleName, err); Logger.err(this.moduleName, err);

View File

@ -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 * @param {String} path The directory's path
* @return {Promise} * @return {Promise}
*/ */
static async deleteDirectory(pathToDir) { static async deleteDirectory(path) {
try { await this.directoryExists(path);
await this.directoryExists(pathToDir);
const files = await this.listDirectory(pathToDir);
for (const file of files) { return new Promise((resolve, reject) => {
const pathToFile = path.join(pathToDir, file); fs.rmdir(path, (err, files) => {
try { if (err) reject(err);
await this.directoryExists(pathToFile); else resolve(files);
await this.deleteDirectory(pathToFile); });
} catch (err) { });
fs.unlinkSync(pathToFile); }
}
/**
* 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);
} }
} }