mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Add unattached_media table, insert one when uploading a file
This commit is contained in:
parent
c6b20e68f6
commit
c88b174d02
5 changed files with 86 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { AppController } from '@/app.ts';
|
import { AppController } from '@/app.ts';
|
||||||
import { Conf } from '@/config.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 { configUploader as uploader } from '@/uploaders/config.ts';
|
||||||
|
|
@ -27,6 +28,17 @@ const mediaController: AppController = async (c) => {
|
||||||
const { file, description } = result.data;
|
const { file, description } = result.data;
|
||||||
const { cid } = await uploader.upload(file);
|
const { cid } = await uploader.upload(file);
|
||||||
|
|
||||||
|
await insertUnattachedMedia({
|
||||||
|
pukey: c.get('pubkey')!,
|
||||||
|
cid,
|
||||||
|
data: {
|
||||||
|
name: file.name,
|
||||||
|
mime: file.type,
|
||||||
|
size: file.size,
|
||||||
|
description,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();
|
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ interface DittoDB {
|
||||||
tags: TagRow;
|
tags: TagRow;
|
||||||
users: UserRow;
|
users: UserRow;
|
||||||
relays: RelayRow;
|
relays: RelayRow;
|
||||||
|
unattached_media: UnattachedMediaRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EventRow {
|
interface EventRow {
|
||||||
|
|
@ -46,6 +47,14 @@ interface RelayRow {
|
||||||
active: boolean;
|
active: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface UnattachedMediaRow {
|
||||||
|
id: string;
|
||||||
|
pukey: string;
|
||||||
|
cid: string;
|
||||||
|
data: string;
|
||||||
|
uploaded_at: Date;
|
||||||
|
}
|
||||||
|
|
||||||
const db = new Kysely<DittoDB>({
|
const db = new Kysely<DittoDB>({
|
||||||
dialect: new DenoSqliteDialect({
|
dialect: new DenoSqliteDialect({
|
||||||
database: new Sqlite(Conf.dbPath),
|
database: new Sqlite(Conf.dbPath),
|
||||||
|
|
|
||||||
34
src/db/migrations/007_unattached_media.ts
Normal file
34
src/db/migrations/007_unattached_media.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { Kysely, sql } from '@/deps.ts';
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable('unattached_media')
|
||||||
|
.addColumn('id', 'text', (c) => c.primaryKey())
|
||||||
|
.addColumn('pukey', 'text', (c) => c.notNull())
|
||||||
|
.addColumn('cid', 'text', (c) => c.notNull())
|
||||||
|
.addColumn('data', 'text', (c) => c.notNull())
|
||||||
|
.addColumn('uploaded_at', 'datetime', (c) => c.notNull().defaultTo(sql`CURRENT_TIMESTAMP`))
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('unattached_media_id')
|
||||||
|
.on('unattached_media')
|
||||||
|
.column('id')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('unattached_media_pukey')
|
||||||
|
.on('unattached_media')
|
||||||
|
.column('pukey')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('unattached_media_cid')
|
||||||
|
.on('unattached_media')
|
||||||
|
.column('cid')
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable('unattached_media').execute();
|
||||||
|
}
|
||||||
30
src/db/unattached-media.ts
Normal file
30
src/db/unattached-media.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { db } from '@/db.ts';
|
||||||
|
import { uuid62 } from '@/deps.ts';
|
||||||
|
|
||||||
|
interface UnattachedMedia {
|
||||||
|
id: string;
|
||||||
|
pukey: string;
|
||||||
|
cid: string;
|
||||||
|
data: {
|
||||||
|
name?: string;
|
||||||
|
mime?: string;
|
||||||
|
width?: number;
|
||||||
|
height?: number;
|
||||||
|
size?: number;
|
||||||
|
description?: string;
|
||||||
|
};
|
||||||
|
uploaded_at: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'uploaded_at'>) {
|
||||||
|
return db.insertInto('unattached_media')
|
||||||
|
.values({
|
||||||
|
id: uuid62.v4(),
|
||||||
|
uploaded_at: new Date(),
|
||||||
|
...media,
|
||||||
|
data: JSON.stringify(media.data),
|
||||||
|
})
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export { insertUnattachedMedia };
|
||||||
|
|
@ -68,5 +68,6 @@ export { default as tldts } from 'npm:tldts@^6.0.14';
|
||||||
export * as cron from 'https://deno.land/x/deno_cron@v1.0.0/cron.ts';
|
export * as cron from 'https://deno.land/x/deno_cron@v1.0.0/cron.ts';
|
||||||
export { S3Client } from 'https://deno.land/x/s3_lite_client@0.6.1/mod.ts';
|
export { S3Client } from 'https://deno.land/x/s3_lite_client@0.6.1/mod.ts';
|
||||||
export { default as IpfsHash } from 'npm:ipfs-only-hash@^4.0.0';
|
export { default as IpfsHash } from 'npm:ipfs-only-hash@^4.0.0';
|
||||||
|
export { default as uuid62 } from 'npm:uuid62@^1.0.2';
|
||||||
|
|
||||||
export type * as TypeFest from 'npm:type-fest@^4.3.0';
|
export type * as TypeFest from 'npm:type-fest@^4.3.0';
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue