snoww
snoww
CCConvex Community
Created by snoww on 4/11/2025 in #support-community
sharp won't work
my previous code was also broken, only actions can run under node so i had to refactor the code to take url and just return the metadata
"use node";

import { internalAction } from "./_generated/server";
import { v } from "convex/values";
import sharp from "sharp";

export const getImageMetadata = internalAction({
args: {
url: v.string(),
},
handler: async (ctx, { url }) => {
const response = await fetch(url);

if (!response.ok) throw new Error("Image not found");
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

return await sharp(buffer).metadata();
},
});
"use node";

import { internalAction } from "./_generated/server";
import { v } from "convex/values";
import sharp from "sharp";

export const getImageMetadata = internalAction({
args: {
url: v.string(),
},
handler: async (ctx, { url }) => {
const response = await fetch(url);

if (!response.ok) throw new Error("Image not found");
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

return await sharp(buffer).metadata();
},
});
12 replies
CCConvex Community
Created by snoww on 4/11/2025 in #support-community
sharp won't work
i downgraded to 0.33.5 of sharp and it worked,
12 replies
CCConvex Community
Created by snoww on 4/11/2025 in #support-community
sharp won't work
i'm on a windows system so i think this is part of the problem
12 replies
CCConvex Community
Created by snoww on 4/11/2025 in #support-community
sharp won't work
yes i did, sorry didn't say that
12 replies
CCConvex Community
Created by snoww on 4/11/2025 in #support-community
sharp won't work
function using sharp
"use node";

import { internalMutation } from "./_generated/server";
import { v } from "convex/values";
import sharp from "sharp";

export const addImageSizing = internalMutation({
args: {
itemId: v.id("closet"),
},
handler: async (ctx, { itemId }) => {
const item = await ctx.db.get(itemId);
if (!item || !item.imageId) throw new Error("Item or image not found");

const imageUrl = await ctx.storage.getUrl(item.imageId);
if (!imageUrl) throw new Error("Image URL not found");

const response = await fetch(imageUrl);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

const metadata = await sharp(buffer).metadata();

await ctx.db.patch(itemId, {
width: metadata.width,
height: metadata.height,
});
},
});
"use node";

import { internalMutation } from "./_generated/server";
import { v } from "convex/values";
import sharp from "sharp";

export const addImageSizing = internalMutation({
args: {
itemId: v.id("closet"),
},
handler: async (ctx, { itemId }) => {
const item = await ctx.db.get(itemId);
if (!item || !item.imageId) throw new Error("Item or image not found");

const imageUrl = await ctx.storage.getUrl(item.imageId);
if (!imageUrl) throw new Error("Image URL not found");

const response = await fetch(imageUrl);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

const metadata = await sharp(buffer).metadata();

await ctx.db.patch(itemId, {
width: metadata.width,
height: metadata.height,
});
},
});
12 replies