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 { createEvent } from '@/utils/web.ts';
|
||||||
import { renderEventAccounts } from '@/views.ts';
|
import { renderEventAccounts } from '@/views.ts';
|
||||||
import { insertUser } from '@/db/users.ts';
|
import { insertUser } from '@/db/users.ts';
|
||||||
|
import { uploadFile } from '@/upload.ts';
|
||||||
|
|
||||||
const usernameSchema = z
|
const usernameSchema = z
|
||||||
.string().min(1).max(30)
|
.string().min(1).max(30)
|
||||||
|
@ -171,13 +172,24 @@ const updateCredentialsController: AppController = async (c) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const author = await getAuthor(pubkey);
|
const author = await getAuthor(pubkey);
|
||||||
if (!author) {
|
const meta = author ? jsonMetaContentSchema.parse(author.content) : {};
|
||||||
return c.json({ error: 'Could not find user.' }, 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
const meta = jsonMetaContentSchema.parse(author.content);
|
const {
|
||||||
meta.name = result.data.display_name ?? meta.name;
|
avatar: avatarFile,
|
||||||
meta.about = result.data.note ?? meta.about;
|
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({
|
const event = await createEvent({
|
||||||
kind: 0,
|
kind: 0,
|
||||||
|
|
|
@ -1,19 +1,13 @@
|
||||||
import { AppController } from '@/app.ts';
|
import { AppController } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
|
||||||
import { insertUnattachedMedia } from '@/db/unattached-media.ts';
|
|
||||||
import { z } from '@/deps.ts';
|
import { z } from '@/deps.ts';
|
||||||
import { fileSchema } from '@/schema.ts';
|
import { fileSchema } from '@/schema.ts';
|
||||||
import { configUploader as uploader } from '@/uploaders/config.ts';
|
|
||||||
import { parseBody } from '@/utils/web.ts';
|
import { parseBody } from '@/utils/web.ts';
|
||||||
import { renderAttachment } from '@/views/attachment.ts';
|
import { renderAttachment } from '@/views/attachment.ts';
|
||||||
|
import { uploadFile } from '@/upload.ts';
|
||||||
const uploadSchema = fileSchema
|
|
||||||
.refine((file) => !!file.type, 'File type is required.')
|
|
||||||
.refine((file) => file.size <= Conf.maxUploadSize, 'File size is too large.');
|
|
||||||
|
|
||||||
const mediaBodySchema = z.object({
|
const mediaBodySchema = z.object({
|
||||||
file: uploadSchema,
|
file: fileSchema,
|
||||||
thumbnail: uploadSchema.optional(),
|
thumbnail: fileSchema.optional(),
|
||||||
description: z.string().optional(),
|
description: z.string().optional(),
|
||||||
focus: z.string().optional(),
|
focus: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
@ -27,21 +21,7 @@ const mediaController: AppController = async (c) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { file, description } = result.data;
|
const { file, description } = result.data;
|
||||||
const { cid } = await uploader.upload(file);
|
const media = await uploadFile(file, { pubkey: c.get('pubkey')!, description });
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return c.json(renderAttachment(media));
|
return c.json(renderAttachment(media));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|
|
@ -34,7 +34,7 @@ const hashtagTimelineController: AppController = (c) => {
|
||||||
|
|
||||||
/** Render statuses for timelines. */
|
/** Render statuses for timelines. */
|
||||||
async function renderStatuses(c: AppContext, filters: DittoFilter<1>[]) {
|
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) {
|
if (!events.length) {
|
||||||
return c.json([]);
|
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