export const { generateUploadUrl, syncMetadata } = r2.clientApi({
checkUpload: async (ctx, bucket) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}
// TODO: check if the storage is full
},
onUpload: async (ctx, key) => {
// Problem: metadata is null here
const metadata = await r2.getMetadata(ctx, key);
console.log("Metadata in onUpload:", metadata);
const user = await ctx.runQuery(api.users.current);
if (!user || !metadata) {
return;
}
await totalStorageAggregate.insert(ctx, {
key: user._id,
id: key,
sumValue: metadata.size, // metadata.size would be undefined if metadata is null
});
},
onDelete: async (ctx, key) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}
await totalStorageAggregate.delete(ctx, {
key: user._id,
id: key,
});
},
});
export const { generateUploadUrl, syncMetadata } = r2.clientApi({
checkUpload: async (ctx, bucket) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}
// TODO: check if the storage is full
},
onUpload: async (ctx, key) => {
// Problem: metadata is null here
const metadata = await r2.getMetadata(ctx, key);
console.log("Metadata in onUpload:", metadata);
const user = await ctx.runQuery(api.users.current);
if (!user || !metadata) {
return;
}
await totalStorageAggregate.insert(ctx, {
key: user._id,
id: key,
sumValue: metadata.size, // metadata.size would be undefined if metadata is null
});
},
onDelete: async (ctx, key) => {
const user = await ctx.runQuery(api.users.current);
if (!user) {
throw new ConvexError("unauthorized");
}
await totalStorageAggregate.delete(ctx, {
key: user._id,
id: key,
});
},
});