Delete and rename handlers

This commit is contained in:
Jiiks 2019-02-28 06:40:33 +02:00
parent 99ec82795c
commit 6030a78b91
3 changed files with 71 additions and 0 deletions

View File

@ -112,6 +112,28 @@ class Comms {
BDIpc.on('bd-getPath', (event, paths) => {
event.reply(path.resolve(this.bd.config.getPath(paths[0]), ...paths.splice(1)));
});
BDIpc.on('bd-rmFile', async (event, paths) => {
const fullPath = path.resolve(this.bd.config.getPath(paths[0]), ...paths.splice(1));
try {
await FileUtils.rm(fullPath);
event.reply('ok');
} catch (err) {
event.reject(err);
}
});
BDIpc.on('bd-rnFile', async (event, paths) => {
const oldPath = path.resolve(this.bd.config.getPath(paths.oldName[0]), ...paths.oldName.splice(1));
const newPath = path.resolve(this.bd.config.getPath(paths.newName[0]), ...paths.newName.splice(1));
try {
await FileUtils.rn(oldPath, newPath);
event.reply('ok');
} catch (err) {
event.reject(err);
}
});
}
async send(channel, message) {

View File

@ -11,6 +11,7 @@
// TODO Use common
import fs from 'fs';
import rimraf from 'rimraf';
import Module from './modulebase';
import BDIpc from './bdipc';
@ -164,6 +165,27 @@ export class FileUtils {
}
}
}
static async rm(path) {
return new Promise((resolve, reject) => {
rimraf(path, err => {
if (err) {
console.log(err);
reject(err);
}
resolve();
});
});
}
static async rn(oldPath, newPath) {
return new Promise((resolve, reject) => {
fs.rename(oldPath, newPath, err => {
if (err) return reject(err);
resolve();
});
});
}
}
export class WindowUtils extends Module {

View File

@ -214,6 +214,33 @@
remote.clipboard.writeText(fullPath);
return;
}
if (action === 'rename') { // TODO select correct file after
this.loading = true;
const { oldName, newName } = item;
try {
await ClientIPC.send('rnFile', { oldName: ['userfiles', oldName], newName: ['userfiles', newName] });
} catch (err) {
console.log(err);
} finally {
this.loading = false;
}
return;
}
if (action === 'delete') {
this.loading = true;
try {
await ClientIPC.send('rmFile', ['userfiles', item.name]);
} catch (err) {
console.log(err);
} finally {
this.loading = false;
}
return;
}
},
toggleLiveUpdate(item) {