parent
e6d20b1d51
commit
3d202d4f32
1 changed files with 58 additions and 72 deletions
@ -1,95 +1,81 @@ |
||||
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 router = express.Router(); |
||||
|
||||
router |
||||
.get('/', (request, response) => { |
||||
console.log('Calling CLI script to get data.'); |
||||
|
||||
const scriptName = 'striker-access-database'; |
||||
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); |
||||
.get('/', getFilesOverview) |
||||
.get('/:fileUUID', getFileDetail) |
||||
.post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => { |
||||
console.log('Receiving shared file.'); |
||||
|
||||
response.status(500).send(); |
||||
} |
||||
if (file) { |
||||
console.log(`file:`); |
||||
console.dir(file); |
||||
|
||||
console.log('Stdout:', childProcess.stdout); |
||||
console.log('Stderr:', childProcess.stderr); |
||||
console.log('body:'); |
||||
console.dir(body); |
||||
|
||||
try { |
||||
stdoutJSON = JSON.parse(childProcess.stdout); |
||||
} catch (jsonParseError) { |
||||
console.warn(`Failed to parse output of script ${scriptName}.`); |
||||
response.status(200).send(); |
||||
} |
||||
|
||||
response.status(200).send(stdoutJSON); |
||||
}) |
||||
.get('/:fileUUID', (request, response) => { |
||||
console.log('Calling CLI script to get data.'); |
||||
|
||||
const scriptName = 'striker-access-database'; |
||||
const childProcess = spawnSync( |
||||
scriptName, |
||||
[ |
||||
'--query', |
||||
`SELECT *
|
||||
FROM files AS fil |
||||
JOIN file_locations AS fil_loc |
||||
ON fil.file_uuid = fil_loc.file_location_file_uuid |
||||
WHERE fil.file_uuid = '${request.params.fileUUID}';`,
|
||||
], |
||||
{ |
||||
timeout: 10000, |
||||
encoding: 'utf-8', |
||||
}, |
||||
); |
||||
let stdoutJSON; |
||||
|
||||
if (childProcess.error) { |
||||
console.log('Error:', childProcess.error); |
||||
|
||||
response.status(500).send(); |
||||
.put('/:fileUUID', (request, response) => { |
||||
console.log('Begin edit single file.'); |
||||
|
||||
const { fileUUID } = request.params; |
||||
const { fileName, fileLocations, fileType } = request.body; |
||||
|
||||
let query = ''; |
||||
|
||||
if (fileName || fileType) { |
||||
query += ` |
||||
UPDATE files |
||||
SET |
||||
${fileName ? `file_name = '${fileName}',` : ''} |
||||
${fileType ? `file_type = '${fileType}',` : ''} |
||||
modified_date = '${accessDB.sub('refresh_timestamp').stdout}' |
||||
WHERE |
||||
file_uuid = '${fileUUID}';`;
|
||||
} |
||||
|
||||
console.log('Stdout:', childProcess.stdout); |
||||
console.log('Stderr:', childProcess.stderr); |
||||
if (fileLocations) { |
||||
fileLocations.forEach(({ fileLocationUUID, isFileLocationActive }) => { |
||||
const fileLocationActive = isFileLocationActive ? 1 : 0; |
||||
|
||||
try { |
||||
stdoutJSON = JSON.parse(childProcess.stdout); |
||||
} catch (jsonParseError) { |
||||
console.warn(`Failed to parse output of script ${scriptName}.`); |
||||
query += ` |
||||
UPDATE file_locations |
||||
SET |
||||
file_location_active = '${fileLocationActive}', |
||||
modified_date = '${accessDB.sub('refresh_timestamp').stdout}' |
||||
WHERE file_location_uuid = '${fileLocationUUID}';`;
|
||||
}); |
||||
} |
||||
|
||||
response.status(200).send(stdoutJSON); |
||||
}) |
||||
.post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => { |
||||
console.log('Receiving shared file.'); |
||||
console.log(`Query (type=[${typeof query}]): [${query}]`); |
||||
|
||||
if (file) { |
||||
console.log(`file:`); |
||||
console.dir(file); |
||||
let queryStdout; |
||||
|
||||
console.log('body:'); |
||||
console.dir(body); |
||||
try { |
||||
({ 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; |
||||
|
Loading…
Reference in new issue