Guest User

Untitled

a guest
Jul 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. const express = require('express');
  2. const fs = require('fs');
  3. const extract = require('extract-zip')
  4. const formidable = require('formidable');
  5. const path = require('path');
  6. const uploadDir = path.join(__dirname, '/uploads/');
  7. const extractDir = path.join(__dirname, '/app/');
  8. if (!fs.existsSync(uploadDir)) {
  9. fs.mkdirSync(uploadDir);
  10. }
  11. if (!fs.existsSync(extractDir)) {
  12. fs.mkdirSync(extractDir);
  13. }
  14.  
  15. const server = express();
  16.  
  17. const uploadMedia = (req, res, next) => {
  18. const form = new formidable.IncomingForm();
  19. // file size limit 100MB. change according to your needs
  20. form.maxFileSize = 100 * 1024 * 1024;
  21. form.keepExtensions = true;
  22. form.multiples = true;
  23. form.uploadDir = uploadDir;
  24.  
  25. // collect all form files and fileds and pass to its callback
  26. form.parse(req, (err, fields, files) => {
  27. // when form parsing fails throw error
  28. if (err) return res.status(500).json({ error: err });
  29.  
  30. if (Object.keys(files).length === 0) return res.status(400).json({ message: "no files uploaded" });
  31.  
  32. // Iterate all uploaded files and get their path, extension, final extraction path
  33. const filesInfo = Object.keys(files).map((key) => {
  34. const file = files[key];
  35. const filePath = file.path;
  36. const fileExt = path.extname(file.name);
  37. const fileName = path.basename(file.name, fileExt);
  38.  
  39. return { filePath, fileExt, fileName };
  40. });
  41.  
  42. // Check whether uploaded files are zip files
  43. const validFiles = filesInfo.every(({ fileExt }) => fileExt === '.zip');
  44.  
  45. // if uploaded files are not zip files, return error
  46. if (!validFiles) return res.status(400).json({ message: "unsupported file type" });
  47.  
  48. res.status(200).json({ uploaded: true });
  49.  
  50. // iterate through each file path and extract them
  51. filesInfo.forEach(({ filePath, fileName }) => {
  52. // create directory with timestamp to prevent overwrite same directory names
  53. const destDir = `${path.join(extractDir, fileName)}_${new Date().getTime()}`;
  54.  
  55. extract(filePath, { dir: destDir }, (err) => {
  56. if (!err) {
  57. const childZips = ['app1.zip', 'app2.zip'];
  58. childZips.forEach((childFile) => {
  59. const appPath = path.join(destDir, 'root', childFile);
  60. if (fs.existsSync(appPath)) {
  61. extract(appPath, { dir: path.join(destDir, 'root')}, (err) => {
  62. if (err) console.error(err);
  63. });
  64. }
  65. });
  66. }
  67. });
  68. });
  69. });
  70.  
  71. // runs when new file detected in upload stream
  72. form.on('fileBegin', function (name, file) {
  73. // get the file base name `index.css.zip` => `index.html`
  74. const fileName = path.basename(file.name, path.extname(file.name));
  75. const fileExt = path.extname(file.name);
  76. // create files with timestamp to prevent overwrite same file names
  77. file.path = path.join(uploadDir, `${fileName}_${new Date().getTime()}${fileExt}`);
  78. });
  79. }
  80.  
  81. server.post('/upload', uploadMedia);
  82.  
  83. server.listen(3000, (err) => {
  84. if (err) throw err;
  85. });
Add Comment
Please, Sign In to add comment