aboutsummaryrefslogtreecommitdiff
path: root/controllers/uploadController.js
blob: b39e70260c5e274955119f6749eed65ad54aa949 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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