Ver código fonte

Added album check to uploads

Pitu 6 anos atrás
pai
commit
80117235f9
2 arquivos alterados com 64 adições e 55 exclusões
  1. 4 4
      controllers/albumsController.js
  2. 60 51
      controllers/uploadController.js

+ 4 - 4
controllers/albumsController.js

@@ -75,12 +75,12 @@ albumsController.create = function(req, res, next) {
 		}).then((album) => {
 			if (album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
 
-			db.table('albums').insert({ 
-				name: name, 
+			db.table('albums').insert({
+				name: name,
 				enabled: 1,
 				userid: user[0].id,
 				identifier: randomstring.generate(8),
-				timestamp: Math.floor(Date.now() / 1000) 
+				timestamp: Math.floor(Date.now() / 1000)
 			}).then(() => {
 				return res.json({ success: true })
 			})
@@ -144,7 +144,7 @@ albumsController.get = function(req, res, next) {
 
 		let title = albums[0].name
 		db.table('files').select('name').where('albumid', albums[0].id).orderBy('id', 'DESC').then((files) => {
-			
+
 			let basedomain = req.get('host')
 			for (let domain of config.domains)
 				if (domain.host === req.get('host'))

+ 60 - 51
controllers/uploadController.js

@@ -62,67 +62,76 @@ uploadsController.upload = function(req, res, next) {
 				album = req.params.albumid
 		}
 
-		upload(req, res, function (err) {
-			if (err) {
-				console.error(err)
-				return res.json({ 
+		db.table('albums').where({ id: album, userid: userid }).then((albums) => {
+			if (albums.length === 0) {
+				return res.json({
 					success: false,
-					description: err
+					description: 'Album doesn\'t exist or it doesn\'t belong to the user'
 				})
 			}
 
-			if (req.files.length === 0) return res.json({ success: false, description: 'no-files' })
+			upload(req, res, function (err) {
+				if (err) {
+					console.error(err)
+					return res.json({
+						success: false,
+						description: err
+					})
+				}
 
-			let files = []
-			let existingFiles = []
-			let iteration = 1
+				if (req.files.length === 0) return res.json({ success: false, description: 'no-files' })
 
-			req.files.forEach(function(file) {
+				let files = []
+				let existingFiles = []
+				let iteration = 1
 
-				// Check if the file exists by checking hash and size
-				let hash = crypto.createHash('md5')
-				let stream = fs.createReadStream(path.join(__dirname, '..', config.uploads.folder, file.filename))
+				req.files.forEach(function(file) {
 
-				stream.on('data', function (data) {
-					hash.update(data, 'utf8')
-				})
+					// Check if the file exists by checking hash and size
+					let hash = crypto.createHash('md5')
+					let stream = fs.createReadStream(path.join(__dirname, '..', config.uploads.folder, file.filename))
 
-				stream.on('end', function () {
-					let fileHash = hash.digest('hex')
+					stream.on('data', function (data) {
+						hash.update(data, 'utf8')
+					})
 
-					db.table('files')
-					.where(function() {
-						if (userid === undefined) 
-							this.whereNull('userid')
-						else 
-							this.where('userid', userid)
+					stream.on('end', function () {
+						let fileHash = hash.digest('hex')
+
+						db.table('files')
+						.where(function() {
+							if (userid === undefined)
+								this.whereNull('userid')
+							else
+								this.where('userid', userid)
+						})
+						.where({
+							hash: fileHash,
+							size: file.size
+						}).then((dbfile) => {
+
+							if (dbfile.length !== 0) {
+								uploadsController.deleteFile(file.filename).then(() => {}).catch((e) => console.error(e))
+								existingFiles.push(dbfile[0])
+							} else {
+								files.push({
+									name: file.filename,
+									original: file.originalname,
+									type: file.mimetype,
+									size: file.size,
+									hash: fileHash,
+									ip: req.ip,
+									albumid: album,
+									userid: userid,
+									timestamp: Math.floor(Date.now() / 1000)
+								})
+							}
+
+							if (iteration === req.files.length)
+								return uploadsController.processFilesForDisplay(req, res, files, existingFiles)
+							iteration++
+						}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
 					})
-					.where({
-						hash: fileHash,
-						size: file.size
-					}).then((dbfile) => {
-
-						if (dbfile.length !== 0) {
-							uploadsController.deleteFile(file.filename).then(() => {}).catch((e) => console.error(e))
-							existingFiles.push(dbfile[0])
-						} else {
-							files.push({
-								name: file.filename, 
-								original: file.originalname,
-								type: file.mimetype,
-								size: file.size, 
-								hash: fileHash,
-								ip: req.ip,
-								albumid: album,
-								userid: userid,
-								timestamp: Math.floor(Date.now() / 1000)
-							})
-						}
-
-						if (iteration === req.files.length)
-							return uploadsController.processFilesForDisplay(req, res, files, existingFiles)
-						iteration++
-					}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
 				})
 			})
 		})
@@ -268,7 +277,7 @@ uploadsController.list = function(req, res) {
 					file.date = utils.getPrettyDate(file.date) // file.date.getFullYear() + '-' + (file.date.getMonth() + 1) + '-' + file.date.getDate() + ' ' + (file.date.getHours() < 10 ? '0' : '') + file.date.getHours() + ':' + (file.date.getMinutes() < 10 ? '0' : '') + file.date.getMinutes() + ':' + (file.date.getSeconds() < 10 ? '0' : '') + file.date.getSeconds()
 
 					file.album = ''
-					
+
 					if (file.albumid !== undefined)
 						for (let album of albums)
 							if (file.albumid === album.id)