Move uploader.ts to utils, make it kind of like api.ts

This commit is contained in:
Alex Gleason 2024-05-18 22:04:43 -05:00
parent 82c03dcb56
commit 6542d6a777
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 15 additions and 17 deletions

View File

@ -8,7 +8,7 @@ import { getAuthor, getFollowedPubkeys } from '@/queries.ts';
import { booleanParamSchema, fileSchema } from '@/schema.ts'; import { booleanParamSchema, fileSchema } from '@/schema.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { addTag, deleteTag, findReplyTag, getTagSet } from '@/tags.ts'; import { addTag, deleteTag, findReplyTag, getTagSet } from '@/tags.ts';
import { uploadFile } from '@/upload.ts'; import { uploadFile } from '@/utils/upload.ts';
import { nostrNow } from '@/utils.ts'; import { nostrNow } from '@/utils.ts';
import { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts'; import { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts';
import { lookupAccount } from '@/utils/lookup.ts'; import { lookupAccount } from '@/utils/lookup.ts';
@ -202,7 +202,6 @@ const updateCredentialsSchema = z.object({
const updateCredentialsController: AppController = async (c) => { const updateCredentialsController: AppController = async (c) => {
const pubkey = await c.get('signer')?.getPublicKey()!; const pubkey = await c.get('signer')?.getPublicKey()!;
const uploader = c.get('uploader');
const body = await parseBody(c.req.raw); const body = await parseBody(c.req.raw);
const result = updateCredentialsSchema.safeParse(body); const result = updateCredentialsSchema.safeParse(body);
@ -221,13 +220,9 @@ const updateCredentialsController: AppController = async (c) => {
nip05, nip05,
} = result.data; } = result.data;
if ((avatarFile || headerFile) && !uploader) {
return c.json({ error: 'No uploader configured.' }, 500);
}
const [avatar, header] = await Promise.all([ const [avatar, header] = await Promise.all([
(avatarFile && uploader) ? uploadFile(uploader, avatarFile, { pubkey }) : undefined, avatarFile ? uploadFile(c, avatarFile, { pubkey }) : undefined,
(headerFile && uploader) ? uploadFile(uploader, headerFile, { pubkey }) : undefined, headerFile ? uploadFile(c, headerFile, { pubkey }) : undefined,
]); ]);
meta.name = display_name ?? meta.name; meta.name = display_name ?? meta.name;

View File

@ -4,7 +4,7 @@ import { AppController } from '@/app.ts';
import { fileSchema } from '@/schema.ts'; import { fileSchema } from '@/schema.ts';
import { parseBody } from '@/utils/api.ts'; import { parseBody } from '@/utils/api.ts';
import { renderAttachment } from '@/views/mastodon/attachments.ts'; import { renderAttachment } from '@/views/mastodon/attachments.ts';
import { uploadFile } from '@/upload.ts'; import { uploadFile } from '@/utils/upload.ts';
const mediaBodySchema = z.object({ const mediaBodySchema = z.object({
file: fileSchema, file: fileSchema,
@ -14,11 +14,6 @@ const mediaBodySchema = z.object({
}); });
const mediaController: AppController = async (c) => { const mediaController: AppController = async (c) => {
const uploader = c.get('uploader');
if (!uploader) {
return c.json({ error: 'No uploader configured.' }, 500);
}
const pubkey = await c.get('signer')?.getPublicKey()!; const pubkey = await c.get('signer')?.getPublicKey()!;
const result = mediaBodySchema.safeParse(await parseBody(c.req.raw)); const result = mediaBodySchema.safeParse(await parseBody(c.req.raw));
const { signal } = c.req.raw; const { signal } = c.req.raw;
@ -29,7 +24,7 @@ const mediaController: AppController = async (c) => {
try { try {
const { file, description } = result.data; const { file, description } = result.data;
const media = await uploadFile(uploader, file, { pubkey, description }, signal); const media = await uploadFile(c, file, { pubkey, description }, signal);
return c.json(renderAttachment(media)); return c.json(renderAttachment(media));
} catch (e) { } catch (e) {
console.error(e); console.error(e);

View File

@ -1,6 +1,7 @@
import { AppContext } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { insertUnattachedMedia, UnattachedMedia } from '@/db/unattached-media.ts'; import { insertUnattachedMedia, UnattachedMedia } from '@/db/unattached-media.ts';
import { DittoUploader } from '@/interfaces/DittoUploader.ts'; import { HTTPException } from 'hono';
interface FileMeta { interface FileMeta {
pubkey: string; pubkey: string;
description?: string; description?: string;
@ -8,11 +9,18 @@ interface FileMeta {
/** Upload a file, track it in the database, and return the resulting media object. */ /** Upload a file, track it in the database, and return the resulting media object. */
export async function uploadFile( export async function uploadFile(
uploader: DittoUploader, c: AppContext,
file: File, file: File,
meta: FileMeta, meta: FileMeta,
signal?: AbortSignal, signal?: AbortSignal,
): Promise<UnattachedMedia> { ): Promise<UnattachedMedia> {
const uploader = c.get('uploader');
if (!uploader) {
throw new HTTPException(500, {
res: c.json({ error: 'No uploader configured.' }),
});
}
const { pubkey, description } = meta; const { pubkey, description } = meta;
if (file.size > Conf.maxUploadSize) { if (file.size > Conf.maxUploadSize) {