UNPKG

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