app.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var gif = require('./gifmaker');
  2. var util = require('./util');
  3. var reload = require('auto-reload');;
  4. var config = reload('./data/category');
  5. var templates = reload('./data/template');
  6. var express = require('express');
  7. var bodyParser = require('body-parser');
  8. var fs = require('fs');
  9. var app = express();
  10. app.use(express.static('public'));
  11. //app.use(bodyParser());
  12. app.use(bodyParser.urlencoded({ extended: false }));
  13. app.use(bodyParser.json());
  14. app.all('*', function(req, res, next) {
  15. res.header("Access-Control-Allow-Origin", "*");
  16. res.header("Access-Control-Allow-Headers", "X-Requested-With");
  17. res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  18. res.header("X-Powered-By",' 3.2.1');
  19. res.header("Content-Type", "application/json;charset=utf-8");
  20. next();
  21. });
  22. app.get('/', function (req, res) {
  23. res.send('get');
  24. });
  25. app.post('/', function (req, res) {
  26. res.send('post');
  27. });
  28. app.get('/gif/category', function (req, res) {
  29. res.json(config.CATEGRORY);
  30. });
  31. app.get('/gif/template', function (req, res) {
  32. res.json(config.CATEGRORY);
  33. });
  34. app.post('/gif/make', function (req, res) {
  35. var tplid = req.body.tplid;
  36. var content = req.body.content;
  37. var quality = req.body.quality;
  38. var filename = 'cache/' + tplid + '_' + util.sha1(content) + '.gif';
  39. fs.exists('public/' + filename, function (exists) {
  40. if (exists) {
  41. res.json({
  42. gifurl: util.SERVER + filename
  43. })
  44. }
  45. else {
  46. var templObj = templates.templates[parseInt(tplid)];
  47. var sentences = content.split(',');
  48. templObj.template.forEach(function (element, index) {
  49. element.options.text = sentences[index];
  50. });
  51. gif.makewithfilters('../data/template/' + templObj.hash + '.mp4', templObj.template)
  52. .size('100%')
  53. .save('public/' + filename)
  54. .on('end', function () {
  55. res.json({
  56. gifurl: util.SERVER + filename
  57. })
  58. });
  59. }
  60. });
  61. });
  62. var server = app.listen(9091, function () {
  63. var host = server.address().address;
  64. var port = server.address().port;
  65. console.log('Start App, listening at http://%s:%s', host, port);
  66. });