mediaController: get cid, fix permissions, host on /ipfs

This commit is contained in:
Alex Gleason 2023-09-06 22:10:28 -05:00
parent 3fc60c78d2
commit 4622b52f78
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 13 additions and 3 deletions

View File

@ -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 };

View File

@ -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';