import { addMinutes } from "date-fns/fp" ; import { AlibabaCloud } from "../../../../constants/Process" ; import crypto from "crypto" ; import OSS from "ali-oss" ; import { Region } from "../../../../constants/Project" ; export function getDisposition ( fileName ) { const encodeFileName = encodeURIComponent ( fileName ) ; return `attachment; filename="${ encodeFileName }"; filename*=UTF-8''${ encodeFileName }` ; } param { string } fileName param { string } filePath param { number } fileSize param { Region } region param { number } expiration see [English Document] https see [Chinese Document] https export const policyTemplate = ( fileName , filePath , fileSize , region , expiration = 60 * 2 ) : { policy : string ; signature : string ; } => { const policyString = JSON . stringify ( { expiration : addMinutes ( expiration ) ( new Date ( ) ) . toISOString ( ) , conditions : [ { bucket : AlibabaCloud [ region ] . OSS_BUCKET , } , [ "content-length-range" , fileSize , fileSize ] , [ "eq" , "$key" , filePath ] , [ "eq" , "$Content-Disposition" , getDisposition ( fileName ) ] , ] , } ) ; const policy = Buffer . from ( policyString ) . toString ( "base64" ) ; const signature = crypto . createHmac ( "sha1" , AlibabaCloud . OSS_ACCESS_KEY_SECRET ) . update ( policy ) . digest ( "base64" ) ; return { policy , signature , } ; } ; const createOSSClient = ( region ) : OSS => { return new OSS ( { bucket : AlibabaCloud [ region ] . OSS_BUCKET , region : AlibabaCloud [ region ] . OSS_REGION , endpoint : AlibabaCloud . OSS_ENDPOINT , accessKeyId : AlibabaCloud . OSS_ACCESS_KEY , accessKeySecret : AlibabaCloud . OSS_ACCESS_KEY_SECRET , secure : true , } ) ; } ; export const ossClient = { [ Region . CN_HZ ] : createOSSClient ( Region . CN_HZ ) , [ Region . US_SV ] : createOSSClient ( Region . US_SV ) , [ Region . SG ] : createOSSClient ( Region . SG ) , [ Region . IN_MUM ] : createOSSClient ( Region . IN_MUM ) , [ Region . GB_LON ] : createOSSClient ( Region . GB_LON ) , } ; param { string } fullPath param { Region } region see [English Document] https see [Chinese Document] https export async function isExistObject ( fullPath , region ) { try { await ossClient [ region ] . head ( fullPath ) ; return true ; } catch { return false ; } }