-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`; | ||
|
@@ -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})`, | ||
|
@@ -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(", ")}]`); | ||
|
||
|
@@ -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 }, | ||
|
@@ -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"); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?