Add a configUploader module to select the uploader based on config
This commit is contained in:
parent
34acc99000
commit
8ae89462b7
|
@ -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() {
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
|
|
@ -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 };
|
Loading…
Reference in New Issue