image.js 4.5 KB

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