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

themesAPI.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var fs = require('fs'),
  2. path = require('path');
  3. var themesPath = path.join(__dirname, '..','settings','themes.json');
  4. function getThemes(cb){
  5. fs.readFile(themesPath, 'utf8', function (err, data) {
  6. if (err){
  7. if(err.code == 'ENOENT'){
  8. cb('');
  9. }
  10. }
  11. else{
  12. cb(data);
  13. }
  14. });
  15. }
  16. function writeThemeFile(data, res){
  17. var file = JSON.stringify(data, null, '\t');
  18. fs.writeFile(themesPath, file, function(err){
  19. if(err)
  20. res.status(500).send('There was a problem saving "themes.json".');
  21. else
  22. res.status(200).send('');
  23. });
  24. }
  25. function addTheme(body, res){
  26. function save(data){
  27. var themeFile = JSON.parse(data);
  28. // add the new addition to the JSON and remove the name and themeName attributes
  29. themeFile[body.name] = body;
  30. delete themeFile[body.name].currentName;
  31. delete themeFile[body.name].name;
  32. writeThemeFile(themeFile, res);
  33. }
  34. getThemes(save);
  35. }
  36. function updateTheme(body, res){
  37. function save(data){
  38. var themeFile = JSON.parse(data);
  39. // remove the old theme options
  40. delete themeFile[body.currentName];
  41. // add the new addition to the JSON and remove the name and themeName attributes
  42. themeFile[body.name] = body;
  43. delete themeFile[body.name].currentName;
  44. delete themeFile[body.name].name;
  45. writeThemeFile(themeFile, res);
  46. }
  47. getThemes(save);
  48. }
  49. function deleteTheme(body, res){
  50. function save(data){
  51. var themeFile = JSON.parse(data);
  52. // remove the deleted theme options
  53. delete themeFile[body.name];
  54. writeThemeFile(themeFile, res);
  55. }
  56. getThemes(save);
  57. }
  58. module.exports = function(req, res){
  59. switch (req.body.type) {
  60. case 'ADD':
  61. addTheme(req.body.data, res);
  62. break;
  63. case 'UPDATE':
  64. updateTheme(req.body.data, res);
  65. break;
  66. case 'DELETE':
  67. deleteTheme(req.body.data, res);
  68. }
  69. };