Skip to content

Commit b5d7cbc

Browse files
committed
Handle upload redirect transparently
Using HTTP 100 Continue
1 parent ff959ea commit b5d7cbc

1 file changed

Lines changed: 11 additions & 22 deletions

File tree

src/plugin.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,47 +104,36 @@ export default class ArtimiPlugin {
104104

105105
// TODO use PNG if image uses alpha
106106
let type = 'jpeg'
107-
let image = await sharp.toBuffer('jpeg', buffer, { raw })
107+
let body = await sharp.toBuffer('jpeg', buffer, { raw })
108108

109-
let checksum = md5(image)
109+
let checksum = md5(body)
110110
let contentMd5 = Buffer.from(checksum, 'hex').toString('base64')
111111
let contentType = `image/${type}`
112112

113113
let res = await fetch(`${host}/uploads/${checksum}.${type}`, {
114114
method: 'PUT',
115-
redirect: 'manual',
115+
redirect: 'follow',
116+
body,
116117
headers: {
117118
'Authorization': `Bearer ${token}`,
118-
'X-Content-Length': image.length,
119119
'Content-MD5': contentMd5,
120-
'Content-Type': contentType
120+
'Content-Type': contentType,
121+
'Expect': '100-continue'
121122
}
122123
})
123124

124125
switch (res.status) {
125126
case 204:
126127
logger.info('Image already cached...')
127128
return `${checksum}.${type}`
128-
case 202:
129-
case 307:
130-
logger.info({
131-
location: res.headers.get('location')
132-
}, 'Upload image to cache...')
133-
res = await fetch(res.headers.get('location'), {
134-
method: 'PUT',
135-
body: image,
136-
headers: {
137-
'Content-MD5': contentMd5,
138-
'Content-Type': contentType
139-
}
140-
})
141-
if (!res.ok) {
142-
throw new Error(await res.text())
143-
}
129+
case 200:
130+
assert(res.redirected, 'Image upload was not redirected')
131+
logger.info('Uploaded image to cache...')
144132
return `${checksum}.${type}`
145133
default:
146134
throw new Error(
147-
`Image upload failed with status "${res.status}": ${await res.text()}`)
135+
`Image upload failed with status "${res.status}": ${await res.text()}`
136+
)
148137
}
149138
}
150139

0 commit comments

Comments
 (0)