租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.5 KiB

3 months ago
  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<Style><Content>'}${style}</Content></Style>`;
  70. let result = await this.ossClient.request(params);
  71. result = await this._parseResponse(result);
  72. return {
  73. res: result.res,
  74. data: result.data
  75. };
  76. };
  77. ImageClient.prototype.getStyle = async function getStyle(styleName, options) {
  78. const params = this.ossClient._objectRequestParams('GET', `/?style&styleName=${styleName}`, options);
  79. params.successStatuses = [200];
  80. let result = await this.ossClient.request(params);
  81. result = await this._parseResponse(result);
  82. return {
  83. res: result.res,
  84. data: result.data
  85. };
  86. };
  87. ImageClient.prototype.listStyle = async function listStyle(options) {
  88. const params = this.ossClient._objectRequestParams('GET', '/?style', options);
  89. params.successStatuses = [200];
  90. let result = await this.ossClient.request(params);
  91. result = await this._parseResponse(result);
  92. return {
  93. res: result.res,
  94. data: result.data.Style
  95. };
  96. };
  97. ImageClient.prototype.deleteStyle = async function deleteStyle(styleName, options) {
  98. const params = this.ossClient._objectRequestParams('DELETE', `/?style&styleName=${styleName}`, options);
  99. params.successStatuses = [204];
  100. const result = await this.ossClient.request(params);
  101. return {
  102. res: result.res
  103. };
  104. };
  105. ImageClient.prototype.signatureUrl = function signatureUrl(name) {
  106. return this.ossClient.signatureUrl(name);
  107. };
  108. ImageClient.prototype._parseResponse = async function _parseResponse(result) {
  109. const str = result.data.toString();
  110. const type = result.res.headers['content-type'];
  111. if (type === 'application/json') {
  112. const data = JSON.parse(str);
  113. result.data = {};
  114. if (data) {
  115. Object.keys(data).forEach(key => {
  116. result.data[key] = parseFloat(data[key].value, 10) || data[key].value;
  117. });
  118. }
  119. } else if (type === 'application/xml') {
  120. result.data = await this.ossClient.parseXML(str);
  121. }
  122. return result;
  123. };
  124. return ImageClient;
  125. };