53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
const { Errors } = require('../../shared')
|
|
const { NodeDescriptorType } = require('../../enum')
|
|
|
|
module.exports = exports = async (message, di) => {
|
|
const new_fd = (node_uuid) => {
|
|
if ( !message.socket.session.last_file_descriptor ) {
|
|
message.socket.session.last_file_descriptor = 0
|
|
message.socket.session.file_descriptors = {}
|
|
}
|
|
|
|
message.socket.session.last_file_descriptor += 1
|
|
message.socket.session.file_descriptors[message.socket.session.last_file_descriptor] = node_uuid
|
|
return message.socket.session.last_file_descriptor
|
|
}
|
|
|
|
const Node = di.models.get('fs:Node')
|
|
const { path, mode } = message.data()
|
|
|
|
if ( !path ) {
|
|
return message.send_response(
|
|
message.fresh().error(Errors.NodeDoesNotExist)
|
|
)
|
|
}
|
|
|
|
if ( path === '/' ) {
|
|
return message.send_response(
|
|
message.fresh.error(Errors.NodeAlreadyExists)
|
|
)
|
|
}
|
|
|
|
const existing_node = await Node.get_path(path, message.socket.session.overlay_name || 'mainline')
|
|
if ( existing_node ) {
|
|
return message.send_response(
|
|
message.fresh().error(Errors.NodeAlreadyExists)
|
|
)
|
|
}
|
|
|
|
const [pied_parent_path, pied_name] = Node.path_parts(path)
|
|
const node = new Node({
|
|
pied_name,
|
|
pied_parent_path,
|
|
overlay_name: message.socket.session.overlay_name || 'mainline',
|
|
mode: 33188, // TODO account for the mode from the client!
|
|
descriptor_type: NodeDescriptorType.File,
|
|
})
|
|
|
|
await node.save()
|
|
|
|
message.send_response(
|
|
message.fresh().data({ node: node.to_api(), descriptor: new_fd(node.uuid) })
|
|
)
|
|
}
|