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

View File

@ -4,7 +4,7 @@ import { AppController } from '@/app.ts';
import { fileSchema } from '@/schema.ts';
import { parseBody } from '@/utils/api.ts';
import { renderAttachment } from '@/views/mastodon/attachments.ts';
import { uploadFile } from '@/upload.ts';
import { uploadFile } from '@/utils/upload.ts';
const mediaBodySchema = z.object({
file: fileSchema,
@ -14,11 +14,6 @@ const mediaBodySchema = z.object({
});
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 result = mediaBodySchema.safeParse(await parseBody(c.req.raw));
const { signal } = c.req.raw;
@ -29,7 +24,7 @@ const mediaController: AppController = async (c) => {
try {
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));
} catch (e) {
console.error(e);

View File

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