import IUploadTask from './ICryptUploadTask'; import {ApplyUploadRequest, IMediaInfo} from '../BackendServerModel'; import {UploadProgress, ImageComplete, UploadResult, AllFileContext} from '../Model'; import { Resolution, ImagePublishUsage } from '../UGCPublishTypeDef'; import S3, { PutObjectRequest } from 'aws-sdk/clients/s3'; import OSS from 'ali-oss'; import fs from 'fs'; import { TransformImage, GetGifInfo } from '../utils/ImageUtils'; import ICryptUploadTask from "./ICryptUploadTask"; import {EnctyptObject, enctyptSdk} from "../Crypt/Encrypt"; import {CheckBucket} from "../StreamCache/CheckCache"; import {SencondPassBucket} from "../StreamCache/SencondPassCache"; import {CryptUploadDispatcher} from "./CryptUploadDispatcher"; import UGCPublish from "../index"; export default class CryptImageUploadTask implements ICryptUploadTask { private _resolutions: Resolution[]; private _definiteResolutions: { w: number, h: number }[]; ffmpegPath?: string; objectKeyPrefix: string; // 默认前缀 images; 另外还有 headshot, figure constructor(resolutions: Resolution[], usage: ImagePublishUsage, definiteResolutions: { w: number, h: number }[]) { this._resolutions = resolutions; this._definiteResolutions = definiteResolutions; if (usage == ImagePublishUsage.Headshot) { this.objectKeyPrefix = 'headshot'; console.log(this.objectKeyPrefix); } else if (usage == ImagePublishUsage.Figure) { this.objectKeyPrefix = 'figure'; } else { this.objectKeyPrefix = 'images'; } } makeApplyUploadRequest( objectKeyPartial: string, file: string, region: string, bucket: string, fileExt: string, serverType: number): ApplyUploadRequest { let fileSize = fs.statSync(file).size; let imageGroups = this._definiteResolutions.map((res, i) => { return { Name: this._resolutions[i].name, Resolution: { width: res.w, height: res.h } }; }); let mediaInfo: IMediaInfo = {FileExt: fileExt, ImageGroup: imageGroups}; let applyRequest: ApplyUploadRequest = { Md5: undefined, CategoryId: 1, MediaType: this.objectKeyPrefix, FileName: objectKeyPartial, FileSize: fileSize, Region: region, BucketName: bucket, ServerType: serverType, MediaInfo: mediaInfo }; return applyRequest; } makeResult(mediaId: string, fileSize: number, mark: string, uploadRet: UploadResult[]): ImageComplete[] { let uploadResultLen = uploadRet.length; return this._resolutions.map((res, i) => { // uploadRet 返回数组可能只有一条数据 let uploadResultIndex = i <= uploadResultLen - 1 ? i : uploadResultLen - 1; let img: ImageComplete = { name: res.name, id: mediaId, url: uploadRet[uploadResultIndex].Url, mark: mark, width: this._definiteResolutions[i].w, height: this._definiteResolutions[i].h, filesize: fileSize, // 不准确 cryptInfo: uploadRet[i].cryptInfo }; return img; }); } async makeUploadTask(client: S3 | OSS, bucket: string, objectKeyPartial: string, mediaId: string, file: string, fileExt: string, statusCp: CheckBucket | undefined,today:Date,_receive_proxy, _minor_receive_proxy, _bucket,allFileInfo:AllFileContext, listener: (progress: UploadProgress) => void): Promise { /** 注意在这里压缩 */ let bufArrInfo = await UGCPublish.processUtil.transformImage(this.ffmpegPath, file, this._resolutions); if (bufArrInfo[0].exception != 0){ return [ { Error: bufArrInfo[0].exception } ]; } console.log(this.objectKeyPrefix); let bufArr = bufArrInfo[0].Complete; /** 查找图片秒速状态缓存 */ let uploadRetArr = UGCPublish.processUtil.getImageSencodePassInfo(statusCp,allFileInfo,bufArr,this); return uploadRetArr; } private async UploadImage(client: S3 | OSS, bucket: string, objectKey: string, fileSize: number, enctyptObject: EnctyptObject,objectKeyPartial:string, isGif: boolean, listener: (progress: UploadProgress) => void): Promise { if (client instanceof S3) { let request: PutObjectRequest = { Bucket: bucket, ContentLength: Buffer.from(enctyptObject.ciphertext).length, ContentType: isGif ? 'image/gif' : 'image/jpg', Key: objectKey, Tagging: 'public=1', Body: Buffer.from(enctyptObject.ciphertext) }; let upload = client.upload(request); upload.on('httpUploadProgress', progress => { listener.call(listener, {Uploaded: progress.loaded, Total: progress.total}); }); let sendData = await upload.promise(); //console.log(sendData); } else if (client instanceof OSS) { //console.log("objectKey:",objectKey); //console.log("开始执行图片阿里云上传"); let sendData = await client.multipartUpload(objectKey, new Buffer(enctyptObject.ciphertext),{}); } else { throw 'makeUploadTask uploader object is wrong.'; } let url = `${IUploadTask.proxyUrl}/${objectKey}`; //let url = `https://${that._bucket}.s3-${that._region}.amazonaws.com/${objectKey}`; const toHexString = bytes => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); console.log("bucket.",bucket); return { Url: url, Bucket: bucket, Key: objectKey, cryptInfo: { digest: toHexString(new Uint8Array(enctyptObject.digest)), key: toHexString(new Uint8Array(enctyptObject.key)), fileSize:fileSize, objectKeyPartial: objectKeyPartial } }; } }