-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] taking a stab at images (broken)
- Loading branch information
Showing
11 changed files
with
715 additions
and
27 deletions.
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
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; |
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 |
---|---|---|
@@ -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); | ||
} | ||
}) | ||
|
||
|
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 |
---|---|---|
@@ -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; | ||
// }, | ||
// }); |
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
Oops, something went wrong.