Manual Reference Source

app/modules/utils/uploads.js

  1. const FS = require('fs');
  2. const Errors = require('../exceptions/errors');
  3. const MinioUtils = require('./minio');
  4. const Archiver = require('archiver');
  5. const EntitiesUtils = require('./entities');
  6. const Utils = require('./utils');
  7. const Logger = require('../../logger');
  8.  
  9.  
  10. async function add_single(ctx) {
  11. const file = ctx.request.file;
  12. console.log(file);
  13. await MinioUtils.create_bucket_if_needed(MinioUtils.default_bucket);
  14. await MinioUtils.put_into_bucket(MinioUtils.default_bucket, file);
  15.  
  16. try {
  17. FS.unlinkSync(file.path);
  18. } catch (errfs) {}
  19. ctx.body = { file: file.filename };
  20. }
  21.  
  22. async function download(ctx) {
  23. const entity = ctx.params.entity.trim();
  24. const eid = ctx.params.eid.trim();
  25. const filename = ctx.params.filename.trim();
  26.  
  27. if (entity === '' || eid === '' || filename === '') {
  28. throw Errors.DownloadDoesNotExist;
  29. }
  30.  
  31. const information = await EntitiesUtils.retrieve_and_get_source(entity, eid);
  32. if (!information) {
  33. throw Errors.DownloadDoesNotExist;
  34. }
  35.  
  36. const files = Utils.find_value_with_path(information, 'files'.split('.'));
  37.  
  38. if (!files) {
  39. throw Errors.DownloadDoesNotExist;
  40. }
  41.  
  42. const file = files.find(f => f.url === filename);
  43.  
  44. if (!file) {
  45. Logger.error(`Unable to find file with URI: ${filename}`);
  46. throw Errors.DownloadDoesNotExist;
  47. }
  48.  
  49.  
  50. const stream = await MinioUtils.retrieve_file(MinioUtils.default_bucket, filename);
  51. ctx.set('Content-disposition', `attachment; filename=${file.name}`);
  52. ctx.statusCode = 200;
  53. ctx.body = stream;
  54. }
  55.  
  56. async function multi_download(ctx) {
  57. const body = ctx.params;
  58. const entity = body.entity || '';
  59. const eid = body.eid || '';
  60. const names = body.names ? body.names.split('|') : [];
  61. const filenames = body.filenames ? body.filenames.split('|') : [];
  62.  
  63.  
  64. if (entity === '' || eid === ''
  65. || filenames.length === 0 || names.length === 0
  66. || names.length !== filenames.length) {
  67. throw Errors.DownloadDoesNotExist;
  68. }
  69.  
  70. const information = await EntitiesUtils.retrieve_and_get_source(entity, eid);
  71. if (!information) {
  72. throw Errors.DownloadDoesNotExist;
  73. }
  74.  
  75. const archive = Archiver('zip', {
  76. zlib: { level: 1 }, // Sets the compression level.
  77. });
  78.  
  79. for (const i in filenames) {
  80. const filename = filenames[i];
  81. const name = names[i];
  82. const stream = await MinioUtils.retrieve_file(MinioUtils.default_bucket, filename);
  83. archive.append(stream, { name });
  84. }
  85.  
  86. ctx.set('Content-disposition', 'attachment; filename=pos_download.zip');
  87. ctx.statusCode = 200;
  88. ctx.body = archive;
  89. archive.finalize();
  90. }
  91.  
  92. module.exports = {
  93. add_single,
  94. download,
  95. multi_download,
  96. };