2020-12-31 21:37:37 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as Joi from 'joi';
|
|
|
|
import { BackendType } from '../media/backends/backend-type.enum';
|
2021-01-15 15:57:04 +00:00
|
|
|
import { registerAs } from '@nestjs/config';
|
2021-01-19 14:47:05 +00:00
|
|
|
import { buildErrorMessage } from './utils';
|
2020-12-31 21:37:37 +00:00
|
|
|
|
|
|
|
export interface MediaConfig {
|
|
|
|
backend: {
|
|
|
|
use: BackendType;
|
|
|
|
filesystem: {
|
|
|
|
uploadPath: string;
|
|
|
|
};
|
|
|
|
s3: {
|
|
|
|
accessKeyId: string;
|
|
|
|
secretAccessKey: string;
|
|
|
|
bucket: string;
|
|
|
|
endPoint: string;
|
|
|
|
};
|
|
|
|
azure: {
|
|
|
|
connectionString: string;
|
|
|
|
container: string;
|
|
|
|
};
|
|
|
|
imgur: {
|
|
|
|
clientID: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:57:04 +00:00
|
|
|
const mediaSchema = Joi.object({
|
2020-12-31 21:37:37 +00:00
|
|
|
backend: {
|
2021-01-19 14:47:05 +00:00
|
|
|
use: Joi.string()
|
|
|
|
.valid(...Object.values(BackendType))
|
|
|
|
.label('HD_MEDIA_BACKEND'),
|
2020-12-31 21:37:37 +00:00
|
|
|
filesystem: {
|
|
|
|
uploadPath: Joi.when('...use', {
|
|
|
|
is: Joi.valid(BackendType.FILESYSTEM),
|
|
|
|
then: Joi.string(),
|
|
|
|
otherwise: Joi.optional(),
|
2021-01-19 14:47:05 +00:00
|
|
|
}).label('HD_MEDIA_BACKEND_FILESYSTEM_UPLOAD_PATH'),
|
2020-12-31 21:37:37 +00:00
|
|
|
},
|
2021-02-23 14:45:59 +00:00
|
|
|
s3: Joi.when('use', {
|
2020-12-31 21:37:37 +00:00
|
|
|
is: Joi.valid(BackendType.S3),
|
|
|
|
then: Joi.object({
|
2021-01-19 14:47:05 +00:00
|
|
|
accessKey: Joi.string().label('HD_MEDIA_BACKEND_S3_ACCESS_KEY'),
|
|
|
|
secretKey: Joi.string().label('HD_MEDIA_BACKEND_S3_SECRET_KEY'),
|
2021-02-23 15:13:33 +00:00
|
|
|
bucket: Joi.string().label('HD_MEDIA_BACKEND_S3_BUCKET'),
|
2021-01-19 14:47:05 +00:00
|
|
|
endPoint: Joi.string().label('HD_MEDIA_BACKEND_S3_ENDPOINT'),
|
2020-12-31 21:37:37 +00:00
|
|
|
}),
|
|
|
|
otherwise: Joi.optional(),
|
|
|
|
}),
|
2021-02-23 14:45:59 +00:00
|
|
|
azure: Joi.when('use', {
|
2020-12-31 21:37:37 +00:00
|
|
|
is: Joi.valid(BackendType.AZURE),
|
|
|
|
then: Joi.object({
|
2021-01-19 14:47:05 +00:00
|
|
|
connectionString: Joi.string().label(
|
|
|
|
'HD_MEDIA_BACKEND_AZURE_CONNECTION_STRING',
|
|
|
|
),
|
|
|
|
container: Joi.string().label('HD_MEDIA_BACKEND_AZURE_CONTAINER'),
|
2020-12-31 21:37:37 +00:00
|
|
|
}),
|
|
|
|
otherwise: Joi.optional(),
|
|
|
|
}),
|
2021-02-23 14:45:59 +00:00
|
|
|
imgur: Joi.when('use', {
|
2020-12-31 21:37:37 +00:00
|
|
|
is: Joi.valid(BackendType.IMGUR),
|
|
|
|
then: Joi.object({
|
2021-01-28 22:28:16 +00:00
|
|
|
clientID: Joi.string().label('HD_MEDIA_BACKEND_IMGUR_CLIENT_ID'),
|
2020-12-31 21:37:37 +00:00
|
|
|
}),
|
|
|
|
otherwise: Joi.optional(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-24 19:29:39 +00:00
|
|
|
export default registerAs('mediaConfig', () => {
|
2021-01-15 15:57:04 +00:00
|
|
|
const mediaConfig = mediaSchema.validate(
|
|
|
|
{
|
|
|
|
backend: {
|
|
|
|
use: process.env.HD_MEDIA_BACKEND,
|
|
|
|
filesystem: {
|
|
|
|
uploadPath: process.env.HD_MEDIA_BACKEND_FILESYSTEM_UPLOAD_PATH,
|
|
|
|
},
|
|
|
|
s3: {
|
|
|
|
accessKey: process.env.HD_MEDIA_BACKEND_S3_ACCESS_KEY,
|
2021-01-19 14:47:05 +00:00
|
|
|
secretKey: process.env.HD_MEDIA_BACKEND_S3_SECRET_KEY,
|
2021-02-23 15:13:33 +00:00
|
|
|
bucket: process.env.HD_MEDIA_BACKEND_S3_BUCKET,
|
2021-01-15 15:57:04 +00:00
|
|
|
endPoint: process.env.HD_MEDIA_BACKEND_S3_ENDPOINT,
|
|
|
|
},
|
|
|
|
azure: {
|
|
|
|
connectionString:
|
|
|
|
process.env.HD_MEDIA_BACKEND_AZURE_CONNECTION_STRING,
|
|
|
|
container: process.env.HD_MEDIA_BACKEND_AZURE_CONTAINER,
|
|
|
|
},
|
|
|
|
imgur: {
|
2021-01-28 22:28:16 +00:00
|
|
|
clientID: process.env.HD_MEDIA_BACKEND_IMGUR_CLIENT_ID,
|
2021-01-15 15:57:04 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-31 21:37:37 +00:00
|
|
|
},
|
2021-01-15 15:57:04 +00:00
|
|
|
{
|
|
|
|
abortEarly: false,
|
|
|
|
presence: 'required',
|
2020-12-31 21:37:37 +00:00
|
|
|
},
|
2021-01-15 15:57:04 +00:00
|
|
|
);
|
|
|
|
if (mediaConfig.error) {
|
2021-02-24 19:29:39 +00:00
|
|
|
const errorMessages = mediaConfig.error.details.map(
|
2021-01-19 14:47:05 +00:00
|
|
|
(detail) => detail.message,
|
|
|
|
);
|
|
|
|
throw new Error(buildErrorMessage(errorMessages));
|
2021-01-15 15:57:04 +00:00
|
|
|
}
|
2021-02-24 19:29:39 +00:00
|
|
|
return mediaConfig.value as MediaConfig;
|
2021-01-15 15:57:04 +00:00
|
|
|
});
|