diff --git a/src/config.ts b/src/config.ts index 1ac8745..de273e5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,8 +1,4 @@ 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({ @@ -100,15 +96,8 @@ const Conf = { }, }, /** 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; - } + get uploader() { + return Deno.env.get('DITTO_UPLOADER'); }, /** Media base URL for uploads. */ get mediaDomain() { diff --git a/src/controllers/api/media.ts b/src/controllers/api/media.ts index ee30961..5b5128c 100644 --- a/src/controllers/api/media.ts +++ b/src/controllers/api/media.ts @@ -2,6 +2,7 @@ import { AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; import { z } from '@/deps.ts'; import { fileSchema } from '@/schema.ts'; +import { configUploader as uploader } from '@/uploaders/config.ts'; import { parseBody } from '@/utils/web.ts'; const mediaBodySchema = z.object({ @@ -20,7 +21,7 @@ const mediaController: AppController = async (c) => { try { 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(); diff --git a/src/uploaders/config.ts b/src/uploaders/config.ts new file mode 100644 index 0000000..b0adece --- /dev/null +++ b/src/uploaders/config.ts @@ -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 };