image.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /* istanbul ignore next */
  3. module.exports = function (OssClient) {
  4. function ImageClient(options) {
  5. if (!(this instanceof ImageClient)) {
  6. return new ImageClient(options);
  7. }
  8. if (!options.bucket) {
  9. throw new Error('require bucket for image service instance');
  10. }
  11. if (!options.imageHost) {
  12. throw new Error('require imageHost for image service instance');
  13. }
  14. this.ossClient = new OssClient(options);
  15. this.ossClient.options.imageHost = options.imageHost;
  16. this.ossClient._objectRequestParams = objectRequestParams;
  17. }
  18. /**
  19. * Image operations
  20. */
  21. ImageClient.prototype.get = function* get(name, file, options) {
  22. return yield this.ossClient.get(name, file, options);
  23. };
  24. ImageClient.prototype.getStream = function* getStream(name, options) {
  25. return yield this.ossClient.getStream(name, options);
  26. };
  27. ImageClient.prototype.getExif = function* getExif(name, options) {
  28. var params = this.ossClient._objectRequestParams('GET', name + '@exif', options);
  29. params.successStatuses = [200];
  30. var result = yield this.ossClient.request(params);
  31. result = yield this._parseResponse(result);
  32. return {
  33. res: result.res,
  34. data: result.data
  35. };
  36. };
  37. ImageClient.prototype.getInfo = function* getInfo(name, options) {
  38. var params = this.ossClient._objectRequestParams('GET', name + '@infoexif', options);
  39. params.successStatuses = [200];
  40. var result = yield this.ossClient.request(params);
  41. result = yield this._parseResponse(result);
  42. return {
  43. res: result.res,
  44. data: result.data
  45. };
  46. };
  47. ImageClient.prototype.putStyle = function* putStyle(styleName, style, options) {
  48. var params = this.ossClient._objectRequestParams('PUT', '/?style&styleName=' + styleName, options);
  49. params.successStatuses = [200];
  50. params.content = '<?xml version="1.0" encoding="UTF-8"?>\n' +
  51. '<Style><Content>' + style + '</Content></Style>';
  52. var result = yield this.ossClient.request(params);
  53. result = yield this._parseResponse(result);
  54. return {
  55. res: result.res,
  56. data: result.data
  57. };
  58. };
  59. ImageClient.prototype.getStyle = function* getStyle(styleName, options) {
  60. var params = this.ossClient._objectRequestParams('GET', '/?style&styleName=' + styleName, options);
  61. params.successStatuses = [200];
  62. var result = yield this.ossClient.request(params);
  63. result = yield this._parseResponse(result);
  64. return {
  65. res: result.res,
  66. data: result.data
  67. };
  68. };
  69. ImageClient.prototype.listStyle = function* listStyle(options) {
  70. var params = this.ossClient._objectRequestParams('GET', '/?style', options);
  71. params.successStatuses = [200];
  72. var result = yield this.ossClient.request(params);
  73. result = yield this._parseResponse(result);
  74. return {
  75. res: result.res,
  76. data: result.data.Style
  77. };
  78. };
  79. ImageClient.prototype.deleteStyle = function* deleteStyle(styleName, options) {
  80. var params = this.ossClient._objectRequestParams('DELETE', '/?style&styleName=' + styleName, options);
  81. params.successStatuses = [204];
  82. var result = yield this.ossClient.request(params);
  83. return {
  84. res: result.res,
  85. };
  86. };
  87. ImageClient.prototype.signatureUrl = function signatureUrl(name) {
  88. return this.ossClient.signatureUrl(name, this.ossClient.options.imageHost);
  89. };
  90. ImageClient.prototype._parseResponse = function* _parseResponse(result) {
  91. var str = result.data.toString();
  92. var type = result.res.headers['content-type'];
  93. if (type === 'application/json') {
  94. var data = JSON.parse(str);
  95. result.data = {};
  96. for (var key in data) {
  97. result.data[key] = parseFloat(data[key].value, 10) || data[key].value;
  98. }
  99. } else if (type === 'application/xml') {
  100. result.data = yield this.ossClient.parseXML(str);
  101. }
  102. return result;
  103. };
  104. return ImageClient;
  105. };
  106. /* istanbul ignore next */
  107. function objectRequestParams(method, name, options) {
  108. options = options || {};
  109. name = this._objectName(name);
  110. var authResource = '/' + this.options.bucket + '/' + name;
  111. var params = {
  112. name: name,
  113. method: method,
  114. host: this.options.imageHost,
  115. resource: '/' + name,
  116. timeout: options.timeout,
  117. authResource: authResource,
  118. ctx: options.ctx,
  119. };
  120. if (options.headers) {
  121. params.headers = options.headers;
  122. }
  123. return params;
  124. }