Turn audio into a shareable video. forked from nypublicradio/audiogram

imageAPI.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var fs = require('fs'),
  2. path = require('path'),
  3. multer = require('multer');
  4. var Storage = multer.diskStorage({
  5. destination: function(req, file, callback) {
  6. callback(null, './settings/backgrounds');
  7. },
  8. filename: function(req, file, callback) {
  9. callback(null, file.originalname);
  10. }
  11. });
  12. var upload = multer({
  13. storage: Storage,
  14. fileFilter: function(req, file, cb) {
  15. var filetypes = /jpeg|jpg|png|gif|tiff|webp/;
  16. var mimetype = filetypes.test(file.mimetype);
  17. var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
  18. if (mimetype && extname) {
  19. return cb(null, true);
  20. }
  21. cb('Error: File upload only supports the following image filetypes: ' + filetypes + '.');
  22. }
  23. }).array('img', 1);
  24. var post = function(req, res){
  25. upload(req, res, function(err) {
  26. if(err)
  27. return res.status(500).send(err);
  28. else
  29. return res.status(200).send('');
  30. });
  31. };
  32. var get = function(req, res){
  33. fs.readdir(path.join(__dirname, '..', 'settings', 'backgrounds'), function(err, files){
  34. if(err)
  35. res.status(500).send('An error occured reading the background directory.');
  36. else{
  37. res.setHeader('Content-Type', 'application/json');
  38. res.status(200).send(JSON.stringify(files));
  39. }
  40. });
  41. };
  42. module.exports = {
  43. post: post,
  44. get: get
  45. };