Add a configUploader module to select the uploader based on config

This commit is contained in:
Alex Gleason 2023-09-08 16:04:55 -05:00
parent 34acc99000
commit 8ae89462b7
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 34 additions and 14 deletions

View File

@ -1,8 +1,4 @@
import { dotenv, getPublicKey, nip19, secp, z } from '@/deps.ts'; 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` */ /** Load environment config from `.env` */
await dotenv.load({ await dotenv.load({
@ -100,15 +96,8 @@ const Conf = {
}, },
}, },
/** Module to upload files with. */ /** Module to upload files with. */
get uploader(): Uploader { get uploader() {
switch (Deno.env.get('DITTO_UPLOADER')) { return Deno.env.get('DITTO_UPLOADER');
case 's3':
return s3Uploader;
case 'ipfs':
return ipfsUploader;
default:
return ipfsUploader;
}
}, },
/** Media base URL for uploads. */ /** Media base URL for uploads. */
get mediaDomain() { get mediaDomain() {

View File

@ -2,6 +2,7 @@ import { AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.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';
const mediaBodySchema = z.object({ const mediaBodySchema = z.object({
@ -20,7 +21,7 @@ const mediaController: AppController = async (c) => {
try { try {
const { file, description } = result.data; const { file, description } = result.data;
const { cid } = await Conf.uploader.upload(file); const { cid } = await uploader.upload(file);
const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString(); const url = new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString();

30
src/uploaders/config.ts Normal file
View File

@ -0,0 +1,30 @@
import { Conf } from '@/config.ts';
import { ipfsUploader } from './ipfs.ts';
import { s3Uploader } from './s3.ts';
import type { Uploader } from './types.ts';
/** Meta-uploader determined from configuration. */
const configUploader: Uploader = {
upload(file) {
return uploader().upload(file);
},
delete(cid) {
return uploader().delete(cid);
},
};
/** Get the uploader module based on configuration. */
function uploader() {
switch (Conf.uploader) {
case 's3':
return s3Uploader;
case 'ipfs':
return ipfsUploader;
default:
return ipfsUploader;
}
}
export { configUploader };