Skip to content

Commit 5608468

Browse files
committed
Now returning an arrayBuffer
1 parent 0f7622f commit 5608468

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

functions/current.mts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ export default async (req: Request) => {
3535
data: false,
3636
})
3737
log('[get-tag-image] Prepared biketag payload', biketagPayload)
38-
const biketagConfig = biketag.config({
39-
aws: {
40-
region: game.awsRegion,
41-
}
42-
}, false, true)
38+
const biketagConfig = biketag.config(
39+
{
40+
aws: {
41+
region: game.awsRegion,
42+
},
43+
},
44+
false,
45+
true,
46+
)
4347

4448
const imageSource = game.awsRegion ? 'aws' : 'imgur'
4549
const currentTagResponse = await biketag.getTag(biketagPayload, { source: imageSource })
@@ -48,7 +52,7 @@ export default async (req: Request) => {
4852
const currentTag = currentTagResponse.data
4953
const data: any = currentTag
5054
const domainInfo = getDomainInfo(req)
51-
const host = imageSource ==='imgur' ? 'i.imgur.com' : biketagConfig.aws.endpoint?.replace('digitaloceanspaces.com', 'cdn.digitaloceanspaces.com')
55+
// const host = imageSource ==='imgur' ? 'i.imgur.com' : biketagConfig.aws.endpoint?.replace('digitaloceanspaces.com', 'cdn.digitaloceanspaces.com')
5256
data.imageUri = getImageSized(imageSource, data.mysteryImageUrl, biketagPayload.size)
5357

5458
log('[get-tag-image] Current tag details', {
@@ -65,20 +69,28 @@ export default async (req: Request) => {
6569
}
6670

6771
try {
68-
const body =
69-
(await axios.get(data.imageUri, {
70-
responseType: 'arraybuffer',
71-
headers: {
72-
// host,
73-
'Content-Type': imageSource ==='imgur' ? `image/jpg` : `image/webp`,
74-
},
75-
})).data
76-
77-
log('[get-tag-image] Image fetched successfully', { imageUri: data.imageUri })
78-
79-
return new Response(body, {
72+
const axiosResponse = await axios.get(data.imageUri, {
73+
responseType: 'arraybuffer',
74+
})
75+
76+
const contentType = imageSource === 'imgur' ? 'image/jpeg' : 'image/webp'
77+
78+
const arrayBuffer = axiosResponse.data.buffer.slice(
79+
axiosResponse.data.byteOffset,
80+
axiosResponse.data.byteOffset + axiosResponse.data.byteLength,
81+
)
82+
83+
log('[get-tag-image] Image fetched successfully', {
84+
imageUri: data.imageUri,
85+
contentType,
86+
})
87+
88+
return new Response(arrayBuffer, {
8089
status: 200,
81-
headers,
90+
headers: {
91+
...headers,
92+
'Content-Type': contentType,
93+
},
8294
})
8395
} catch (error) {
8496
log('[get-tag-image] Error fetching image', error, 'error')

0 commit comments

Comments
 (0)