v2.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var validator = require('../validator.js');
  2. var DEFAULT_FORMAT = 'png';
  3. var FORMATS = {
  4. gif: 'image/gif',
  5. jpeg: 'image/jpeg',
  6. pdf: 'application/pdf',
  7. png: 'image/png'
  8. };
  9. var VALID_POST_DATA = {
  10. version: 'string',
  11. data: 'string',
  12. format: 'string',
  13. filename: /^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$/g,
  14. width: 'string',
  15. height: 'string',
  16. scale: 'string',
  17. pdf: 'string',
  18. jpeg: 'string'
  19. };
  20. var VALID_PDF_OPTIONS = {
  21. width: 'string',
  22. height: 'string',
  23. border: 'string',
  24. format: 'string',
  25. orientation: 'string'
  26. };
  27. var VALID_JPEG_OPTIONS = {
  28. quality: 'string'
  29. };
  30. function convert(config) {
  31. config = validator.verifyConfig(config, VALID_POST_DATA);
  32. if (config.format in FORMATS) {
  33. config.contentType = FORMATS[config.format];
  34. } else {
  35. config.format = DEFAULT_FORMAT;
  36. config.contentType = FORMATS[DEFAULT_FORMAT];
  37. }
  38. if (config.pdf) {
  39. try {
  40. config.pdf = validator.verifyConfig(JSON.parse(config.pdf), VALID_PDF_OPTIONS);
  41. } catch (e) {
  42. console.error("Parsing 'pdf' config failed.", e);
  43. }
  44. }
  45. if (config.jpeg) {
  46. try {
  47. } catch (e) {
  48. console.error("Parsing 'jpeg' config failed.", e);
  49. }
  50. config.jpeg = validator.verifyConfig(JSON.parse(config.jpeg), VALID_JPEG_OPTIONS);
  51. }
  52. config.width = config.width || 0;
  53. config.height = config.height || 0;
  54. config.scale = config.scale || 1;
  55. return config;
  56. }
  57. module.exports = {
  58. convert: convert
  59. };