uploads: make uploader and media URL configurable

This commit is contained in:
Alex Gleason 2023-09-08 15:20:57 -05:00
parent 014b9f6d29
commit 4f57ac0352
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 39 additions and 4 deletions

View File

@ -1,4 +1,8 @@
import { dotenv, getPublicKey, nip19, secp, z } from '@/deps.ts';
import { ipfsUploader } from '@/uploaders/ipfs.ts';
import { s3Uploader } from '@/uploaders/s3.ts';
import type { Uploader } from '@/uploaders/types.ts';
/** Load environment config from `.env` */
await dotenv.load({
@ -42,7 +46,7 @@ const Conf = {
const { protocol, host } = Conf.url;
return `${protocol === 'https:' ? 'wss:' : 'ws:'}//${host}/relay`;
},
/** Domain of the Ditto server, including the protocol. */
/** Origin of the Ditto server, including the protocol and port. */
get localDomain() {
return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
},
@ -88,12 +92,36 @@ const Conf = {
return optionalBooleanSchema.parse(Deno.env.get('S3_USE_SSL'));
},
},
/** IPFS uploader configuration. */
ipfs: {
/** Base URL for private IPFS API calls. */
get apiUrl() {
return Deno.env.get('IPFS_API_URL') || 'http://localhost:5001';
},
},
/** Module to upload files with. */
get uploader(): Uploader {
switch (Deno.env.get('DITTO_UPLOADER')) {
case 's3':
return s3Uploader;
case 'ipfs':
return ipfsUploader;
default:
return ipfsUploader;
}
},
/** Media base URL for uploads. */
get mediaDomain() {
const value = Deno.env.get('MEDIA_DOMAIN');
if (!value) {
const url = Conf.url;
url.host = `media.${url.host}`;
return url.toString();
}
return value;
},
/** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */
get url() {
return new URL(Conf.localDomain);

View File

@ -1,8 +1,8 @@
import { AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { z } from '@/deps.ts';
import { fileSchema } from '@/schema.ts';
import { parseBody } from '@/utils/web.ts';
import { s3Uploader } from '@/uploaders/s3.ts';
const mediaBodySchema = z.object({
file: fileSchema.refine((file) => !!file.type),
@ -19,12 +19,19 @@ const mediaController: AppController = async (c) => {
}
try {
const { file } = result.data;
const { cid } = await s3Uploader.upload(file);
const { file, description } = result.data;
const { cid } = await Conf.uploader.upload(file);
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();
return c.json({
id: cid,
type: file.type,
url,
preview_url: url,
remote_url: null,
description,
blurhash: null,
});
} catch (e) {
console.error(e);