|
@@ -4,6 +4,7 @@ import * as stream from 'stream';
|
|
|
import * as moment from 'moment';
|
|
import * as moment from 'moment';
|
|
|
import * as Path from 'path';
|
|
import * as Path from 'path';
|
|
|
import * as OSS from 'ali-oss';
|
|
import * as OSS from 'ali-oss';
|
|
|
|
|
+import * as fs from 'fs';
|
|
|
|
|
|
|
|
export interface UploadResult {
|
|
export interface UploadResult {
|
|
|
uploaded: boolean;
|
|
uploaded: boolean;
|
|
@@ -18,8 +19,9 @@ export interface File {
|
|
|
originalname: string;
|
|
originalname: string;
|
|
|
encoding: string;
|
|
encoding: string;
|
|
|
mimetype: string;
|
|
mimetype: string;
|
|
|
- buffer: Buffer;
|
|
|
|
|
|
|
+ buffer?: Buffer;
|
|
|
size: number;
|
|
size: number;
|
|
|
|
|
+ path?: string;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export interface OSSSucessResponse {
|
|
export interface OSSSucessResponse {
|
|
@@ -67,7 +69,7 @@ export class OSSBase {
|
|
|
*/
|
|
*/
|
|
|
protected async putStream(
|
|
protected async putStream(
|
|
|
target: string,
|
|
target: string,
|
|
|
- imageStream: stream.PassThrough,
|
|
|
|
|
|
|
+ imageStream: stream.Readable | stream.PassThrough,
|
|
|
): Promise<OSSSucessResponse> {
|
|
): Promise<OSSSucessResponse> {
|
|
|
return await this.ossClient.putStream(target, imageStream);
|
|
return await this.ossClient.putStream(target, imageStream);
|
|
|
}
|
|
}
|
|
@@ -99,19 +101,40 @@ export class OSSBase {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- const imageStream = new stream.PassThrough();
|
|
|
|
|
- imageStream.end(item.buffer);
|
|
|
|
|
- const uploadResult = await this.putStream(target, imageStream);
|
|
|
|
|
-
|
|
|
|
|
- if (uploadResult.res.status === 200) {
|
|
|
|
|
- info.path = uploadResult.name;
|
|
|
|
|
- info.src = this.formatDomain(uploadResult.url);
|
|
|
|
|
- info.srcSign = this.getOssSign(info.src);
|
|
|
|
|
|
|
+ if (item.path) {
|
|
|
|
|
+ // Use multipartUpload for file paths (large files)
|
|
|
|
|
+ const uploadResult = await this.ossClient.multipartUpload(target, item.path);
|
|
|
|
|
+ if (uploadResult.res.status === 200) {
|
|
|
|
|
+ info.path = uploadResult.name;
|
|
|
|
|
+ // multipartUpload result might not contain url directly in the same way, construct it or use what's available
|
|
|
|
|
+ // Usually uploadResult.res.requestUrls contains the URL, or we construct it.
|
|
|
|
|
+ // But formatDomain expects a full URL.
|
|
|
|
|
+ // Let's reconstruct the URL based on bucket and endpoint if needed, or use existing method if compatible.
|
|
|
|
|
+ // uploadResult.name is the object key.
|
|
|
|
|
+ const bucket = this.options.client.bucket;
|
|
|
|
|
+ const endpoint = this.options.client.endpoint;
|
|
|
|
|
+ const protocol = this.options.client.secure ? 'https' : 'http';
|
|
|
|
|
+ const url = `${protocol}://${bucket}.${endpoint}/${uploadResult.name}`;
|
|
|
|
|
+ info.src = this.formatDomain(url);
|
|
|
|
|
+ info.srcSign = this.getOssSign(info.src);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (item.buffer) {
|
|
|
|
|
+ const imageStream = new stream.PassThrough();
|
|
|
|
|
+ imageStream.end(item.buffer);
|
|
|
|
|
+ const uploadResult = await this.putStream(target, imageStream);
|
|
|
|
|
+
|
|
|
|
|
+ if (uploadResult.res.status === 200) {
|
|
|
|
|
+ info.path = uploadResult.name;
|
|
|
|
|
+ info.src = this.formatDomain(uploadResult.url);
|
|
|
|
|
+ info.srcSign = this.getOssSign(info.src);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new Error('File has no buffer or path');
|
|
|
}
|
|
}
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error('error', error);
|
|
console.error('error', error);
|
|
|
info.uploaded = false;
|
|
info.uploaded = false;
|
|
|
- info.path = item.originalname;
|
|
|
|
|
|
|
+ // info.path = item.originalname;
|
|
|
info.message = `上传失败: ${error}`;
|
|
info.message = `上传失败: ${error}`;
|
|
|
}
|
|
}
|
|
|
|
|
|