feat: heic support
This commit is contained in:
+53
-1
@@ -1,4 +1,4 @@
|
||||
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"
|
||||
import { DeleteObjectCommand, GetObjectCommand, HeadObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
|
||||
import { nanoid } from "nanoid"
|
||||
|
||||
@@ -61,6 +61,58 @@ export async function createPresignedDownloadUrl(objectKey: string) {
|
||||
)
|
||||
}
|
||||
|
||||
export async function downloadUploadObject(objectKey: string, maxSizeBytes: number) {
|
||||
const response = await getS3Client().send(new GetObjectCommand({
|
||||
Bucket: getUploadBucket(),
|
||||
Key: objectKey,
|
||||
}))
|
||||
const body = response.Body
|
||||
if (!body) throw new Error("Uploaded object has no body")
|
||||
|
||||
if ("transformToByteArray" in body && typeof body.transformToByteArray === "function") {
|
||||
const bytes = await body.transformToByteArray()
|
||||
if (bytes.byteLength > maxSizeBytes) throw new Error("Uploaded object is too large")
|
||||
return Buffer.from(bytes)
|
||||
}
|
||||
|
||||
const chunks: Buffer[] = []
|
||||
let size = 0
|
||||
for await (const chunk of body as AsyncIterable<Uint8Array | string>) {
|
||||
const buffer = Buffer.from(chunk)
|
||||
size += buffer.length
|
||||
if (size > maxSizeBytes) throw new Error("Uploaded object is too large")
|
||||
chunks.push(buffer)
|
||||
}
|
||||
return Buffer.concat(chunks, size)
|
||||
}
|
||||
|
||||
export async function putUploadObject({
|
||||
objectKey,
|
||||
contentType,
|
||||
body,
|
||||
}: {
|
||||
objectKey: string
|
||||
contentType: string
|
||||
body: Buffer
|
||||
}) {
|
||||
await getS3Client().send(new PutObjectCommand({
|
||||
Bucket: getUploadBucket(),
|
||||
Key: objectKey,
|
||||
Body: body,
|
||||
ContentType: contentType,
|
||||
ContentLength: body.length,
|
||||
}))
|
||||
}
|
||||
|
||||
export async function getUploadedObjectMetadata(objectKey: string) {
|
||||
const response = await getS3Client().send(new HeadObjectCommand({
|
||||
Bucket: getUploadBucket(),
|
||||
Key: objectKey,
|
||||
}))
|
||||
|
||||
return { contentType: response.ContentType, sizeBytes: response.ContentLength }
|
||||
}
|
||||
|
||||
export async function deleteUploadObject(objectKey: string) {
|
||||
const client = getS3Client()
|
||||
const bucket = getUploadBucket()
|
||||
|
||||
Reference in New Issue
Block a user