Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 17, 2023
1 parent f3aadac commit a58fa65
Showing 1 changed file with 72 additions and 73 deletions.
145 changes: 72 additions & 73 deletions src/OSSObject.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Readable } from 'node:stream';
import type {
// IObjectSimple,
// GetObjectOptions,
ListObjectsQuery,
RequestOptions,
ListObjectResult,
// PutObjectOptions,
// PutObjectResult,
PutObjectOptions,
PutObjectResult,
// NormalSuccessResponse,
// HeadObjectOptions,
// HeadObjectResult,
Expand All @@ -19,7 +20,6 @@ import type {
// UserMeta,
// ObjectCallback,
} from 'oss-interface';

import {
OSSBaseClientInitOptions,
OSSBaseClient,
Expand Down Expand Up @@ -69,84 +69,83 @@ export class OSSObject extends OSSBaseClient {
// return result;
// };

// /**
// * put an object from String(file path)/Buffer/ReadableStream
// * @param {String} name the object key
// * @param {Mixed} file String(file path)/Buffer/ReadableStream
// * @param {Object} options options
// * {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
// * {String} options.callback.url the OSS sends a callback request to this URL
// * {String} options.callback.host The host header value for initiating callback requests
// * {String} options.callback.body The value of the request body when a callback is initiated
// * {String} options.callback.contentType The Content-Type of the callback requests initiatiated
// * {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
// * customValue = {
// * key1: 'value1',
// * key2: 'value2'
// * }
// * @return {Object} result
// */
// proto.put = async function put(name, file, options) {
// let content;
// options = options || {};
// name = this._objectName(name);

// if (Buffer.isBuffer(file)) {
// content = file;
// } else if (typeof file === 'string') {
// const stats = fs.statSync(file);
// if (!stats.isFile()) {
// throw new Error(`${file} is not file`);
// }
// options.mime = options.mime || mime.getType(path.extname(file));
// options.contentLength = await this._getFileSize(file);
// const getStream = () => fs.createReadStream(file);
// const putStreamStb = (objectName, makeStream, configOption) => {
// return this.putStream(objectName, makeStream(), configOption);
// };
// return await retry(putStreamStb, this.options.retryMax, {
// errorHandler: err => {
// const _errHandle = _err => {
// const statusErr = [ -1, -2 ].includes(_err.status);
// const requestErrorRetryHandle = this.options.requestErrorRetryHandle || (() => true);
// return statusErr && requestErrorRetryHandle(_err);
// };
// if (_errHandle(err)) return true;
// return false;
// },
// })(name, getStream, options);
// } else if (isReadable(file)) {
// return await this.putStream(name, file, options);
// } else {
// throw new TypeError('Must provide String/Buffer/ReadableStream for put.');
// }
/**
* put an object from String(file path)/Buffer/Readable
* @param {String} name the object key
* @param {Mixed} file String(file path)/Buffer/Readable
* @param {Object} options options
* {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
* {String} options.callback.url the OSS sends a callback request to this URL
* {String} options.callback.host The host header value for initiating callback requests
* {String} options.callback.body The value of the request body when a callback is initiated
* {String} options.callback.contentType The Content-Type of the callback requests initiated
* {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
* customValue = {
* key1: 'value1',
* key2: 'value2'
* }
* @return {Object} result
*/
async put(name: string, file: string | Buffer | Readable, options?: PutObjectOptions): Promise<PutObjectResult> {
let content;
name = this.#objectName(name);
options = options || {};
if (Buffer.isBuffer(file)) {
content = file;
} else if (typeof file === 'string') {
const stats = fs.statSync(file);
if (!stats.isFile()) {
throw new Error(`${file} is not file`);
}
options.mime = options.mime || mime.getType(path.extname(file));
options.contentLength = await this._getFileSize(file);
const getStream = () => fs.createReadStream(file);
const putStreamStb = (objectName, makeStream, configOption) => {
return this.putStream(objectName, makeStream(), configOption);
};
return await retry(putStreamStb, this.options.retryMax, {
errorHandler: err => {
const _errHandle = _err => {
const statusErr = [ -1, -2 ].includes(_err.status);
const requestErrorRetryHandle = this.options.requestErrorRetryHandle || (() => true);
return statusErr && requestErrorRetryHandle(_err);
};
if (_errHandle(err)) return true;
return false;
},
})(name, getStream, options);
} else if (isReadable(file)) {
return await this.putStream(name, file, options);
} else {
throw new TypeError('Must provide String/Buffer/ReadableStream for put.');
}

// options.headers = options.headers || {};
// this._convertMetaToHeaders(options.meta, options.headers);
options.headers = options.headers || {};
this._convertMetaToHeaders(options.meta, options.headers);

// const method = options.method || 'PUT';
// const params = this._objectRequestParams(method, name, options);
const method = options.method || 'PUT';
const params = this._objectRequestParams(method, name, options);

// callback.encodeCallback(params, options);
callback.encodeCallback(params, options);

// params.mime = options.mime;
// params.content = content;
// params.successStatuses = [ 200 ];
params.mime = options.mime;
params.content = content;
params.successStatuses = [ 200 ];

// const result = await this.request(params);
const result = await this.request(params);

// const ret = {
// name,
// url: this._objectUrl(name),
// res: result.res,
// };
const ret = {
name,
url: this._objectUrl(name),
res: result.res,
};

// if (params.headers && params.headers['x-oss-callback']) {
// ret.data = JSON.parse(result.data.toString());
// }
if (params.headers && params.headers['x-oss-callback']) {
ret.data = JSON.parse(result.data.toString());
}

// return ret;
// };
return ret;
}

// /**
// * put an object from ReadableStream.
Expand Down

0 comments on commit a58fa65

Please sign in to comment.