Skip to content

Commit 2604671

Browse files
committed
WIP: Refactor to use drivemanager
1 parent 0549951 commit 2604671

File tree

8 files changed

+148
-93
lines changed

8 files changed

+148
-93
lines changed

.env.sample

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ LISTEN_PORT=3000
1212
# is implemented.
1313
ALLOW_PRIVATE_ADDRESS=false
1414
REMOTE_ACTOR_FETCH_POSTS=10
15+
16+
# File uploads and media storage:
1517
DRIVE_DISK=
16-
ASSET_URL_BASE=
17-
FS_ASSET_PATH=
18-
AWS_ACCESS_KEY_ID=
19-
AWS_SECRET_ACCESS_KEY=
20-
S3_REGION=
21-
S3_ENDPOINT_URL=
22-
S3_BUCKET=
23-
S3_FORCE_PATH_STYLE=false
18+
STORAGE_URL_BASE=
19+
20+
# If DRIVE_DISK is "fs":
21+
# FS_STORAGE_PATH=
22+
23+
# If DRIVE_DISK is "s3":
24+
# AWS_ACCESS_KEY_ID=
25+
# AWS_SECRET_ACCESS_KEY=
26+
# S3_REGION=
27+
# S3_ENDPOINT_URL=
28+
# S3_BUCKET=
29+
# S3_FORCE_PATH_STYLE=false

src/api/v1/accounts.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
pinnedPosts,
5454
posts,
5555
} from "../../schema";
56-
import { disk, getAssetUrl } from "../../storage";
56+
import { drive } from "../../storage";
5757
import { extractCustomEmojis, formatText } from "../../text";
5858
import { type Uuid, isUuid } from "../../uuid";
5959
import { timelineQuerySchema } from "./timelines";
@@ -107,6 +107,7 @@ app.patch(
107107
}),
108108
),
109109
async (c) => {
110+
const disk = drive.use();
110111
const owner = c.get("token").accountOwner;
111112
if (owner == null) {
112113
return c.json(
@@ -133,7 +134,7 @@ app.patch(
133134
contentLength: content.byteLength,
134135
visibility: "public",
135136
});
136-
avatarUrl = getAssetUrl(`${path}?${Date.now()}`, c.req.url);
137+
avatarUrl = await disk.getUrl(path);
137138
}
138139
let coverUrl = undefined;
139140
if (form.header instanceof File) {
@@ -156,7 +157,7 @@ app.patch(
156157
} catch (error) {
157158
return c.json({ error: "Failed to upload header image." }, 500);
158159
}
159-
coverUrl = getAssetUrl(`${path}?${Date.now()}`, c.req.url);
160+
coverUrl = await disk.getUrl(path);
160161
}
161162
const fedCtx = federation.createContext(c.req.raw, undefined);
162163
const fmtOpts = {

src/api/v1/media.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import { serializeMedium } from "../../entities/medium";
77
import { makeVideoScreenshot, uploadThumbnail } from "../../media";
88
import { type Variables, scopeRequired, tokenRequired } from "../../oauth";
99
import { media } from "../../schema";
10-
import { disk, getAssetUrl } from "../../storage";
10+
import { drive } from "../../storage";
1111
import { isUuid, uuidv7 } from "../../uuid";
1212

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

1515
export async function postMedia(c: Context<{ Variables: Variables }>) {
16+
const disk = drive.use();
1617
const owner = c.get("token").accountOwner;
1718
if (owner == null) {
1819
return c.json({ error: "This method requires an authenticated user" }, 422);
@@ -47,7 +48,7 @@ export async function postMedia(c: Context<{ Variables: Variables }>) {
4748
} catch (error) {
4849
return c.json({ error: "Failed to save media file" }, 500);
4950
}
50-
const url = getAssetUrl(path, c.req.url);
51+
const url = await disk.getUrl(path);
5152
const result = await db
5253
.insert(media)
5354
.values({
@@ -57,7 +58,7 @@ export async function postMedia(c: Context<{ Variables: Variables }>) {
5758
width: fileMetadata.width!,
5859
height: fileMetadata.height!,
5960
description,
60-
...(await uploadThumbnail(id, image, c.req.url)),
61+
...(await uploadThumbnail(id, image)),
6162
})
6263
.returning();
6364
if (result.length < 1) {

src/federation/post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export async function persistPost(
416416
}
417417
const image = sharp(imageBytes);
418418
metadata = await image.metadata();
419-
thumbnail = await uploadThumbnail(id, image, baseUrl);
419+
thumbnail = await uploadThumbnail(id, image);
420420
} catch {
421421
metadata = {
422422
width: attachment.width ?? 512,

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import fedi from "./federation";
1010
import image from "./image";
1111
import oauth, { oauthAuthorizationServer } from "./oauth";
1212
import pages from "./pages";
13-
import { DRIVE_DISK, assetPath } from "./storage";
13+
import { DRIVE_DISK, FS_STORAGE_PATH } from "./storage";
1414

1515
const app = new Hono();
1616

@@ -23,7 +23,7 @@ if (DRIVE_DISK === "fs") {
2323
app.use(
2424
"/assets/*",
2525
serveStatic({
26-
root: relative(process.cwd(), assetPath!),
26+
root: relative(process.cwd(), FS_STORAGE_PATH!),
2727
rewriteRequestPath: (path) => path.substring("/assets".length),
2828
}),
2929
);

src/media.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import ffmpeg from "fluent-ffmpeg";
66
import type { Sharp } from "sharp";
7-
import { disk } from "./storage";
8-
import { getAssetUrl } from "./storage";
7+
import { drive } from "./storage";
98

109
const DEFAULT_THUMBNAIL_AREA = 230_400;
1110

@@ -19,9 +18,9 @@ export interface Thumbnail {
1918
export async function uploadThumbnail(
2019
id: string,
2120
original: Sharp,
22-
url: URL | string,
2321
thumbnailArea = DEFAULT_THUMBNAIL_AREA,
2422
): Promise<Thumbnail> {
23+
const disk = drive.use();
2524
const originalMetadata = await original.metadata();
2625
let width = originalMetadata.width!;
2726
let height = originalMetadata.height!;
@@ -55,7 +54,7 @@ export async function uploadThumbnail(
5554
throw error;
5655
}
5756
return {
58-
thumbnailUrl: getAssetUrl(`media/${id}/thumbnail.webp`, url),
57+
thumbnailUrl: await disk.getUrl(`media/${id}/thumbnail.webp`),
5958
thumbnailType: "image/webp",
6059
thumbnailWidth: thumbnailSize.width,
6160
thumbnailHeight: thumbnailSize.height,

src/pages/emojis.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { DashboardLayout } from "../components/DashboardLayout";
66
import db from "../db";
77
import { loginRequired } from "../login";
88
import { accounts, customEmojis, posts, reactions } from "../schema";
9-
import { disk, getAssetUrl } from "../storage";
9+
import { drive } from "../storage";
1010

1111
const logger = getLogger(["hollo", "pages", "emojis"]);
1212

@@ -153,6 +153,7 @@ emojis.get("/new", async (c) => {
153153
});
154154

155155
emojis.post("/", async (c) => {
156+
const disk = drive.use();
156157
const form = await c.req.formData();
157158
const categoryValue = form.get("category")?.toString();
158159
const category = categoryValue?.startsWith("category:")
@@ -185,9 +186,10 @@ emojis.post("/", async (c) => {
185186
visibility: "public",
186187
});
187188
} catch (error) {
189+
console.error(error);
188190
return c.text("Failed to store emoji image", 500);
189191
}
190-
const url = getAssetUrl(path, c.req.url);
192+
const url = await disk.getUrl(path);
191193
await db.insert(customEmojis).values({
192194
category,
193195
shortcode,

0 commit comments

Comments
 (0)