From 4622b52f78c67db4951a1a1b208d1c9338b8ea49 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 6 Sep 2023 22:10:28 -0500 Subject: [PATCH] mediaController: get cid, fix permissions, host on /ipfs --- src/controllers/api/media.ts | 15 ++++++++++++--- src/deps.ts | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/controllers/api/media.ts b/src/controllers/api/media.ts index 176bb7a..db8299a 100644 --- a/src/controllers/api/media.ts +++ b/src/controllers/api/media.ts @@ -1,6 +1,6 @@ import { AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { S3Client, z } from '@/deps.ts'; +import { IpfsHash, S3Client, z } from '@/deps.ts'; import { fileSchema } from '@/schema.ts'; import { parseBody } from '@/utils/web.ts'; @@ -21,15 +21,24 @@ const mediaController: AppController = async (c) => { } const { file } = result.data; + const cid = await IpfsHash.of(file.stream()) as string; try { - await s3.putObject('test', file.stream()); + await s3.putObject(`ipfs/${cid}`, file.stream(), { + metadata: { + 'Content-Type': file.type, + 'x-amz-acl': 'public-read', + }, + }); } catch (e) { console.error(e); return c.json({ error: 'Failed to upload file.' }, 500); } - return c.json({}); + return c.json({ + id: cid, + type: file.type, + }); }; export { mediaController }; diff --git a/src/deps.ts b/src/deps.ts index f1aad9a..5b4547b 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -67,5 +67,6 @@ export { DenoSqliteDialect } from 'https://gitlab.com/soapbox-pub/kysely-deno-sq 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 { 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 type * as TypeFest from 'npm:type-fest@^4.3.0';