diff options
| author | Pitu <[email protected]> | 2017-01-13 04:34:21 -0300 |
|---|---|---|
| committer | Pitu <[email protected]> | 2017-01-13 04:34:21 -0300 |
| commit | 376cf10663f76d99721ee8596634488868482606 (patch) | |
| tree | 28774ab842a044e571a31b7612311ef1a339be48 /controllers | |
| parent | Initial commit (diff) | |
| download | host.fuwn.me-376cf10663f76d99721ee8596634488868482606.tar.xz host.fuwn.me-376cf10663f76d99721ee8596634488868482606.zip | |
First version
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/uploadController.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/controllers/uploadController.js b/controllers/uploadController.js new file mode 100644 index 0000000..b39e702 --- /dev/null +++ b/controllers/uploadController.js @@ -0,0 +1,49 @@ +const path = require('path') +const config = require('../config.js') +const multer = require('multer') +const randomstring = require('randomstring') +const db = require('knex')(config.database) + +let uploadsController = {} + +const storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, './' + config.uploads.folder + '/') + }, + filename: function (req, file, cb) { + cb(null, randomstring.generate(config.fileLength) + path.extname(file.originalname)) + } +}) + +const upload = multer({ + storage: storage, + limits: { fileSize: config.uploads.maxsize } +}).single('file') + +uploadsController.upload = function(req, res, next){ + + let gallery = req.headers.gallery + + if(!config.privacy.public) + if(!config.privacy.IPs.includes(req.ip)) return res.status(401).send('Not Authorized!') + + upload(req, res, function (err) { + if (err) { + console.error(err) + return res.json({ error: err }) + } + + db.table('files').insert({ + file: req.file.filename, + galleryid: gallery + }).then(() => { + res.json({ + 'filename': req.file.filename + }) + }) + + }) + +} + +module.exports = uploadsController
\ No newline at end of file |