import { ImagePublishParam, VideoPublishParam, FilePublishParam, Resolution } from './UGCPublishTypeDef' import { UploadProgress, VideoComplete, ImageComplete, FileComplete } from './Model'; import { UploadDispatcher } from './UploadTask'; import fs from 'fs'; import path from 'path'; import FFmpeg from './node-ffmpeg'; import { MakeResulotionsDefinite } from './utils/ImageUtils'; import FileUploadTask from './UploadTask/FileUploadTask'; import VideoUploadTask from './UploadTask/VideoUploadTask'; import ImageUploadTask from './UploadTask/ImageUploadTask'; import { EnctyptObject } from "./Crypt/Encrypt"; import {CryptUploadDispatcher} from "./CryptUploadTack/CryptUploadDispatcher"; import CryptFileUploadTask from "./CryptUploadTack/CryptFileUploadTask"; import CryptImageUploadTask from "./CryptUploadTack/CryptImageUploadTask"; import { decryptSdkInBuffer, decryptSdkInPath } from './Crypt/Decrypt'; import {VerifUtil} from "./utils/VerifUtil"; import {VedioExceptionModel} from "./utils/ExceptionUtil"; import {ProcessUtil} from "./utils/ProcessUtil"; import {StaleDataManager} from "./StaleDataControl/StaleDataManager"; export { ImagePublishParam, VideoPublishParam, FilePublishParam, Resolution }; export default class UGCPublish { private _url: string = ""; //private static _settings:SdkSettingsResult|null = null; //private static _timeSettings:number = 0; // private _region = 'ap-northeast-1'; // private _bucket = 'didisit'; static _ffmpegPath?: string; private _token: string = ""; static verifUtil: VerifUtil = new VerifUtil(); static processUtil: ProcessUtil = new ProcessUtil(); static staleDataManager:StaleDataManager = new StaleDataManager(); constructor() { } /** 需要手动执行一下接口信息 */ public async RunTimeSpectCycle(){ UGCPublish.staleDataManager.run(); } /** * 设置应用服务器的地址 * @param url */ withAppServerUrl(url: string) { this._url = url; return this; } /** * 设置用户令牌 * @param token */ withToken(token: string) { this._token = token; return this; } /** * ffmpeg 路径 */ withFFmpeg(ffmpegPath: string) { UGCPublish._ffmpegPath = ffmpegPath; return this; } /** * 上传视频文件 (视频文件 + 封面图) * * @param param * @return */ publishVideo(param: VideoPublishParam, listener: (progress: UploadProgress | null, result: VideoComplete | null, err: any) => void) { // 上传视频 this.PublishVideoAsync(param, (progress) => { listener.call(listener, progress, null, null); }) .then((uploadRet) => { listener.call(listener, null, uploadRet, null); }) .catch(e => { listener.call(listener, null, null, e); }) } /** * 上传图片文件 * @param param * @return */ publishImage(param: ImagePublishParam, listener: (progress: UploadProgress | null, result: Array | null, err: any) => void) { this.PublishImageAsync(param, prog => { listener.call(listener, prog, null, null); }) .then((imageCompletes) => { listener.call(listener, null, imageCompletes, null); }) .catch(e => { listener.call(listener, null, null, e); }) } /** * 上传文件 * @param param * @param listener */ public PublishFile(param: FilePublishParam, listener: (progress: UploadProgress | null, result: FileComplete | null, err: any) => void) { this.PublishFileAsync(param, prog => { listener.call(listener, prog, null, null); }) .then((fileComplete) => { listener.call(listener, null, fileComplete, null); }) .catch(e => { listener.call(listener, null, null, e); }) } /** * 异步视频上传 * @param param * @param listener */ public async PublishVideoAsync(param: VideoPublishParam, listener: (progress: UploadProgress) => void): Promise { if (param.encrypt != undefined && param.encrypt == true) { return this.PushlishCryptVideoAsync(param, listener); } await this.isFileExsiting(param.video); let video = await new FFmpeg(param.video, UGCPublish._ffmpegPath); let metadata: any = video.metadata; let duration = metadata.duration.seconds; let resolution = metadata.video.resolution; let rotate = metadata.video.rotate; let task = new VideoUploadTask; task.ffmpegPath = UGCPublish._ffmpegPath; //task.proxyUrl = this._receive_proxy; task.duration = duration; task.width = (rotate == 90 || rotate == 270) ? resolution.h : resolution.w; task.height = (rotate == 90 || rotate == 270) ? resolution.w : resolution.h; let dispatcher = new UploadDispatcher(task, this._url, this._token); return await dispatcher.start(param.video, param.mark, param.duration, listener); } /** * 异步图片上传 * @param param * @param listener */ public async publishImageAsync(param: ImagePublishParam, listener: (progress: UploadProgress) => void): Promise { if (param.encrypt != undefined && param.encrypt == true) { console.log("进入懂啊加密"); return this.PublishCryptImageAsync(param, listener); } await this.isFileExsiting(param.file); let extname = path.extname(param.file).toLowerCase(); // gif, png 第一张强制传原图 // if(extname == '.gif' || extname == '.png') { // param.resolutions[0] = { // name: 'Original', // quality: 95 // }; // } let definiteRes = await MakeResulotionsDefinite(UGCPublish._ffmpegPath, param.file, param.resolutions); let task = new ImageUploadTask(param.resolutions, param.usage, definiteRes); task.ffmpegPath = UGCPublish._ffmpegPath; let dispatcher = new UploadDispatcher(task, this._url, this._token); return await dispatcher.start(param.file, param.mark, param.duration, listener); } /** * 异步图片上传 * @param param * @param listener */ public async PublishImageAsync(param: ImagePublishParam, listener: (progress: UploadProgress) => void): Promise { if (param.encrypt != undefined && param.encrypt == true) { return this.PublishCryptImageAsync(param, listener); } await this.isFileExsiting(param.file); let extname = path.extname(param.file).toLowerCase(); // gif, png 第一张强制传原图 // if(extname == '.gif' || extname == '.png') { // param.resolutions[0] = { // name: 'Original', // quality: 95 // }; // } let definiteRes = await MakeResulotionsDefinite(UGCPublish._ffmpegPath, param.file, param.resolutions); let task = new ImageUploadTask(param.resolutions, param.usage, definiteRes); task.ffmpegPath = UGCPublish._ffmpegPath; let dispatcher = new UploadDispatcher(task, this._url, this._token); return await dispatcher.start(param.file, param.mark, param.duration, listener); } /** * 上传文件异步方法 * @param param * @param listener */ public async PublishFileAsync(param: FilePublishParam, listener: (progress: UploadProgress) => void): Promise { if (param.encrypt != undefined && param.encrypt == true) { return this.PublishCryptFileAsync(param, listener); } await this.isFileExsiting(param.file); let task = new FileUploadTask; let dispatcher = new UploadDispatcher(task, this._url, this._token); return await dispatcher.start(param.file, param.mark, param.duration, listener); } /** V2.0.1 加密方法对外接口 */ /** * 加密异步上传视频文件 * @param param * @param listener * */ public async PushlishCryptVideoAsync(param: VideoPublishParam, listener: (progress: UploadProgress) => void): Promise { param.encrypt = true; /** 如果文件获取不成功 */'' if (await UGCPublish.verifUtil.verifGetVedioFile(param) != true){ //console.log("获取视频信息失败"); return { Exception : VedioExceptionModel.E_GET_FILE_EXCEPTION }; }; /** Process is getVedioParamProcess */ let task = await UGCPublish.processUtil.getVedioParamProcess(param); /** 获取参数成功则继续执行正常流程 否则直接返回参数 */ console.log(task); let dispatcher = new CryptUploadDispatcher(task, this._url, this._token,"IUploadTask"); return await dispatcher.start(param.video, param.mark, param.duration, listener); //else { // return task; //} } /** * 加密异步图片上传 * @param param * @param listener */ public async PublishCryptImageAsync(param: ImagePublishParam, listener: (progress: UploadProgress) => void): Promise { param.encrypt = true; await this.isFileExsiting(param.file); let extname = path.extname(param.file).toLowerCase(); let definiteRes = await MakeResulotionsDefinite(UGCPublish._ffmpegPath, param.file, param.resolutions); let task = new CryptImageUploadTask(param.resolutions, param.usage, definiteRes); task.ffmpegPath = UGCPublish._ffmpegPath; let dispatcher = new CryptUploadDispatcher(task, this._url, this._token,"IUploadTask"); return await dispatcher.start(param.file, param.mark, param.duration, listener); } /** * 加密上传文件异步方法 * @param param * @param listener */ public async PublishCryptFileAsync(param: FilePublishParam, listener: (progress: UploadProgress) => void): Promise { param.encrypt = true; await this.isFileExsiting(param.file); let task = new CryptFileUploadTask; let dispatcher = new CryptUploadDispatcher(task, this._url, this._token,"IUploadTask"); return await dispatcher.start(param.file, param.mark, param.duration, listener); } /** * 通过BUFF来进行解密 * @param enctyptObject 密文对象 */ public async PublishBufferDecrypt(enctyptObject: EnctyptObject): Promise { //解密 let decryptResult = await decryptSdkInBuffer(enctyptObject); return decryptResult.plaintext; } /** * @breif 外部接口信息 * @param path string * @param key Uint8Array * @param digest ArrayBuffer * @returns 明文数据 */ public async PublishPathDecrypt(path: string, key: string, digest: string): Promise { //解密 let decryptResult = await decryptSdkInPath(path,key,digest); return decryptResult.plaintext; } private async isFileExsiting(path: string): Promise { return new Promise((resolve, reject) => { fs.stat(path, (err, stats) => { if (err) reject(err); else if (stats.size > 0) resolve(true); else reject("file is empty :" + path); }); }) } }