From 97b3fd6cb0f3e48a8c32cf37335848983b5a43bf Mon Sep 17 00:00:00 2001 From: zemotacqy Date: Sat, 26 Oct 2019 09:34:25 +0530 Subject: [PATCH] lost files uploaded --- .gitignore | 4 ++- index.js | 4 +-- models/losts.js | 9 +++++ package.json | 3 +- routes/index.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 models/losts.js diff --git a/.gitignore b/.gitignore index 572406b..9be9576 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /node_modules -package-lock.json \ No newline at end of file +package-lock.json +/uploads +/lostdb \ No newline at end of file diff --git a/index.js b/index.js index 1453d34..dce872b 100644 --- a/index.js +++ b/index.js @@ -34,9 +34,9 @@ app.use(cors()); /** * Routes */ -// const rootRoutes = require('./routes/index.js'); +const rootRoutes = require('./routes/index.js'); -// app.use('/api', rootRoutes); +app.use('/api', rootRoutes); diff --git a/models/losts.js b/models/losts.js new file mode 100644 index 0000000..e454c16 --- /dev/null +++ b/models/losts.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); + +const lostSchema = mongoose.Schema({ + _id : mongoose.Schema.Types.ObjectId, + label: { type: String }, + // images: [String] +}); + +module.exports = mongoose.model('Losts', lostSchema); \ No newline at end of file diff --git a/package.json b/package.json index 80905d0..0210543 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Our team project for Hack.Moscow", "main": "index.js", "scripts": { - "start" : "nodemon index.js" + "start": "nodemon index.js" }, "repository": { "type": "git", @@ -25,6 +25,7 @@ "express": "^4.17.1", "mongoose": "^5.7.7", "morgan": "^1.9.1", + "multer": "^1.4.2", "nodemon": "^1.19.4" } } diff --git a/routes/index.js b/routes/index.js index e69de29..cc3d2d8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -0,0 +1,94 @@ +const express = require("express"); +const router = express.Router(); +const multer = require('multer'); +const path = require('path'); +const fs = require('fs'); +const mongoose = require('mongoose'); +const lost = require('./../models/losts.js'); +const { exec } = require('child_process'); + +const storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, 'uploads/'); + }, + filename: function (req, file, cb) { + cb(null, new Date().toISOString().replace(/:/g, '-') + file.originalname); + } +}); + +const fileFilter = (req, file, cb) => { + // reject a file + if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png' || file.mimetype === 'image/jpg') { + cb(null, true); + } else { + cb(null, false); + } +}; + +const upload = multer({ + storage: storage, + limits: { + fileSize: 1024 * 1024 * 50 + }, + fileFilter: fileFilter +}); + +async function rearrangeFiles(labelname, files) { + fs.mkdirSync(`lostdb/${labelname}`, {recursive: true, mode: 770}); + await files.forEach((file, i) => { + // exec(`mv ${file.path} lostdb/${labelname}/${i}.${path.extname(file.originalname)}`); + fs.renameSync(`${file.path}`, `lostdb/${labelname}/${i}.${path.extname(file.originalname)}`); + }); + + // console.log('stdout:', stdout); + // console.log('stderr:', stderr); + } + + +router.post('/uploadlost', upload.array("lostImage", 10), (req, res, next) => { + if(req.files.size<=0 ) { + res.status(500).json({ + status: "fail" + }); + } else { + const mongooseId = mongoose.Types.ObjectId(); + const labelname = mongooseId; + + const newimage = new lost({ + _id: mongooseId, + label: labelname, + + }); + + + rearrangeFiles(labelname, req.files); + + newimage + .save() + .then((result) => { + console.log(result); + res.status(200).json({ + status: "success" + }); + }).catch(err => { + console.log(err); + res.status(500).json({ + status: "fail" + }); + }); + + /** + * Delete files from Uploads folder after it has been moved to lostdb folder + * req.files.forEach((file, i) => { + fs.unlink(`${file.path}`, (err) => { + if (err) throw err; + console.log(`${file.path} was deleted`); + }); + }); + */ + + } + +}); + +module.exports = router; \ No newline at end of file