Skip to content

Commit 445fda7

Browse files
committed
Updated sharp task code
1 parent f4dfde1 commit 445fda7

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

docs/guides/examples/sharp-image-processing.mdx

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default defineConfig({
4747
## Task code
4848

4949
```ts trigger/sharp-image-processing.ts
50-
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
50+
import { S3Client } from "@aws-sdk/client-s3";
51+
import { Upload } from "@aws-sdk/lib-storage";
5152
import { logger, task } from "@trigger.dev/sdk/v3";
5253
import fs from "fs/promises";
5354
import os from "os";
@@ -66,64 +67,51 @@ const r2Client = new S3Client({
6667

6768
export const sharpProcessImage = task({
6869
id: "sharp-process-image",
69-
// Only retry this task once if it fails
70-
retry: {
71-
maxAttempts: 1,
72-
},
70+
retry: { maxAttempts: 1 },
7371
run: async (payload: { imageUrl: string; watermarkUrl: string }) => {
7472
const { imageUrl, watermarkUrl } = payload;
73+
const outputPath = path.join(os.tmpdir(), `output_${Date.now()}.jpg`);
7574

76-
// Generate temporary and output file names
77-
const tempDirectory = os.tmpdir();
78-
const outputPath = path.join(tempDirectory, `output_${Date.now()}.jpg`);
79-
80-
// Fetch the image and watermark
8175
const [imageResponse, watermarkResponse] = await Promise.all([
8276
fetch(imageUrl),
8377
fetch(watermarkUrl),
8478
]);
8579
const imageBuffer = await imageResponse.arrayBuffer();
8680
const watermarkBuffer = await watermarkResponse.arrayBuffer();
8781

88-
// Process the image using Sharp
8982
await sharp(Buffer.from(imageBuffer))
90-
.resize(800, 800) // Resize the image to 800x800
83+
.resize(800, 800) // Resize the image to 800x800px
9184
.composite([
9285
{
9386
input: Buffer.from(watermarkBuffer),
9487
gravity: "southeast", // Position the watermark in the bottom-right corner
9588
},
9689
])
97-
.toFormat("jpeg")
98-
.toFile(outputPath);
99-
100-
logger.log(`Image processed.`);
101-
102-
// Read the processed image file
103-
const processedImageBuffer = await fs.readFile(outputPath);
104-
105-
// Upload the image to R2, replacing slashes with underscores
106-
const r2Key = `processed-images/${path.basename(outputPath)}`;
107-
108-
const uploadParams = {
109-
Bucket: process.env.R2_BUCKET,
110-
Key: r2Key,
111-
Body: processedImageBuffer,
112-
};
113-
114-
// Upload the image to R2 and get the URL
115-
await r2Client.send(new PutObjectCommand(uploadParams));
116-
logger.log("Processed image uploaded to R2", {
117-
path: `/${process.env.R2_BUCKET}/${r2Key}`,
118-
});
119-
120-
// Delete the temporary file
121-
await fs.unlink(outputPath);
122-
logger.log("Temporary file deleted", { outputPath: outputPath });
123-
124-
return {
125-
message: `New image uploaded to /${process.env.R2_BUCKET}/${r2Key}`,
126-
};
90+
.jpeg() // Convert to jpeg
91+
.toBuffer() // Convert to buffer
92+
.then(async (outputBuffer) => {
93+
await fs.writeFile(outputPath, outputBuffer); // Write the buffer to file
94+
95+
const r2Key = `processed-images/${path.basename(outputPath)}`;
96+
const uploadParams = {
97+
Bucket: process.env.R2_BUCKET,
98+
Key: r2Key,
99+
Body: await fs.readFile(outputPath),
100+
};
101+
102+
const upload = new Upload({
103+
client: r2Client,
104+
params: uploadParams,
105+
});
106+
107+
await upload.done();
108+
logger.log("Image uploaded to R2 storage.", {
109+
path: `/${process.env.R2_BUCKET}/${r2Key}`,
110+
});
111+
112+
await fs.unlink(outputPath); // Clean up the temporary file
113+
return { r2Key };
114+
});
127115
},
128116
});
129117
```

0 commit comments

Comments
 (0)