@@ -121,26 +121,44 @@ export default class ArtimiPlugin {
121121 let contentMd5 = Buffer . from ( checksum , 'hex' ) . toString ( 'base64' )
122122 let contentType = `image/${ type } `
123123
124+ // Use PUT request with Expect 100-continue
124125 let res = await fetch ( `${ host } /uploads/${ checksum } .${ type } ` , {
125- method : 'PUT' ,
126- redirect : 'follow' ,
127- body,
126+ method : 'GET' ,
128127 headers : {
129128 'Authorization' : `Bearer ${ token } ` ,
130- 'Content-MD5 ' : contentMd5 ,
131- 'Content-Type ' : contentType ,
132- 'Expect ' : '100-continue'
129+ 'X- Content-Length ' : body . length ,
130+ 'X- Content-MD5 ' : contentMd5 ,
131+ 'X-Content-Type ' : contentType
133132 }
134133 } )
135134
136135 switch ( res . status ) {
137136 case 204 :
138137 logger . info ( 'Image already cached...' )
139138 return `${ checksum } .${ type } `
140- case 200 :
141- assert ( res . redirected , 'image upload was not redirected' )
142- logger . info ( 'uploaded image to cache' )
139+ case 202 : {
140+ let url = res . headers . get ( 'location' )
141+ logger . debug ( { url } , 'Uploading image to cache...' )
142+
143+ res = await fetch ( url , {
144+ method : 'PUT' ,
145+ body,
146+ headers : {
147+ 'Content-MD5' : contentMd5 ,
148+ 'Content-Type' : contentType
149+ }
150+ } )
151+
152+ if ( ! res . ok ) {
153+ throw new Error (
154+ `Image upload failed: ${ res . status } ${ await res . text ( ) } ` , {
155+ cause : res
156+ } )
157+ }
158+
159+ logger . info ( 'Image uploaded to cache' )
143160 return `${ checksum } .${ type } `
161+ }
144162 default :
145163 throw new Error (
146164 `Image upload failed with status "${ res . status } ": ${ await res . text ( ) } `
0 commit comments