Skip to content

Commit

Permalink
POST /api/v1/media
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jun 15, 2024
1 parent 99d6b54 commit 2299751
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
53 changes: 51 additions & 2 deletions src/api/v1/media.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { eq } from "drizzle-orm";
import { Hono } from "hono";
import { type Context, Hono } from "hono";
import sharp from "sharp";
import { uuidv7 } from "uuidv7-js";
import { db } from "../../db";
import { serializeMedium } from "../../entities/medium";
import type { Variables } from "../../oauth";
import { uploadThumbnail } from "../../media";
import { type Variables, scopeRequired, tokenRequired } from "../../oauth";
import { S3_BUCKET, S3_URL_BASE, s3 } from "../../s3";
import { media } from "../../schema";

const app = new Hono<{ Variables: Variables }>();

export async function postMedia(c: Context<{ Variables: Variables }>) {
const owner = c.get("token").accountOwner;
if (owner == null) {
return c.json({ error: "This method requires an authenticated user" }, 422);
}
const form = await c.req.formData();
const file = form.get("file");
if (!(file instanceof File)) {
return c.json({ error: "file is required" }, 422);
}
const description = form.get("description")?.toString();
const id = uuidv7();
const fileBuffer = await file.arrayBuffer();
const image = sharp(fileBuffer);
const fileMetadata = await image.metadata();
await s3.send(
new PutObjectCommand({
Bucket: S3_BUCKET,
Key: `media/${id}/original`,
Body: new Uint8Array(fileBuffer),
ContentType: file.type,
}),
);
const url = new URL(`media/${id}/original`, S3_URL_BASE).href;
const result = await db
.insert(media)
.values({
id,
type: file.type,
url,
width: fileMetadata.width!,
height: fileMetadata.height!,
description,
...(await uploadThumbnail(id, image)),
})
.returning();
if (result.length < 1) {
return c.json({ error: "Failed to insert media" }, 500);
}
return c.json(serializeMedium(result[0]));
}

app.post("/", tokenRequired, scopeRequired(["write:media"]), postMedia);

app.get("/:id", async (c) => {
const medium = await db.query.media.findFirst({
where: eq(media.id, c.req.param("id")),
Expand Down
51 changes: 2 additions & 49 deletions src/api/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,12 @@
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { Hono } from "hono";
import sharp from "sharp";
import { uuidv7 } from "uuidv7-js";
import { db } from "../../db";
import { serializeMedium } from "../../entities/medium";
import { uploadThumbnail } from "../../media";
import { type Variables, scopeRequired, tokenRequired } from "../../oauth";
import { S3_BUCKET, S3_URL_BASE, s3 } from "../../s3";
import { media } from "../../schema";
import { postMedia } from "../v1/media";
import instance from "./instance";

const app = new Hono<{ Variables: Variables }>();

app.route("/instance", instance);

app.post("/media", tokenRequired, scopeRequired(["write:media"]), async (c) => {
const owner = c.get("token").accountOwner;
if (owner == null) {
return c.json({ error: "This method requires an authenticated user" }, 422);
}
const form = await c.req.formData();
const file = form.get("file");
if (!(file instanceof File)) {
return c.json({ error: "file is required" }, 422);
}
const description = form.get("description")?.toString();
const id = uuidv7();
const fileBuffer = await file.arrayBuffer();
const image = sharp(fileBuffer);
const fileMetadata = await image.metadata();
await s3.send(
new PutObjectCommand({
Bucket: S3_BUCKET,
Key: `media/${id}/original`,
Body: new Uint8Array(fileBuffer),
ContentType: file.type,
}),
);
const url = new URL(`media/${id}/original`, S3_URL_BASE).href;
const result = await db
.insert(media)
.values({
id,
type: file.type,
url,
width: fileMetadata.width!,
height: fileMetadata.height!,
description,
...(await uploadThumbnail(id, image)),
})
.returning();
if (result.length < 1) {
return c.json({ error: "Failed to insert media" }, 500);
}
return c.json(serializeMedium(result[0]));
});
app.post("/media", tokenRequired, scopeRequired(["write:media"]), postMedia);

export default app;

0 comments on commit 2299751

Please sign in to comment.