tweak: add improvements to share/file-by-username

This commit is contained in:
KernelDeimos 2024-06-14 13:23:28 -04:00 committed by Eric Dubé
parent 9ac8535832
commit 0f05a41ea8
2 changed files with 99 additions and 83 deletions

View File

@ -11,16 +11,39 @@ const FSNodeParam = require('../api/filesystem/FSNodeParam');
const { TYPE_DIRECTORY } = require('../filesystem/FSNodeContext');
const { PermissionUtil } = require('../services/auth/PermissionService');
const { validate } = require('uuid');
const configurable_auth = require('../middleware/configurable_auth');
const uuidv4 = require('uuid').v4;
const router = express.Router();
router.use(auth2);
Endpoint({
route: '/file-by-username',
methods: ['POST'],
handler: async (req, res) => {
const validate_share_file_params = req => {
let path = req.body.path;
if ( ! (typeof path === 'string') ) {
throw APIError.create('field_invalid', null, {
key: 'path',
expected: 'string',
got: typeof path,
});
}
const access_level = req.body.access_level || 'write';
if ( ! ['read','write'].includes(access_level) ) {
throw APIError.create('field_invalid', null, {
key: 'access_level',
expected: '"read" or "write"',
});
}
const permission = PermissionUtil.join('fs', path, access_level);
return {
path, access_level, permission,
};
}
const handler_file_by_username = async (req, res) => {
const svc_token = req.services.get('token');
const svc_email = req.services.get('email');
const svc_permission = req.services.get('permission');
@ -39,22 +62,8 @@ Endpoint({
});
}
let path = req.body.path;
if ( ! (typeof path === 'string') ) {
throw APIError.create('field_invalid', null, {
key: 'path',
expected: 'string',
got: typeof path,
});
}
const access_level = req.body.access_level || 'write';
if ( ! ['read','write'].includes(access_level) ) {
throw APIError.create('field_invalid', null, {
key: 'access_level',
expected: '"read" or "write"',
});
}
const { path, permission } =
validate_share_file_params(req);
const recipient = await get_user({ username });
if ( ! recipient ) {
@ -64,9 +73,7 @@ Endpoint({
}
await svc_permission.grant_user_user_permission(
actor, username,
PermissionUtil.join('fs', path, access_level),
{}, {},
actor, username, permission, {}, {},
);
const node = await (new FSNodeParam('path')).consolidate({
@ -77,13 +84,14 @@ Endpoint({
throw APIError.create('subject_does_not_exist');
}
let email_path = path;
if ( await node.get('type') !== TYPE_DIRECTORY ) {
// remove last component
path = path.slice(0, path.lastIndexOf('/')+1);
email_path = email_path.slice(0, path.lastIndexOf('/')+1);
}
if ( path.startsWith('/') ) path = path.slice(1);
const link = `${config.origin}/show/${path}`;
if ( email_path.startsWith('/') ) email_path = email_path.slice(1);
const link = `${config.origin}/show/${email_path}`;
const email_values = {
link,
@ -99,7 +107,15 @@ Endpoint({
await svc_email.send_email({ email: recipient.email }, email_tmpl, email_values);
res.send({});
},
};
Endpoint({
route: '/file-by-username',
mw: [configurable_auth()],
methods: ['POST'],
handler: handler_file_by_username,
}).attach(router);
module.exports = router;
module.exports = app => {
app.use('/share', router);
};

View File

@ -76,7 +76,7 @@ class PuterAPIService extends BaseService {
app.use(require('../routers/healthcheck'))
app.use(require('../routers/test'))
app.use(require('../routers/update-taskbar-items'))
app.use('/share', require('../routers/share'));
require('../routers/share')(app);
require('../routers/whoami')(app);
}