Merge branch 'upload-avatar' into 'develop'
Refactor media, upload avatar See merge request soapbox-pub/ditto!43
This commit is contained in:
commit
3394474ee2
|
@ -11,6 +11,7 @@ import { paginated, paginationSchema, parseBody } from '@/utils/web.ts';
|
|||
import { createEvent } from '@/utils/web.ts';
|
||||
import { renderEventAccounts } from '@/views.ts';
|
||||
import { insertUser } from '@/db/users.ts';
|
||||
import { uploadFile } from '@/upload.ts';
|
||||
|
||||
const usernameSchema = z
|
||||
.string().min(1).max(30)
|
||||
|
@ -171,13 +172,24 @@ const updateCredentialsController: AppController = async (c) => {
|
|||
}
|
||||
|
||||
const author = await getAuthor(pubkey);
|
||||
if (!author) {
|
||||
return c.json({ error: 'Could not find user.' }, 404);
|
||||
}
|
||||
const meta = author ? jsonMetaContentSchema.parse(author.content) : {};
|
||||
|
||||
const meta = jsonMetaContentSchema.parse(author.content);
|
||||
meta.name = result.data.display_name ?? meta.name;
|
||||
meta.about = result.data.note ?? meta.about;
|
||||
const {
|
||||
avatar: avatarFile,
|
||||
header: headerFile,
|
||||
display_name,
|
||||
note,
|
||||
} = result.data;
|
||||
|
||||
const [avatar, header] = await Promise.all([
|
||||
avatarFile ? uploadFile(avatarFile, { pubkey }) : undefined,
|
||||
headerFile ? uploadFile(headerFile, { pubkey }) : undefined,
|
||||
]);
|
||||
|
||||
meta.name = display_name ?? meta.name;
|
||||
meta.about = note ?? meta.about;
|
||||
meta.picture = avatar?.url ?? meta.picture;
|
||||
meta.banner = header?.url ?? meta.banner;
|
||||
|
||||
const event = await createEvent({
|
||||
kind: 0,
|
||||
|
|
|
@ -1,19 +1,13 @@
|
|||
import { AppController } from '@/app.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { insertUnattachedMedia } from '@/db/unattached-media.ts';
|
||||
import { z } from '@/deps.ts';
|
||||
import { fileSchema } from '@/schema.ts';
|
||||
import { configUploader as uploader } from '@/uploaders/config.ts';
|
||||
import { parseBody } from '@/utils/web.ts';
|
||||
import { renderAttachment } from '@/views/attachment.ts';
|
||||
|
||||
const uploadSchema = fileSchema
|
||||
.refine((file) => !!file.type, 'File type is required.')
|
||||
.refine((file) => file.size <= Conf.maxUploadSize, 'File size is too large.');
|
||||
import { uploadFile } from '@/upload.ts';
|
||||
|
||||
const mediaBodySchema = z.object({
|
||||
file: uploadSchema,
|
||||
thumbnail: uploadSchema.optional(),
|
||||
file: fileSchema,
|
||||
thumbnail: fileSchema.optional(),
|
||||
description: z.string().optional(),
|
||||
focus: z.string().optional(),
|
||||
});
|
||||
|
@ -27,21 +21,7 @@ const mediaController: AppController = async (c) => {
|
|||
|
||||
try {
|
||||
const { file, description } = result.data;
|
||||
const { cid } = await uploader.upload(file);
|
||||
|
||||
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();
|
||||
|
||||
const media = await insertUnattachedMedia({
|
||||
pubkey: c.get('pubkey')!,
|
||||
url,
|
||||
data: {
|
||||
name: file.name,
|
||||
mime: file.type,
|
||||
size: file.size,
|
||||
description,
|
||||
},
|
||||
});
|
||||
|
||||
const media = await uploadFile(file, { pubkey: c.get('pubkey')!, description });
|
||||
return c.json(renderAttachment(media));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
|
|
@ -34,7 +34,7 @@ const hashtagTimelineController: AppController = (c) => {
|
|||
|
||||
/** Render statuses for timelines. */
|
||||
async function renderStatuses(c: AppContext, filters: DittoFilter<1>[]) {
|
||||
const events = await mixer.getFilters(filters, { timeout: Time.seconds(3) });
|
||||
const events = await mixer.getFilters(filters, { timeout: Time.seconds(1) });
|
||||
|
||||
if (!events.length) {
|
||||
return c.json([]);
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
import { Conf } from '@/config.ts';
|
||||
import { insertUnattachedMedia } from '@/db/unattached-media.ts';
|
||||
import { configUploader as uploader } from '@/uploaders/config.ts';
|
||||
|
||||
interface FileMeta {
|
||||
pubkey: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
/** Upload a file, track it in the database, and return the resulting media object. */
|
||||
async function uploadFile(file: File, meta: FileMeta) {
|
||||
const { name, type, size } = file;
|
||||
const { pubkey, description } = meta;
|
||||
|
||||
if (file.size > Conf.maxUploadSize) {
|
||||
throw new Error('File size is too large.');
|
||||
}
|
||||
|
||||
const { cid } = await uploader.upload(file);
|
||||
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();
|
||||
|
||||
return insertUnattachedMedia({
|
||||
pubkey,
|
||||
url,
|
||||
data: {
|
||||
name,
|
||||
size,
|
||||
description,
|
||||
mime: type,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export { uploadFile };
|
Loading…
Reference in New Issue