Skip to content

Commit

Permalink
lost files uploaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemotacqy committed Oct 26, 2019
1 parent 5d2bb46 commit 97b3fd6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules
package-lock.json
package-lock.json
/uploads
/lostdb
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);



Expand Down
9 changes: 9 additions & 0 deletions models/losts.js
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -25,6 +25,7 @@
"express": "^4.17.1",
"mongoose": "^5.7.7",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"nodemon": "^1.19.4"
}
}
94 changes: 94 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 97b3fd6

Please sign in to comment.