aboutsummaryrefslogtreecommitdiff
path: root/controllers/uploadController.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2017-08-30 19:35:40 -0300
committerPitu <[email protected]>2017-08-30 19:35:40 -0300
commitad4eeb5eaac4ac90379dd7c44eb2f422a9cff57b (patch)
treec01667942a6300cfc5512f96266eb923c71350aa /controllers/uploadController.js
parentAdded album check to uploads (diff)
downloadhost.fuwn.me-ad4eeb5eaac4ac90379dd7c44eb2f422a9cff57b.tar.xz
host.fuwn.me-ad4eeb5eaac4ac90379dd7c44eb2f422a9cff57b.zip
Fixed last night's fuckup
Diffstat (limited to 'controllers/uploadController.js')
-rw-r--r--controllers/uploadController.js133
1 files changed, 72 insertions, 61 deletions
diff --git a/controllers/uploadController.js b/controllers/uploadController.js
index c634115..08f431f 100644
--- a/controllers/uploadController.js
+++ b/controllers/uploadController.js
@@ -62,82 +62,93 @@ uploadsController.upload = function(req, res, next) {
album = req.params.albumid
}
- db.table('albums').where({ id: album, userid: userid }).then((albums) => {
- if (albums.length === 0) {
- return res.json({
- success: false,
- description: 'Album doesn\'t exist or it doesn\'t belong to the user'
- })
- }
-
- upload(req, res, function (err) {
- if (err) {
- console.error(err)
+ /*
+ A rewrite is due so might as well do awful things here and fix them later :bloblul:
+ */
+
+ if (album !== undefined && userid !== undefined) {
+ // If both values are present, check if the album owner is the user uploading
+ 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'
})
}
+ uploadsController.actuallyUpload(req, res, userid, album);
+ })
+ } else {
+ uploadsController.actuallyUpload(req, res, userid, album);
+ }
+ }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
+}
- if (req.files.length === 0) return res.json({ success: false, description: 'no-files' })
+uploadsController.actuallyUpload = function(req, res, userid, album) {
+ 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)
- })
- .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' }) })
- })
+ 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' }) })
})
})
- }).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
+ })
}
-
uploadsController.processFilesForDisplay = function(req, res, files, existingFiles) {
let basedomain = req.get('host')