Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: switch to using backblaze for images #948

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"prepare": "husky install"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.715.0",
"@aws-sdk/lib-storage": "^3.715.0",
"@bull-board/express": "^4.11.0",
"@faker-js/faker": "^8.3.1",
"@prisma/client": "5",
Expand Down
172 changes: 86 additions & 86 deletions src/config/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ export default {
smartSubsample: true,
},
},
jpeg: {
ext: ".jpg",
outputOptions: {
chromaSubsampling: "4:4:4",
optimiseCoding: true,
quantisationTable: 3,
progressive: false,
optimiseScans: false, // required mozjpeg support
trellisQuantisation: true, // required mozjpeg support
overshootDeringing: true, // required mozjpeg support
},
},
png: {
ext: ".png",
outputOptions: {
chromaSubsampling: "4:4:4",
optimiseCoding: true,
quantisationTable: 3,
progressive: false,
optimiseScans: false, // required mozjpeg support
trellisQuantisation: true, // required mozjpeg support
overshootDeringing: true, // required mozjpeg support
},
},
// jpeg: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm getting rid of these because they're not getting used in the UI, and I can't quite imagine why they'd get used, and we can always generate them in the future?

// ext: ".jpg",
// outputOptions: {
// chromaSubsampling: "4:4:4",
// optimiseCoding: true,
// quantisationTable: 3,
// progressive: false,
// optimiseScans: false, // required mozjpeg support
// trellisQuantisation: true, // required mozjpeg support
// overshootDeringing: true, // required mozjpeg support
// },
// },
// png: {
// ext: ".png",
// outputOptions: {
// chromaSubsampling: "4:4:4",
// optimiseCoding: true,
// quantisationTable: 3,
// progressive: false,
// optimiseScans: false, // required mozjpeg support
// trellisQuantisation: true, // required mozjpeg support
// overshootDeringing: true, // required mozjpeg support
// },
// },
},
config: {
banner: {
Expand All @@ -47,24 +47,24 @@ export default {
{ width: 625, height: 625 },
],
},
jpeg: {
variants: [
{ width: 2500, height: 2500 },
{ width: 1250, height: 1250 },
{ width: 1200, height: 630 },
{ width: 1024, height: 1024 },
{ width: 625, height: 625 },
],
},
png: {
variants: [
{ width: 2500, height: 2500 },
{ width: 1250, height: 1250 },
{ width: 1200, height: 630 },
{ width: 1024, height: 1024 },
{ width: 625, height: 625 },
],
},
// jpeg: {
// variants: [
// { width: 2500, height: 2500 },
// { width: 1250, height: 1250 },
// { width: 1200, height: 630 },
// { width: 1024, height: 1024 },
// { width: 625, height: 625 },
// ],
// },
// png: {
// variants: [
// { width: 2500, height: 2500 },
// { width: 1250, height: 1250 },
// { width: 1200, height: 630 },
// { width: 1024, height: 1024 },
// { width: 625, height: 625 },
// ],
// },
},
avatar: {
webp: {
Expand All @@ -78,28 +78,28 @@ export default {
{ width: 60, height: 60 },
],
},
jpeg: {
variants: [
{ width: 1500, height: 1500 },
{ width: 1200, height: 1200 },
{ width: 960, height: 960 },
{ width: 600, height: 600 },
{ width: 300, height: 300 },
{ width: 120, height: 120 },
{ width: 60, height: 60 },
],
},
png: {
variants: [
{ width: 1500, height: 1500 },
{ width: 1200, height: 1200 },
{ width: 960, height: 960 },
{ width: 600, height: 600 },
{ width: 300, height: 300 },
{ width: 120, height: 120 },
{ width: 60, height: 60 },
],
},
// jpeg: {
// variants: [
// { width: 1500, height: 1500 },
// { width: 1200, height: 1200 },
// { width: 960, height: 960 },
// { width: 600, height: 600 },
// { width: 300, height: 300 },
// { width: 120, height: 120 },
// { width: 60, height: 60 },
// ],
// },
// png: {
// variants: [
// { width: 1500, height: 1500 },
// { width: 1200, height: 1200 },
// { width: 960, height: 960 },
// { width: 600, height: 600 },
// { width: 300, height: 300 },
// { width: 120, height: 120 },
// { width: 60, height: 60 },
// ],
// },
},
artwork: {
webp: {
Expand All @@ -113,28 +113,28 @@ export default {
{ width: 60, height: 60 },
],
},
jpeg: {
variants: [
{ width: 1500, height: 1500 },
{ width: 1200, height: 1200 },
{ width: 960, height: 960 },
{ width: 600, height: 600 },
{ width: 300, height: 300 },
{ width: 120, height: 120 },
{ width: 60, height: 60 },
],
},
png: {
variants: [
{ width: 1500, height: 1500 },
{ width: 1200, height: 1200 },
{ width: 960, height: 960 },
{ width: 600, height: 600 },
{ width: 300, height: 300 },
{ width: 120, height: 120 },
{ width: 60, height: 60 },
],
},
// jpeg: {
// variants: [
// { width: 1500, height: 1500 },
// { width: 1200, height: 1200 },
// { width: 960, height: 960 },
// { width: 600, height: 600 },
// { width: 300, height: 300 },
// { width: 120, height: 120 },
// { width: 60, height: 60 },
// ],
// },
// png: {
// variants: [
// { width: 1500, height: 1500 },
// { width: 1200, height: 1200 },
// { width: 960, height: 960 },
// { width: 600, height: 600 },
// { width: 300, height: 300 },
// { width: 120, height: 120 },
// { width: 60, height: 60 },
// ],
// },
},
},
};
88 changes: 53 additions & 35 deletions src/jobs/optimize-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { Job } from "bullmq";
import { uniq } from "lodash";
import {
createBucketIfNotExists,
getBufferFromMinio,
getBufferFromStorage,
minioClient,
removeObjectFromStorage,
uploadWrapper,
} from "../utils/minio";
import prisma from "@mirlo/prisma";
import { logger } from "./queue-worker";
Expand All @@ -17,13 +19,13 @@ import sendMail from "./send-mail";

const { defaultOptions, config: sharpConfig } = tempSharpConfig;

const {
MINIO_HOST = "",
MINIO_ROOT_USER = "",
MINIO_API_PORT = 9000,
SIGHTENGINE_USER,
SIGHTENGINE_SECRET,
} = process.env;
const { SIGHTENGINE_USER, SIGHTENGINE_SECRET } = process.env;

const sleep = (ms: number) =>
new Promise((resolve) => {
console.log("sleeping", ms);
return setTimeout(resolve, ms);
});

/**
* Convert and optimize track artworks to mozjpeg and webp
Expand All @@ -40,42 +42,53 @@ const optimizeImage = async (job: Job) => {

try {
const profiler = logger.startTimer();
logger.info(
`MinIO is at ${MINIO_HOST}:${MINIO_API_PORT} ${MINIO_ROOT_USER}`
);

logger.info(`Starting to optimize images ${model}/${destinationId}`);
const { buffer, size } = await getBufferFromMinio(
const { buffer } = await getBufferFromStorage(
minioClient,
incomingMinioBucket,
destinationId,
logger
destinationId
);

await createBucketIfNotExists(minioClient, finalMinioBucket, logger);

logger.info(`Got object of size ${size}`);
// logger.info(`Got object of size ${size}`);
const promises = Object.entries(config)
.map(([key, value]) => {
const outputType = key as "webp" | "jpeg" | "png"; // output type (jpeg, webp)
.map(([key, value], i) => {
const outputType = key as "webp"; // output type (jpeg, webp)
const {
// @ts-ignore
options = {},
// @ts-ignore
variants = [],
// @ts-ignore
ext = defaultOptions[outputType].ext,
} = value;
} = value as {
options: {
[key: string]: unknown;
};
variants: { width: number; height: number }[];
};

return variants.map(
async (variant: {
extract?: any;
resize?: any;
outputOptions?: any;
blur?: any;
width?: number;
height?: number;
suffix?: any;
}) => {
async (
variant: {
extract?: any;
resize?: any;
outputOptions?: any;
blur?: any;
width?: number;
height?: number;
suffix?: any;
},
j
) => {
// This is a pretty hacky way of sleeping and probably something we shoudl
// not do?
// But basically the reason we have to do this is because backblaze
// seems to not like it when we bombard it with files all in one go,
// so when we do Promise.all at the end of this we're sending all the
// files and it just doesn't process anything but the first one.
await sleep((i + 1) * (j + 1) * 1500);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having to sleep in these promises because if I don't then backblaze seems to get overwhelmed and returns an error 408

const { width, height, suffix = `-x${width}` } = variant;

const finalFileName = `${destinationId}${suffix}${ext}`;
Expand Down Expand Up @@ -110,11 +123,14 @@ const optimizeImage = async (job: Job) => {
);

logger.info("Uploading image to bucket");
await minioClient.putObject(
finalMinioBucket,
finalFileName,
newBuffer
);
await uploadWrapper(finalMinioBucket, finalFileName, newBuffer, {
contentType: `image/${outputType}`,
});
// await minioClient.putObject(
// finalMinioBucket,
// finalFileName,
// newBuffer
// );

logger.info(`Converted and optimized image to ${outputType}`, {
ratio: `${width}x${height})`,
Expand All @@ -134,7 +150,7 @@ const optimizeImage = async (job: Job) => {

const results = await Promise.all(promises);
const urls = uniq(
results.map((r: { width: number }) => `${destinationId}-x${r.width}`)
results.map((r?: { width?: number }) => `${destinationId}-x${r?.width}`)
) as string[];
logger.info(`Saving URLs [${urls.join(", ")}]`);

Expand All @@ -160,6 +176,8 @@ const optimizeImage = async (job: Job) => {
faviconFinalName,
sharp(buffer)
);
await uploadWrapper(finalMinioBucket, faviconFinalName, sharp(buffer));

await prisma.artistAvatar.update({
where: { id: destinationId },
data: { url: urls },
Expand All @@ -174,7 +192,7 @@ const optimizeImage = async (job: Job) => {
profiler.done({ message: "Done optimizing image" });
logger.info(`Removing from Bucket ${incomingMinioBucket}`);

await minioClient.removeObject(incomingMinioBucket, destinationId);
await removeObjectFromStorage(incomingMinioBucket, destinationId);

if (SIGHTENGINE_USER && SIGHTENGINE_SECRET) {
logger.info("Checking SightEngine");
Expand Down
Loading
Loading