Skip to content

Commit

Permalink
[wip] taking a stab at images (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
vakila committed Aug 27, 2024
1 parent a25f951 commit c250048
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 27 deletions.
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type * as auth from "../auth.js";
import type * as crons from "../crons.js";
import type * as data from "../data.js";
import type * as http from "../http.js";
import type * as images from "../images.js";
import type * as posts from "../posts.js";
import type * as users from "../users.js";
import type * as versions from "../versions.js";
Expand All @@ -35,6 +36,7 @@ declare const fullApi: ApiFromModules<{
crons: typeof crons;
data: typeof data;
http: typeof http;
images: typeof images;
posts: typeof posts;
users: typeof users;
versions: typeof versions;
Expand Down
16 changes: 16 additions & 0 deletions convex/http.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { httpRouter } from "convex/server";
import { auth } from "./auth";
import { httpAction } from "./_generated/server";

const http = httpRouter();

auth.addHttpRoutes(http);


http.route({
path: '/images/upload',
method: "GET",
handler: httpAction(async (ctx, request) => {
const { body: { storageId } } = await request.json();

console.log(`received request.body.storageId ${storageId}`)

return new Response(null, {
status: 200,
});
})
});

export default http;
42 changes: 42 additions & 0 deletions convex/images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import { v } from "convex/values";
import { mutation } from "./_generated/server";
import { viewer as getViewer } from "./users";

export const generateUploadUrl = mutation({
args: {},
handler: async (ctx) => {
// Verify the user is authenticated
const viewer = await getViewer(ctx, {});
if (!viewer) throw new Error('User not authenticated; cannot generate upload URL')

// Return an upload URL
return await ctx.storage.generateUploadUrl();
},
});

export const save = mutation({
args: {
storageId: v.id('_storage'),
authorId: v.id('users'),
name: v.string(),
type: v.string(),
size: v.number()
},
handler: async (ctx, args) => {
// Verify the user is still authenticated
const viewer = await getViewer(ctx, {});
if (!viewer)
throw new Error('User not authenticated; cannot save file metadata')

const url = await ctx.storage.getUrl(args.storageId);
if (!url)
throw new Error(`Could not find storageId ${args.storageId}`)

// Save the file metadata, url & storageId to 'files' table
const docId = await ctx.db.insert('images', { ...args, url });
return await ctx.db.get(docId);
}
})


26 changes: 26 additions & 0 deletions convex/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// "use node";

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



// export const resize = internalAction({
// args: { storageId: v.id("_storage") },
// handler: async (ctx, args) => {
// const data = await ctx.storage.get(args.storageId);
// if (data === null) {
// throw new Error("Image not found");
// }
// const buffer = await data.arrayBuffer();

// const small = await sharp(buffer).resize(100).webp().toBuffer();
// const smallId = await ctx.storage.store(new Blob([small], { type: "image/webp" }));

// const storageIds = {
// small: smallId,
// };
// return storageIds;
// },
// });
11 changes: 10 additions & 1 deletion convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export const usersZod = {
}
export const users = Table('users', zodToConvexFields(usersZod));

export const imagesZod = {
name: zodOptionalString(),
storageId: zid('_storage'),
url: z.string().url()
}
export const images = Table('images', zodToConvexFields(imagesZod))

//// Convex DB ////
export default defineSchema({
...authTables,
Expand All @@ -82,5 +89,7 @@ export default defineSchema({
.index("by_authorId", ["authorId"]),
versions: versions.table
.index("by_postId", ["postId"])
.index("by_slug", ["slug"]) // to lookup old slugs for redirects
.index("by_slug", ["slug"]), // to lookup old slugs for redirects
images: images.table
.index("by_storageID", ["storageId"])
});
16 changes: 11 additions & 5 deletions convex/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
"jsx": "react-jsx",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,

/* These compiler options are required by Convex */
"target": "ESNext",
"lib": ["ES2021", "dom"],
"lib": [
"ES2021",
"dom"
],
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"isolatedModules": true,
"noEmit": true
},
"include": ["./**/*"],
"exclude": ["./_generated"]
}
"include": [
"./**/*"
],
"exclude": [
"./_generated"
]
}
Loading

0 comments on commit c250048

Please sign in to comment.