@@ -4,7 +4,7 @@ import { AssumedCredentials, MinioConfig } from ".";
44import { ExtendedRequestMethod , ExtendedRequestOptionsInit } from "../request" ;
55import { ApplicationError , BucketPolicy } from "../core" ;
66import * as Minio from "minio" ;
7- import { UploadResult } from "./types" ;
7+ import { PresignedResult , UploadResult } from "./types" ;
88
99export interface MinioOptions {
1010 configURL : string ;
@@ -21,13 +21,11 @@ interface MinioContext {
2121const minioContext : MinioContext = { tokenTime : Date . now ( ) . valueOf ( ) } ;
2222
2323type SimpleStringValue = {
24- value : string
25- }
24+ value : string ;
25+ } ;
2626
2727export class MinioUtils {
28- private constructor ( private request : ExtendedRequestMethod , public options : MinioOptions ) {
29-
30- }
28+ private constructor ( private request : ExtendedRequestMethod , public options : MinioOptions ) { }
3129
3230 static create ( request : ExtendedRequestMethod , serverBaseUrl : string ) {
3331 const url = ( serverBaseUrl || "" ) . trim ( ) ;
@@ -76,14 +74,14 @@ export class MinioUtils {
7674 // 提前三分钟过期
7775 const durationMills = durationSeconds ? ( durationSeconds - 180 ) * 1000 : 0 ;
7876
79- if ( minioContext . stsToken === undefined || ( minioContext . tokenTime + durationMills ) <= Date . now ( ) . valueOf ( ) ) {
77+ if ( minioContext . stsToken === undefined || minioContext . tokenTime + durationMills <= Date . now ( ) . valueOf ( ) ) {
8078 const r = await this . request < AssumedCredentials > ( this . options . stsTokenURL , {
8179 method : "POST" ,
8280 skipNotifyError : true
8381 } ) ;
8482 if ( r . response . ok ) {
8583 minioContext . stsToken = r . data ;
86- minioContext . tokenTime = Date . now ( ) . valueOf ( )
84+ minioContext . tokenTime = Date . now ( ) . valueOf ( ) ;
8785 } else {
8886 return r as any ; // 发生错误,类型无所谓
8987 }
@@ -92,11 +90,9 @@ export class MinioUtils {
9290 return { data : minioContext , response : { ok : true } } as any ;
9391 }
9492
95-
9693 public generateObjectUrl ( key : string ) : string | undefined {
9794 const config = minioContext . config ;
9895 if ( config && ! isNullOrEmptyString ( config . publicBucket ) ) {
99-
10096 const file = key . startsWith ( "/" ) ? key . substr ( 1 , key . length - 1 ) : key ;
10197 if ( config . port ) {
10298 return `${ config . schema } ://${ config . host } :${ config . port } /${ config . publicBucket } /${ file } ` ;
@@ -109,73 +105,64 @@ export class MinioUtils {
109105 public async presignedObjectUrl (
110106 filePath : string ,
111107 resOptions ?: Omit < ExtendedRequestOptionsInit , "method" | "data" >
112- ) : Promise < string > {
113- const file = encodeURI ( filePath )
108+ ) : Promise < RequestResponse < PresignedResult & ApplicationError > > {
109+ const file = encodeURI ( filePath ) ;
114110 const requestOptions = { ...( resOptions || { } ) , method : "POST" } ;
115- const { response, data } = await this . request < SimpleStringValue > ( `${ this . options . genUrl } ?key=${ file } ` , requestOptions ) ;
116- if ( response . ok ) {
117- return data . value ;
118- } else {
119- return "" ;
120- }
111+ const { response, data } = await this . request < SimpleStringValue & ApplicationError > (
112+ `${ this . options . genUrl } ?key=${ file } ` ,
113+ requestOptions
114+ ) ;
115+ return { data : { url : ( data ?. value || "" ) } , response }
121116 }
122117
123118 private putObjectAsync (
124119 client : Minio . Client ,
125120 bucketName : string ,
126121 objectName : string ,
127- stream : any ) : Promise < RequestResponse < UploadResult > > {
122+ stream : any
123+ ) : Promise < RequestResponse < UploadResult > > {
128124 return new Promise ( ( resolve , reject ) => {
129125 client . putObject ( bucketName , objectName , stream , ( err , etag ) => {
130126 if ( err === undefined || err === null ) {
131- resolve (
132- { data : { etag } , response : { ok : true , status : 200 , type :"default" } as any }
133- ) ;
127+ resolve ( { data : { etag } , response : { ok : true , status : 200 , type : "default" } as any } ) ;
134128 } else {
135- const data : UploadResult = { etag} ;
136- if ( ( typeof err ) === "string" ) {
129+ const data : UploadResult = { etag } ;
130+ if ( typeof err === "string" ) {
137131 data . error = err as any ;
138132 data . error_description = err as any ;
139- } else {
133+ } else {
140134 data . error = err ?. name ;
141- data . error_description = err ?. message
135+ data . error_description = err ?. message ;
142136 }
143-
144- reject ( { data, response : { ok : false , status : 500 } as any } ) ;
137+
138+ reject ( { data, response : { ok : false , status : 500 } as any } ) ;
145139 }
146140 } ) ;
147141 } ) ;
148142 }
149143
150-
151- public async upload (
152- bucketPolicy : BucketPolicy ,
153- key : string ,
154- stream : any
155- ) : Promise < RequestResponse < UploadResult > > {
156-
144+ public async upload ( bucketPolicy : BucketPolicy , key : string , stream : any ) : Promise < RequestResponse < UploadResult > > {
157145 const r = await this . getMinioContext ( ) ;
158146
159- const { response, data } = r ;
147+ const { response, data } = r ;
160148 if ( ! r . response . ok ) {
161- return { data : data as any , response }
149+ return { data : data as any , response } ;
162150 }
163151
164152 const { stsToken, config } = r . data ;
165- const cnf = config as MinioConfig
153+ const cnf = config as MinioConfig ;
166154
167155 const bucketName = bucketPolicy === BucketPolicy . Private ? cnf . privateBucket : cnf . publicBucket ;
168156
169157 const minioClient = new Minio . Client ( {
170158 endPoint : cnf . host ,
171159 port : cnf . port ,
172160 region : cnf . region ,
173- useSSL : ( cnf . schema === "https" ) ,
161+ useSSL : cnf . schema === "https" ,
174162 accessKey : stsToken ?. accessKey || "" ,
175163 secretKey : stsToken ?. secretKey || ""
176164 } ) ;
177165
178- return await this . putObjectAsync ( minioClient , bucketName , key , stream )
179- } ;
180-
181- }
166+ return await this . putObjectAsync ( minioClient , bucketName , key , stream ) ;
167+ }
168+ }
0 commit comments