in source/image-handler/image-request.ts [103:143]
public async getOriginalImage(bucket: string, key: string): Promise<OriginalImageInfo> {
try {
const result: OriginalImageInfo = {};
const imageLocation = { Bucket: bucket, Key: key };
const originalImage = await this.s3Client.getObject(imageLocation).promise();
const imageBuffer = Buffer.from(originalImage.Body as Uint8Array);
if (originalImage.ContentType) {
// If using default S3 ContentType infer from hex headers
if (['binary/octet-stream', 'application/octet-stream'].includes(originalImage.ContentType)) {
result.contentType = this.inferImageType(imageBuffer);
} else {
result.contentType = originalImage.ContentType;
}
} else {
result.contentType = 'image';
}
if (originalImage.Expires) {
result.expires = new Date(originalImage.Expires).toUTCString();
}
if (originalImage.LastModified) {
result.lastModified = new Date(originalImage.LastModified).toUTCString();
}
result.cacheControl = originalImage.CacheControl ?? 'max-age=31536000,public';
result.originalImage = imageBuffer;
return result;
} catch (error) {
let status = StatusCodes.INTERNAL_SERVER_ERROR;
let message = error.message;
if (error.code === 'NoSuchKey') {
status = StatusCodes.NOT_FOUND;
message = `The image ${key} does not exist or the request may not be base64 encoded properly.`;
}
throw new ImageHandlerError(status, error.code, message);
}
}