data-man-url.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var request = Npm.require("request");
  2. /**
  3. * @method DataMan.URL
  4. * @public
  5. * @constructor
  6. * @param {String} url
  7. * @param {String} type The data content (MIME) type.
  8. */
  9. DataMan.URL = function DataManURL(url, type, options) {
  10. var self = this;
  11. options = options || {};
  12. self.url = url;
  13. self._type = type;
  14. // This is some code borrowed from the http package. Hopefully
  15. // we can eventually use HTTP pkg directly instead of 'request'
  16. // once it supports streams and buffers and such. (`request` takes
  17. // and `auth` option, too, but not of the same form as `HTTP`.)
  18. if (options.auth) {
  19. if (options.auth.indexOf(':') < 0)
  20. throw new Error('auth option should be of the form "username:password"');
  21. options.headers = options.headers || {};
  22. options.headers['Authorization'] = "Basic "+
  23. (new Buffer(options.auth, "ascii")).toString("base64");
  24. delete options.auth;
  25. }
  26. self.urlOpts = options;
  27. };
  28. /**
  29. * @method DataMan.URL.prototype.getBuffer
  30. * @private
  31. * @param {function} callback callback(err, buffer)
  32. * @returns {Buffer|undefined}
  33. *
  34. * Passes a Buffer representing the data at the URL to a callback.
  35. */
  36. DataMan.URL.prototype.getBuffer = function dataManUrlGetBuffer(callback) {
  37. var self = this;
  38. request(_.extend({
  39. url: self.url,
  40. method: "GET",
  41. encoding: null,
  42. jar: false
  43. }, self.urlOpts), Meteor.bindEnvironment(function(err, res, body) {
  44. if (err) {
  45. callback(err);
  46. } else {
  47. self._type = res.headers['content-type'];
  48. callback(null, body);
  49. }
  50. }, function(err) {
  51. callback(err);
  52. }));
  53. };
  54. /**
  55. * @method DataMan.URL.prototype.getDataUri
  56. * @private
  57. * @param {function} callback callback(err, dataUri)
  58. *
  59. * Passes a data URI representing the data at the URL to a callback.
  60. */
  61. DataMan.URL.prototype.getDataUri = function dataManUrlGetDataUri(callback) {
  62. var self = this;
  63. self.getBuffer(function (error, buffer) {
  64. if (error) {
  65. callback(error);
  66. } else {
  67. if (!self._type) {
  68. callback(new Error("DataMan.getDataUri couldn't get a contentType"));
  69. } else {
  70. var dataUri = "data:" + self._type + ";base64," + buffer.toString("base64");
  71. callback(null, dataUri);
  72. }
  73. }
  74. });
  75. };
  76. /**
  77. * @method DataMan.URL.prototype.createReadStream
  78. * @private
  79. *
  80. * Returns a read stream for the data.
  81. */
  82. DataMan.URL.prototype.createReadStream = function dataManUrlCreateReadStream() {
  83. var self = this;
  84. // Stream from URL
  85. return request(_.extend({
  86. url: self.url,
  87. method: "GET"
  88. }, self.urlOpts));
  89. };
  90. /**
  91. * @method DataMan.URL.prototype.size
  92. * @param {function} callback callback(err, size)
  93. * @private
  94. *
  95. * Returns the size in bytes of the data at the URL.
  96. */
  97. DataMan.URL.prototype.size = function dataManUrlSize(callback) {
  98. var self = this;
  99. if (typeof self._size === "number") {
  100. callback(null, self._size);
  101. return;
  102. }
  103. self.getBuffer(function (error, buffer) {
  104. if (error) {
  105. callback(error);
  106. } else {
  107. self._size = buffer.length;
  108. callback(null, self._size);
  109. }
  110. });
  111. };
  112. /**
  113. * @method DataMan.URL.prototype.type
  114. * @private
  115. *
  116. * Returns the type of the data.
  117. */
  118. DataMan.URL.prototype.type = function dataManUrlType() {
  119. return this._type;
  120. };