Skip to content

Commit

Permalink
feat(store): export list of supported image content types
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed Jul 4, 2023
1 parent e461e23 commit f6cc296
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/store/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
objectStorageGetObjectStream,
} from "./src/object-storage.js";
export {
STORE_FILE_IMAGE_TYPES,
fileCreateOrUpdate,
fileTransformInPlace,
fileFormatMetadata,
Expand Down
1 change: 1 addition & 0 deletions packages/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export {
} from "./src/object-storage.js";

export {
STORE_FILE_IMAGE_TYPES,
fileCreateOrUpdate,
fileTransformInPlace,
fileFormatMetadata,
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/file.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function fileVerifyAccessToken(options: {
signingKey: string;
expectedFileId: string;
}): void;
export const TRANSFORMED_CONTENT_TYPES: string[];
export const STORE_FILE_IMAGE_TYPES: string[];
/**
* The various options supported by {@link fileTransformInPlace }.
* By default transforms SVG input in to PNG. This can't be disabled, skip calling this
Expand Down
16 changes: 13 additions & 3 deletions packages/store/src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import {
import { query } from "./query.js";
import { queueWorkerAddJob } from "./queue-worker.js";

export const TRANSFORMED_CONTENT_TYPES = [
export const STORE_FILE_IMAGE_TYPES = [
"image/png",
"image/jpeg",
"image/jpg",
"image/webp",
"image/avif",
"image/gif",
"image/svg+xml",
];

/**
Expand Down Expand Up @@ -108,7 +109,10 @@ export async function fileCreateOrUpdate(
source = createReadStream(source);
}

if (options.fileTransformInPlaceOptions) {
if (
options.fileTransformInPlaceOptions &&
STORE_FILE_IMAGE_TYPES.includes(props.contentType ?? "")
) {
const fileBuffer = Buffer.isBuffer(source)
? source
: await streamToBuffer(source);
Expand All @@ -117,6 +121,12 @@ export async function fileCreateOrUpdate(
fileBuffer,
options.fileTransformInPlaceOptions,
);

if (props.contentType === "image/svg+xml") {
// After the transform, svgs are converted as png's
// See Sharp's toBuffer docs for more information.
props.contentType = "image/png";
}
}

const upload = new Upload({
Expand Down Expand Up @@ -145,7 +155,7 @@ export async function fileCreateOrUpdate(

if (
options.schedulePlaceholderImageJob &&
TRANSFORMED_CONTENT_TYPES.includes(props.contentType ?? "")
STORE_FILE_IMAGE_TYPES.includes(props.contentType ?? "")
) {
await queueWorkerAddJob(sql, {
name: "compas.file.generatePlaceholderImage",
Expand Down
4 changes: 2 additions & 2 deletions packages/store/src/files-jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import sharp from "sharp";
import {
fileCreateOrUpdate,
fileSyncDeletedWithObjectStorage,
TRANSFORMED_CONTENT_TYPES,
STORE_FILE_IMAGE_TYPES,
} from "./file.js";
import { queryFile } from "./generated/database/file.js";
import { objectStorageGetObjectStream } from "./object-storage.js";
Expand Down Expand Up @@ -70,7 +70,7 @@ export function jobFileGeneratePlaceholderImage(s3Client, bucketName) {
}).execRaw(sql);

// @ts-expect-error
if (!TRANSFORMED_CONTENT_TYPES.includes(file?.contentType)) {
if (!STORE_FILE_IMAGE_TYPES.includes(file?.contentType)) {
// not supported
eventStop(event);
return;
Expand Down

0 comments on commit f6cc296

Please sign in to comment.