fix(striker-ui-api): draft edit file endpoint

main
Tsu-ba-me 3 years ago
parent e6d20b1d51
commit 3d202d4f32
  1. 130
      striker-ui-api/routes/files.js

@ -1,95 +1,81 @@
const express = require('express'); const express = require('express');
const { spawnSync } = require('child_process');
const accessDB = require('../lib/accessDB');
const getFilesOverview = require('../lib/request_handlers/files/getFilesOverview');
const getFileDetail = require('../lib/request_handlers/files/getFileDetail');
const uploadSharedFiles = require('../middlewares/uploadSharedFiles'); const uploadSharedFiles = require('../middlewares/uploadSharedFiles');
const router = express.Router(); const router = express.Router();
router router
.get('/', (request, response) => { .get('/', getFilesOverview)
console.log('Calling CLI script to get data.'); .get('/:fileUUID', getFileDetail)
.post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => {
const scriptName = 'striker-access-database'; console.log('Receiving shared file.');
const childProcess = spawnSync(
scriptName,
[
'--query',
'SELECT file_uuid, file_name, file_size, file_type, file_md5sum FROM files;',
],
{
timeout: 10000,
encoding: 'utf-8',
},
);
let stdoutJSON;
if (childProcess.error) {
console.log('Error:', childProcess.error);
response.status(500).send(); if (file) {
} console.log(`file:`);
console.dir(file);
console.log('Stdout:', childProcess.stdout); console.log('body:');
console.log('Stderr:', childProcess.stderr); console.dir(body);
try { response.status(200).send();
stdoutJSON = JSON.parse(childProcess.stdout);
} catch (jsonParseError) {
console.warn(`Failed to parse output of script ${scriptName}.`);
} }
response.status(200).send(stdoutJSON);
}) })
.get('/:fileUUID', (request, response) => { .put('/:fileUUID', (request, response) => {
console.log('Calling CLI script to get data.'); console.log('Begin edit single file.');
const scriptName = 'striker-access-database'; const { fileUUID } = request.params;
const childProcess = spawnSync( const { fileName, fileLocations, fileType } = request.body;
scriptName,
[ let query = '';
'--query',
`SELECT * if (fileName || fileType) {
FROM files AS fil query += `
JOIN file_locations AS fil_loc UPDATE files
ON fil.file_uuid = fil_loc.file_location_file_uuid SET
WHERE fil.file_uuid = '${request.params.fileUUID}';`, ${fileName ? `file_name = '${fileName}',` : ''}
], ${fileType ? `file_type = '${fileType}',` : ''}
{ modified_date = '${accessDB.sub('refresh_timestamp').stdout}'
timeout: 10000, WHERE
encoding: 'utf-8', file_uuid = '${fileUUID}';`;
},
);
let stdoutJSON;
if (childProcess.error) {
console.log('Error:', childProcess.error);
response.status(500).send();
} }
console.log('Stdout:', childProcess.stdout); if (fileLocations) {
console.log('Stderr:', childProcess.stderr); fileLocations.forEach(({ fileLocationUUID, isFileLocationActive }) => {
const fileLocationActive = isFileLocationActive ? 1 : 0;
try {
stdoutJSON = JSON.parse(childProcess.stdout); query += `
} catch (jsonParseError) { UPDATE file_locations
console.warn(`Failed to parse output of script ${scriptName}.`); SET
file_location_active = '${fileLocationActive}',
modified_date = '${accessDB.sub('refresh_timestamp').stdout}'
WHERE file_location_uuid = '${fileLocationUUID}';`;
});
} }
response.status(200).send(stdoutJSON); console.log(`Query (type=[${typeof query}]): [${query}]`);
})
.post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => {
console.log('Receiving shared file.');
if (file) { let queryStdout;
console.log(`file:`);
console.dir(file);
console.log('body:'); try {
console.dir(body); ({ stdout: queryStdout } = accessDB.query(query, 'write'));
} catch (queryError) {
console.log(`Query error: ${queryError}`);
response.status(200).send(); response.status(500).send();
} }
console.log(
`Query stdout (type=[${typeof queryStdout}]): ${JSON.stringify(
queryStdout,
null,
2,
)}`,
);
response.status(200).send(queryStdout);
}); });
module.exports = router; module.exports = router;

Loading…
Cancel
Save