dev: move and simplify writeFile trash and delete

This commit is contained in:
KernelDeimos
2024-12-13 13:18:34 -05:00
parent d23c33c377
commit 96a5c3c213
4 changed files with 63 additions and 144 deletions
-144
View File
@@ -104,116 +104,6 @@ module.exports = eggspress('/writeFile', {
});
}
// -----------------------------------------------------------------------//
// mkdir
// -----------------------------------------------------------------------//
else if(req.query.operation && req.query.operation === 'mkdir'){
const {uuid2fsentry, get_user, id2path} = require('../helpers')
// name is required
if(!req.body.name){
return res.status(400).send({
error:{
message: 'Name is required.'
}
})
}
// TODO: [fs:operation:param-coercion]
const source_node = await (new FSNodeParam('uid')).consolidate({
req, getParam: () => req.query.uid
});
// Get user
let user = await get_user({id: await source_node.get('user_id')});
// Create new dir and return
try{
// TODO: [fs:remove-old-methods]
const hl_mkdir = new HLMkdir();
const r = await Context.get().sub({ actor: Actor.adapt(user) }).arun(async () => {
return await hl_mkdir.run({
parent: source_node,
path: req.body.name,
overwrite: false,
dedupe_name: req.body.dedupe_name ?? false,
user,
actor: Context.get('actor'),
});
});
const newdir_node = await req.fs.node(new NodeUIDSelector(r.uid));
return res.send(await sign_file(
await newdir_node.get('entry'), 'write'));
}catch(e){
console.log(e)
return res.status(400).send(e);
}
}
// -----------------------------------------------------------------------//
// Trash
// -----------------------------------------------------------------------//
if(req.query.operation && req.query.operation === 'trash'){
const {validate_fsentry_name, uuid2fsentry, get_user, id2path} = require('../helpers')
const _path = require('path');
const mime = require('mime-types');
// Get fsentry
const fs = req.services.get('filesystem');
// TODO: [fs:move-FSNodeParam]
const node = await (new FSNodeParam('path')).consolidate({
req, getParam: () => req.query.uid
});
// Get user
// TODO: [avoid-database-user-id]
let user = await get_user({id: await node.get('user_id')});
// metadata for trashed file
const new_name = await node.get('uid');
const metadata = {
original_name: await node.get('name'),
original_path: await node.get('path'),
trashed_ts: Math.round(Date.now() / 1000),
};
// Get Trash fsentry
const trash = await fs.node(
new NodePathSelector('/' + user.username + '/Trash')
);
// let trash_path = '/' + user.username + '/Trash';
// let trash = await convert_path_to_fsentry(trash_path);
console.log('what is trash?', trash);
const hl_move = new HLMove();
await Context.get().sub({ actor: Actor.adapt(user) }).arun(async () => {
await hl_move.run({
source: node,
destination_or_parent: trash,
// TODO: [fs:decouple-user]
user,
actor: Context.get('actor'),
new_name: new_name,
new_metadata: metadata,
});
});
// No Trash?
if(!trash){
return res.status(400).send({
error:{
message: 'No Trash directory found.'
}
})
}
return res.status(200).send({
message: 'Item trashed'
})
}
// -----------------------------------------------------------------------//
// Rename
// -----------------------------------------------------------------------//
@@ -338,40 +228,6 @@ module.exports = eggspress('/writeFile', {
return res.send(return_obj);
}
// -----------------------------------------------------------------------//
// Delete
// -----------------------------------------------------------------------//
if(req.query.operation && req.query.operation === 'delete'){
const {get_user, uuid2fsentry, id2path} = require('../helpers')
const _path = require('path');
const mime = require('mime-types');
// TODO: [fs:operation:param-coercion]
const source_node = await (new FSNodeParam('uid')).consolidate({
req, getParam: () => req.query.uid
});
const user = await get_user({id: await source_node.get('user_id')});
// Delete
try{
const hl_remove = new HLRemove();
await Context.get().sub({ actor: Actor.adapt(user) }).arun(async () => {
await hl_remove.run({
target: source_node,
user,
actor: Context.get('actor'),
});
});
}catch(error){
console.log(error)
res.status(400).send(error);
}
// Send success msg
return res.send();
}
// -----------------------------------------------------------------------//
// Write
// -----------------------------------------------------------------------//
@@ -0,0 +1,17 @@
const { HLRemove } = require("../../filesystem/hl_operations/hl_remove");
module.exports = async function writeFile_handle_delete ({
req, res, actor, node,
}) {
// Delete
const hl_remove = new HLRemove();
await hl_remove.run({
target: node,
user: actor.type.user,
actor,
});
// Send success msg
return res.send();
}
@@ -0,0 +1,44 @@
const { HLMove } = require("../../filesystem/hl_operations/hl_move");
const { NodePathSelector } = require("../../filesystem/node/selectors");
module.exports = async function writeFile_handle_trash ({
req, res, actor, node,
}) {
// metadata for trashed file
const new_name = await node.get('uid');
const metadata = {
original_name: await node.get('name'),
original_path: await node.get('path'),
trashed_ts: Math.round(Date.now() / 1000),
};
// Get Trash fsentry
const fs = req.services.get('filesystem');
const trash = await fs.node(
new NodePathSelector('/' + actor.type.user.username + '/Trash')
);
// No Trash?
if(!trash){
return res.status(400).send({
error:{
message: 'No Trash directory found.'
}
})
}
const hl_move = new HLMove();
await hl_move.run({
source: node,
destination_or_parent: trash,
user: actor.type.user,
actor,
new_name: new_name,
new_metadata: metadata,
});
return res.status(200).send({
message: 'Item trashed'
})
};
@@ -2,4 +2,6 @@ module.exports = {
move: require('./move'),
copy: require('./copy'),
mkdir: require('./mkdir'),
trash: require('./trash'),
delete: require('./delete'),
};