utils.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const request = require('request')
  2. const crypto = require('crypto')
  3. /**
  4. *
  5. * @param {string} value
  6. * @param {string[]} criteria
  7. * @returns {boolean}
  8. */
  9. exports.hasMatch = (value, criteria) => {
  10. return criteria.some((i) => {
  11. return value === i || (new RegExp(i)).test(value)
  12. })
  13. }
  14. /**
  15. *
  16. * @param {object} data
  17. * @returns {string}
  18. */
  19. exports.jsonStringify = (data) => {
  20. const cache = []
  21. return JSON.stringify(data, (key, value) => {
  22. if (typeof value === 'object' && value !== null) {
  23. if (cache.indexOf(value) !== -1) {
  24. // Circular reference found, discard key
  25. return
  26. }
  27. cache.push(value)
  28. }
  29. return value
  30. })
  31. }
  32. /**
  33. * Gets the size and content type of a url's content
  34. *
  35. * @param {string} url
  36. * @return {Promise}
  37. */
  38. exports.getURLMeta = (url) => {
  39. return new Promise((resolve, reject) => {
  40. const opts = {
  41. uri: url,
  42. method: 'HEAD',
  43. followAllRedirects: true
  44. }
  45. request(opts, (err, response, body) => {
  46. if (err) {
  47. reject(err)
  48. } else {
  49. resolve({
  50. type: response.headers['content-type'],
  51. size: parseInt(response.headers['content-length'])
  52. })
  53. }
  54. })
  55. })
  56. }
  57. // all paths are assumed to be '/' prepended
  58. /**
  59. * Returns a url builder
  60. *
  61. * @param {object} options uppy options
  62. */
  63. module.exports.getURLBuilder = (options) => {
  64. /**
  65. * Builds uppy targeted url
  66. *
  67. * @param {string} path the tail path of the url
  68. * @param {boolean} isExternal if the url is for the external world
  69. * @param {boolean=} excludeHost if the server domain and protocol should be included
  70. */
  71. const buildURL = (path, isExternal, excludeHost) => {
  72. let url = path
  73. // supports for no path specified too
  74. if (isExternal) {
  75. url = `${options.server.implicitPath || ''}${url}`
  76. }
  77. url = `${options.server.path || ''}${url}`
  78. if (!excludeHost) {
  79. url = `${options.server.protocol}://${options.server.host}${url}`
  80. }
  81. return url
  82. }
  83. return buildURL
  84. }
  85. /**
  86. *
  87. * @param {*} input
  88. * @param {string} secret
  89. */
  90. module.exports.encrypt = (input, secret) => {
  91. const cipher = crypto.createCipher('aes256', secret)
  92. let encrypted = cipher.update(input, 'utf8', 'hex')
  93. encrypted += cipher.final('hex')
  94. return encrypted
  95. }
  96. module.exports.decrypt = (encrypted, secret) => {
  97. var decipher = crypto.createDecipher('aes256', secret)
  98. var decrypted = decipher.update(encrypted, 'hex', 'utf8')
  99. decrypted += decipher.final('utf8')
  100. return decrypted
  101. }