diff options
| author | Pitu <[email protected]> | 2017-01-20 03:28:26 -0300 |
|---|---|---|
| committer | Pitu <[email protected]> | 2017-01-20 03:28:26 -0300 |
| commit | bae03cdc25e0d52c4e2ca628d21eed0ca63a5c60 (patch) | |
| tree | b69f45ccdfa6f100df00a0060528dfd31255f0fe /controllers | |
| parent | Album delete WIP (diff) | |
| download | host.fuwn.me-bae03cdc25e0d52c4e2ca628d21eed0ca63a5c60.tar.xz host.fuwn.me-bae03cdc25e0d52c4e2ca628d21eed0ca63a5c60.zip | |
File delete, album delete and album rename. Sugoooi!
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/albumsController.js | 30 | ||||
| -rw-r--r-- | controllers/tokenController.js | 2 | ||||
| -rw-r--r-- | controllers/uploadController.js | 35 |
3 files changed, 59 insertions, 8 deletions
diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 47ab1d5..8b5b5dd 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -35,8 +35,8 @@ albumsController.list = function(req, res, next){ for(let album of albums) album.files = albumsCount[album.id] return res.json({ success: true, albums }) - }) - }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) } albumsController.create = function(req, res, next){ @@ -58,7 +58,7 @@ albumsController.create = function(req, res, next){ }).then(() => { return res.json({ success: true }) }) - }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) } albumsController.delete = function(req, res, next){ @@ -71,8 +71,30 @@ albumsController.delete = function(req, res, next){ db.table('albums').where('id', id).update({ enabled: 0 }).then(() => { return res.json({ success: true }) - }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) } +albumsController.rename = function(req, res, next){ + if(req.headers.auth !== config.adminToken) + return res.status(401).json({ success: false, description: 'not-authorized'}) + + let id = req.body.id + if(id === undefined || id === '') + return res.json({ success: false, description: 'No album specified' }) + + let name = req.body.name + if(name === undefined || name === '') + return res.json({ success: false, description: 'No name specified' }) + + db.table('albums').where('name', name).then((results) => { + if(results.length !== 0) + return res.json({ success: false, description: 'Name already in use' }) + + db.table('albums').where('id', id).update({ name: name }).then(() => { + return res.json({ success: true }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) + +} module.exports = albumsController
\ No newline at end of file diff --git a/controllers/tokenController.js b/controllers/tokenController.js index e302869..ad2b469 100644 --- a/controllers/tokenController.js +++ b/controllers/tokenController.js @@ -54,7 +54,7 @@ tokenController.change = function(req, res, next){ config.adminToken = token res.json({ success: true }) - }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) } module.exports = tokenController
\ No newline at end of file diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 87b3ab6..af9f592 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -4,7 +4,7 @@ const multer = require('multer') const randomstring = require('randomstring') const db = require('knex')(config.database) //const crypto = require('crypto') -//const fs = require('fs') +const fs = require('fs') let uploadsController = {} @@ -94,11 +94,40 @@ uploadsController.upload = function(req, res, next){ }) }) - }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) }) } +uploadsController.delete = function(req, res){ + + if(req.headers.auth !== config.adminToken) + return res.status(401).json({ success: false, description: 'not-authorized'}) + + let id = req.body.id + if(id === undefined || id === '') + return res.json({ success: false, description: 'No file specified' }) + + db.table('files').where('id', id).then((file) => { + + fs.stat('./' + config.uploads.folder + '/' + file[0].name, function (err, stats) { + + if (err) { return res.json({ success: false, description: err.toString() }) } + + fs.unlink('./' + config.uploads.folder + '/' + file[0].name, function(err){ + if (err) { return res.json({ success: false, description: err.toString() }) } + + db.table('files').where('id', id).del().then(() =>{ + return res.json({ success: true }) + }).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.list = function(req, res){ if(req.headers.auth !== config.adminToken) @@ -141,7 +170,7 @@ uploadsController.list = function(req, res){ }) }) - }) + }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) } module.exports = uploadsController
\ No newline at end of file |