mirror of
https://github.com/HeyPuter/puter.git
synced 2025-02-03 07:48:46 +08:00
dev: rewrite and move writeFile rename
This commit is contained in:
parent
298609c6e9
commit
6b6c295a87
@ -104,131 +104,6 @@ module.exports = eggspress('/writeFile', {
|
||||
});
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------//
|
||||
// Rename
|
||||
// -----------------------------------------------------------------------//
|
||||
if(req.query.operation && req.query.operation === 'rename'){
|
||||
const {validate_fsentry_name, uuid2fsentry, get_app, id2path} = require('../helpers')
|
||||
const _path = require('path');
|
||||
const mime = require('mime-types');
|
||||
|
||||
// new_name validation
|
||||
try{
|
||||
validate_fsentry_name(req.body.new_name)
|
||||
}catch(e){
|
||||
return res.status(400).send({
|
||||
error:{
|
||||
message: e.message
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get fsentry
|
||||
let fsentry = await uuid2fsentry(req.query.uid);
|
||||
|
||||
// Not found
|
||||
if(fsentry === false){
|
||||
return res.status(400).send({
|
||||
error:{
|
||||
message: 'No entry found with this uid'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Immutable?
|
||||
if(fsentry.immutable){
|
||||
return res.status(400).send({
|
||||
error:{
|
||||
message: 'Immutable: cannot rename.'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let res1;
|
||||
|
||||
// parent is root
|
||||
if(fsentry.parent_uid === null){
|
||||
try{
|
||||
res1 = await db.read(
|
||||
`SELECT uuid FROM fsentries WHERE parent_uid IS NULL AND name = ? AND id != ? LIMIT 1`,
|
||||
[
|
||||
//name
|
||||
req.body.new_name,
|
||||
fsentry.id,
|
||||
]);
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
// parent is regular dir
|
||||
else{
|
||||
res1 = await db.read(
|
||||
`SELECT uuid FROM fsentries WHERE parent_uid = ? AND name = ? AND id != ? LIMIT 1`,
|
||||
[
|
||||
//parent_uid
|
||||
fsentry.parent_uid,
|
||||
//name
|
||||
req.body.new_name,
|
||||
fsentry.id,
|
||||
]);
|
||||
}
|
||||
if(res1[0]){
|
||||
return res.status(400).send({
|
||||
error:{
|
||||
message: 'An entry with the same name exists under target path.'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// old path
|
||||
const old_path = await id2path(fsentry.id);
|
||||
|
||||
// update `name`
|
||||
await db.write(
|
||||
`UPDATE fsentries SET name = ? WHERE id = ?`,
|
||||
[req.body.new_name, fsentry.id]
|
||||
)
|
||||
|
||||
// new path
|
||||
const new_path = _path.join(_path.dirname(old_path), req.body.new_name);
|
||||
|
||||
// associated_app
|
||||
let associated_app;
|
||||
if(fsentry.associated_app_id){
|
||||
const app = await get_app({id: fsentry.associated_app_id})
|
||||
// remove some privileged information
|
||||
delete app.id;
|
||||
delete app.approved_for_listing;
|
||||
delete app.approved_for_opening_items;
|
||||
delete app.godmode;
|
||||
delete app.owner_user_id;
|
||||
// add to array
|
||||
associated_app = app;
|
||||
}else{
|
||||
associated_app = {};
|
||||
}
|
||||
|
||||
// send the fsentry of the new object created
|
||||
const contentType = mime.contentType(req.body.new_name)
|
||||
const return_obj = {
|
||||
uid: fsentry.uuid,
|
||||
name: req.body.new_name,
|
||||
is_dir: fsentry.is_dir,
|
||||
path: new_path,
|
||||
old_path: old_path,
|
||||
type: contentType ? contentType : null,
|
||||
associated_app: associated_app,
|
||||
original_client_socket_id: req.body.original_client_socket_id,
|
||||
};
|
||||
|
||||
// send realtime success msg to client
|
||||
const user = await get_user({id: fsentry.user_id});
|
||||
const svc_socketio = req.services.get('socketio');
|
||||
svc_socketio.send({ room: user.id }, 'item.renamed', return_obj);
|
||||
|
||||
return res.send(return_obj);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------//
|
||||
// Write
|
||||
// -----------------------------------------------------------------------//
|
||||
|
54
src/backend/src/routers/writeFile/rename.js
Normal file
54
src/backend/src/routers/writeFile/rename.js
Normal file
@ -0,0 +1,54 @@
|
||||
const mime = require('mime-types');
|
||||
const { validate_fsentry_name } = require("../../helpers");
|
||||
const { DB_WRITE } = require('../../services/database/consts');
|
||||
|
||||
module.exports = async function writeFile_handle_rename ({
|
||||
req, res, node,
|
||||
}) {
|
||||
const new_name = req.body.new_name;
|
||||
|
||||
try {
|
||||
validate_fsentry_name(new_name);
|
||||
} catch(e) {
|
||||
return res.status(400).send({
|
||||
error:{
|
||||
message: e.message
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ( await node.get('immutable') ) {
|
||||
return res.status(400).send({
|
||||
error:{
|
||||
message: 'Immutable: cannot rename.'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if ( await node.isUserDirectory() || await node.isRoot ) {
|
||||
return res.status(403).send({
|
||||
error:{
|
||||
message: 'Not allowed to rename this item via writeFile.'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const old_path = await node.get('path');
|
||||
|
||||
const db = req.services.get('database').get(DB_WRITE, 'writeFile:rename');
|
||||
const mysql_id = await node.get('mysql-id');
|
||||
await db.write(
|
||||
`UPDATE fsentries SET name = ? WHERE id = ?`,
|
||||
[new_name, mysql_id]
|
||||
);
|
||||
|
||||
const contentType = mime.contentType(req.body.new_name)
|
||||
const return_obj = {
|
||||
...await node.getSafeEntry(),
|
||||
old_path,
|
||||
type: contentType ? contentType : null,
|
||||
original_client_socket_id: req.body.original_client_socket_id,
|
||||
};
|
||||
|
||||
return res.send(return_obj);
|
||||
}
|
@ -4,4 +4,5 @@ module.exports = {
|
||||
mkdir: require('./mkdir'),
|
||||
trash: require('./trash'),
|
||||
delete: require('./delete'),
|
||||
rename: require('./rename'),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user