diff options
Diffstat (limited to 'bucky')
| -rw-r--r-- | bucky/app/api.js | 20 | ||||
| -rw-r--r-- | bucky/app/bucky.js | 30 | ||||
| -rw-r--r-- | bucky/db/index.js | 21 |
3 files changed, 71 insertions, 0 deletions
diff --git a/bucky/app/api.js b/bucky/app/api.js index a8149d0..44c4982 100644 --- a/bucky/app/api.js +++ b/bucky/app/api.js @@ -169,6 +169,26 @@ function route (app){ function(req, res){ res.json({ comment: res.comment }) }) + // move a file to another thread + app.get("/api/file/:id/move/:thread_id", + middleware.ensureAuthenticated, + bucky.checkIsAdmin, + bucky.ensureFile, + bucky.ensureThreadById, + bucky.moveFile, + function(req, res){ + res.json({ file: res.file }) + }) + // move a comment to another thread + app.get("/api/comment/:id/move/:thread_id", + middleware.ensureAuthenticated, + bucky.checkIsAdmin, + bucky.ensureComment, + bucky.ensureThreadById, + bucky.moveComment, + function(req, res){ + res.json({ comment: res.comment }) + }) // delete a comment app.delete("/api/comment/:id", middleware.ensureAuthenticated, diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index 84bbe70..77e1667 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -113,6 +113,21 @@ var bucky = module.exports = { } }) }, + ensureThreadById: function (req, res, next){ + var id = req.params.thread_id.replace(/\D/g, "") + if (! id) { + return res.sendStatus(404) + } + db.getThread(id).then(function(thread){ + if (thread) { + res.thread = thread + next() + } + else { + res.sendStatus(404) + } + }) + }, prepareThread: function (req, res, next){ var thread = res.thread if (thread) { @@ -380,6 +395,14 @@ var bucky = module.exports = { res.sendStatus(500) }) }, + moveComment: function(req, res, next){ + res.comment.set('thread', res.thread.get('id')) + res.comment.save().then(() => { + next() + }).catch(err => { + res.sendStatus(500) + }) + }, destroyComment: function(req, res, next){ res.comment.destroy().then(() => { next() @@ -451,6 +474,13 @@ var bucky = module.exports = { console.log(err) }) }, + moveFile: function(req, res, next){ + db.moveFile(res.file, res.thread.get('id')).then(() => { + next() + }).catch(err => { + res.sendStatus(500) + }) + }, destroyFile: function(req,res,next){ var filePromises = db.destroyFiles([res.file]) Promise.all(filePromises).then( () => next() ) diff --git a/bucky/db/index.js b/bucky/db/index.js index b10e3d8..350f2a4 100644 --- a/bucky/db/index.js +++ b/bucky/db/index.js @@ -152,6 +152,27 @@ db.createFile = function(data){ } db.destroyFile = function(id){ } +db.moveFile = function(file, thread_id){ + var s3client = upload.client() + var srcPath = '/bucky/data/' + file.get('thread') + '/' + file.get('filename') + var destPath = '/bucky/data/' + thread_id + '/' + file.get('filename') + var copyPromise = new Promise((resolve, reject) => { + s3client.copy(srcPath, destPath).on('response', function(res){ + s3client.deleteFile(srcPath, function(err, res){ + if (err) { + return reject(err) + } + file.set('thread', thread_id) + file.save().then(() => { + resolve() + }).catch(err => { + reject(err) + }) + }) + }).end() + }) + return copyPromise +} db.destroyFiles = function(files){ var s3client = upload.client() var rmPromises = files.map((file) => { |
