import CryptVideoUploadTask from "../CryptUploadTack/CryptVideoUploadTask"; import {VideoPublishParam} from "../UGCPublishTypeDef"; import fs from "fs"; import {AllFileContext, UploadResult} from "../Model"; import path from "path"; import {CryptUploadDispatcher} from "../CryptUploadTack/CryptUploadDispatcher"; import S3 from "aws-sdk/clients/s3"; import OSS from "ali-oss"; import {CheckBucket} from "../StreamCache/CheckCache"; import {stringToUint8Array} from "./TransferFunction"; import {SencondPassBucket} from "../StreamCache/SencondPassCache"; import {EnctyptObject, getPathDigest} from "../Crypt/Encrypt"; import UGCPublish from "../index"; import {VedioSencondPassBucket} from "../StreamCache/VedioSencondPassCache"; /** * 参数检查 * */ export class VerifUtil { private fromHexString(data:string):Uint8Array { const fromHexString = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); return fromHexString(data); } private toHexString(data:Uint8Array):string { const toHexString = bytes => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); return toHexString(data); } /** 其他的验证过程 */ public verifGetAllFileContext(allFileInfo: AllFileContext,verifContext: Map): boolean{ if (verifContext.get("file") != true) { if (allFileInfo.file != undefined || allFileInfo.file.length > 0){ verifContext.set("file",true); } } if (verifContext.get("fileSize") != true) { if (allFileInfo.fileSize != undefined || allFileInfo.fileSize > 0){ verifContext.set("fileSize",true); } } if (verifContext.get("digest") != true){ if(allFileInfo.digest.length == 32){ verifContext.set("digest",true); } } /** 注意 如果不是文件上传的话 后缀名不能为空 */ if (allFileInfo.uploadTaskType == "IUploadTask"){ if (verifContext.get("fileExt") != true) { if (allFileInfo.fileExt.length != 0) { verifContext.set("fileExt", true); } } } else if (allFileInfo.uploadTaskType == "IUploadTask"){ if (verifContext.get("fileExt") != true) { if (allFileInfo.fileExt.length != 0) { verifContext.set("fileExt", true); } } }else if (allFileInfo.uploadTaskType == "IUploadTask"){ verifContext.set("fileExt", true); } if (verifContext.get("today") != true) { if (allFileInfo.today != undefined){ verifContext.set("today",true); } } if (verifContext.get("timeData") != true) { if (allFileInfo.timeData != undefined){ verifContext.set("timeData",true); } } if (verifContext.get("objectKeyPartial") != true) { if (allFileInfo.objectKeyPartial != undefined || allFileInfo.objectKeyPartial.length!= 0){ verifContext.set("objectKeyPartial",true); } } if (verifContext.get("client") != true) { if (allFileInfo.client != undefined){ if (allFileInfo.client instanceof S3){ verifContext.set("client",true); } else if (allFileInfo.client instanceof OSS){ verifContext.set("client",true); } } allFileInfo.client } if (verifContext.get("uploadTaskType") != true) { if (allFileInfo.uploadTaskType != undefined || allFileInfo.uploadTaskType.length!= 0){ verifContext.set("uploadTaskType",true); } } if (verifContext.get("fileSize") == true || verifContext.get("fileExt") == true || verifContext.get("today") == true || verifContext.get("timeData") == true || verifContext.get("objectKeyPartial") == true || verifContext.get("client") == true || verifContext.get("uploadTaskType") == true ){ return true; } return false; } /** 验证续传信息参数 */ public verifCheckPassInfo(checkpoint: CheckBucket): boolean{ if (checkpoint.plain_sha == null || checkpoint.md5 == null || checkpoint.media_id == null || checkpoint.file_size == null || checkpoint.object_key == null || checkpoint.part_size == null || checkpoint.upload_id == null || checkpoint.key_value == null || checkpoint.iv_value == null || checkpoint.plain_sha == undefined || checkpoint.md5 == undefined || checkpoint.media_id == undefined || checkpoint.file_size == undefined || checkpoint.object_key == undefined || checkpoint.part_size == undefined || checkpoint.upload_id == undefined || checkpoint.key_value == undefined || checkpoint.iv_value == undefined) { return false; } if ( checkpoint.plain_sha.length == 32 && checkpoint.md5.length != 0 && checkpoint.media_id.length != 0 && checkpoint.file_size.length != 0 && checkpoint.object_key.length != 0 && checkpoint.part_size.length != 0 && checkpoint.upload_id.length != 0 && stringToUint8Array(checkpoint.key_value).length == 64 && stringToUint8Array(checkpoint.iv_value).length == 32 ) { return true; } return false; } /** 验证上传申请参数 */ public verifMakeApplyUploadRequest(_uploadTask,applyRequest,allFileInfo): boolean{ /** 注意 如果不是文件上传的话 后缀名不能为空 */ if (allFileInfo.uploadTaskType == "IUploadTask") { //console.log("_uploadTask:",_uploadTask); if ( applyRequest.CategoryId == 1 && applyRequest.MediaType == 'video' && applyRequest.FileName.length != 0 && applyRequest.FileSize.length != 0 && applyRequest.Region.length != 0 && applyRequest.BucketName.length != 0 && applyRequest.ServerType.length != 0 ) { return true } } else if (allFileInfo.uploadTaskType == "IUploadTask"){ //console.log("_uploadTask:",_uploadTask); if ( applyRequest.CategoryId == 1 && ( applyRequest.MediaType == 'headshot' || applyRequest.MediaType == 'figure' || applyRequest.MediaType == 'images' ) && applyRequest.FileName.length != 0 && applyRequest.FileSize.length != 0 && applyRequest.Region.length != 0 && applyRequest.BucketName.length != 0 && applyRequest.ServerType.length != 0 ) { return true } } else if (allFileInfo.uploadTaskType == "IUploadTask"){ //console.log("_uploadTask:",_uploadTask); if ( applyRequest.CategoryId == 1 && applyRequest.MediaType == 'other' && applyRequest.FileName.length != 0 && applyRequest.FileSize.length != 0 && applyRequest.Region.length != 0 && applyRequest.BucketName.length != 0 && applyRequest.ServerType.length != 0 ) { return true } } return false; } /** 验证UUID是否获取成功 */ public verifUuid(uuid:string):boolean{ if (uuid.length != 0){ return true; } return false; } /** 视频上传验证 */ public async verifGetVedioFile(param: VideoPublishParam): Promise{ return await this.isFileExsiting(param.video); } public verifGetVedioParam(task:CryptVideoUploadTask): boolean{ if (task.ffmpegPath != undefined && task.ffmpegPath.length != 0 && task.duration != undefined && task.duration != 0 && task.width != undefined && task.width != 0 && task.height != undefined && task.height != 0 ) { return true; } return false; } /** 验证解压图片是否为空 */ public verifGetCoverFile(coverFile: string): boolean{ if (coverFile.length > 0) { return true; } return false; } /** 验证秒传信息 */ public verifSencodePassInfo(bucket): boolean{ if(/** 原来文件的摘要 */ bucket.plain_sha == null || /** 创建时间 */ bucket.creation_time == null || /** 存储时间 */ bucket.duration_time == null || /** 桶名称 */ //bucket.Complete.Bucket== null || /** 图片名称 */ bucket.key == null || /** 图片url */ bucket.url == null || /** 图片加密信息 */ bucket.crypt_info_filesize == null || bucket.crypt_info_key == null || bucket.crypt_info_digest == null //bucket.crypt_info_object_key_partial == null ){ return false; } if(/** 原来文件的摘要 */ bucket.plain_sha.length > 0 && /** 创建时间 */ bucket.creation_time.length > 0 && /** 存储时间 */ bucket.duration_time.length >= 0 && /** 桶名称 */ //bucket.Complete.Bucket.length > 0 && /** 图片名称 */ bucket.key.length > 0 && /** 图片url */ bucket.url.length > 0 && /** 图片加密信息 */ bucket.crypt_info_filesize.length > 0 && bucket.crypt_info_key.length > 0 && bucket.crypt_info_digest.length > 0 //bucket.crypt_info_object_key_partial >= 0 ){ return true; } return false; } /** 验证续传信息 */ public verifCacheCache(key_value,iv_value): boolean{ console.log("1:",key_value.length,"2",iv_value.length); if(key_value != undefined && iv_value != undefined && key_value.length == 64 && iv_value.length == 16){ return true; } return false; } /** 验证加密信息 */ public verifEnctyptInfo(enctyptObjectVideo:EnctyptObject): boolean{ console.log("EnctyptObject",enctyptObjectVideo.iv.byteLength); if (enctyptObjectVideo.key.byteLength == 64 && enctyptObjectVideo.digest.byteLength == 32 && enctyptObjectVideo.iv.byteLength == 16 && enctyptObjectVideo.ciphertext.byteLength > 0){ return true; } console.log("验证加密信息发现信息中有错误信息"); return false; } /** 验证上传后的信息 */ public verifUploadInfoVedio(promise0: UploadResult,promise1: UploadResult): boolean[] { let verifResult = []; if (this.fromHexString(promise0.cryptInfo.key.toString()).length == 64 && this.fromHexString(promise0.cryptInfo.digest.toString()).length == 32 && promise0.Key.length > 0 && promise0.Url.length > 0 //&& //promise0.Bucket.length > 0 ) { verifResult[0] = true; }else { verifResult[0] = false; } if (this.fromHexString(promise1.cryptInfo.key.toString()).length == 64 && this.fromHexString(promise1.cryptInfo.digest.toString()).length == 32 && promise1.Key.length > 0 && promise1.Url.length > 0 //&& //promise1.Bucket.length > 0 ) { verifResult[0] = true; }else { verifResult[0] = false; } return verifResult; } /** 验证上传后的信息 */ public verifUploadInfoFile(promise0: UploadResult): boolean { let verifResult: boolean; console.log("promise0",promise0); if (this.fromHexString(promise0.cryptInfo.key.toString()).length == 64 && this.fromHexString(promise0.cryptInfo.digest.toString()).length == 32 && promise0.Key.length > 0 && promise0.Url.length > 0 /*&& promise0.Bucket != null*/ ) { console.log("验证成功"); verifResult = true; }else { verifResult= false; } return verifResult; } /** 图片转换验证 */ public verifTransformImage(bufArr,_resolutions): boolean{ for (let i = 0; i < _resolutions.length ; i++){ if (bufArr[i].name.length > 0 && bufArr[i].file.length > 0 && bufArr[i].fileSize > 0 && bufArr[i].width > 0 && bufArr[i].height > 0){ continue; }else { return false; } } return true; } 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); }); }) } }