|
|
@@ -1,4 +1,6 @@
|
|
|
-import { ConfigSource } from '@growi/core/dist/interfaces';
|
|
|
+import {
|
|
|
+ ConfigSource, toNonBlankString, toNonBlankStringOrUndefined,
|
|
|
+} from '@growi/core/dist/interfaces';
|
|
|
import { ErrorV3 } from '@growi/core/dist/models';
|
|
|
import { body } from 'express-validator';
|
|
|
|
|
|
@@ -367,6 +369,7 @@ module.exports = (crowi) => {
|
|
|
body('gcsBucket').trim(),
|
|
|
body('gcsUploadNamespace').trim(),
|
|
|
body('gcsReferenceFileWithRelayMode').if(value => value != null).isBoolean(),
|
|
|
+ body('s3Bucket').trim(),
|
|
|
body('s3Region')
|
|
|
.trim()
|
|
|
.if(value => value !== '')
|
|
|
@@ -387,7 +390,6 @@ module.exports = (crowi) => {
|
|
|
}
|
|
|
return true;
|
|
|
}),
|
|
|
- body('s3Bucket').trim(),
|
|
|
body('s3AccessKeyId').trim().if(value => value !== '').matches(/^[\da-zA-Z]+$/),
|
|
|
body('s3SecretAccessKey').trim(),
|
|
|
body('s3ReferenceFileWithRelayMode').if(value => value != null).isBoolean(),
|
|
|
@@ -888,42 +890,88 @@ module.exports = (crowi) => {
|
|
|
router.put('/file-upload-setting', loginRequiredStrictly, adminRequired, addActivity, validator.fileUploadSetting, apiV3FormValidator, async(req, res) => {
|
|
|
const { fileUploadType } = req.body;
|
|
|
|
|
|
- const requestParams = {
|
|
|
- 'app:fileUploadType': fileUploadType,
|
|
|
- };
|
|
|
-
|
|
|
- if (fileUploadType === 'gcs') {
|
|
|
- requestParams['gcs:apiKeyJsonPath'] = req.body.gcsApiKeyJsonPath;
|
|
|
- requestParams['gcs:bucket'] = req.body.gcsBucket;
|
|
|
- requestParams['gcs:uploadNamespace'] = req.body.gcsUploadNamespace;
|
|
|
- requestParams['gcs:referenceFileWithRelayMode'] = req.body.gcsReferenceFileWithRelayMode;
|
|
|
+ if (fileUploadType === 'aws') {
|
|
|
+ try {
|
|
|
+ try {
|
|
|
+ toNonBlankString(req.body.s3Bucket);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ throw new Error('S3 Bucket name is required');
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ toNonBlankString(req.body.s3Region);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ throw new Error('S3 Region is required');
|
|
|
+ }
|
|
|
+ await configManager.updateConfigs({
|
|
|
+ 'app:fileUploadType': fileUploadType,
|
|
|
+ 'aws:s3Region': toNonBlankString(req.body.s3Region),
|
|
|
+ 'aws:s3Bucket': toNonBlankString(req.body.s3Bucket),
|
|
|
+ 'aws:referenceFileWithRelayMode': req.body.s3ReferenceFileWithRelayMode,
|
|
|
+ },
|
|
|
+ { skipPubsub: true });
|
|
|
+ await configManager.updateConfigs({
|
|
|
+ 'app:s3CustomEndpoint': toNonBlankStringOrUndefined(req.body.s3CustomEndpoint),
|
|
|
+ 'aws:s3AccessKeyId': toNonBlankStringOrUndefined(req.body.s3AccessKeyId),
|
|
|
+ 'aws:s3SecretAccessKey': toNonBlankStringOrUndefined(req.body.s3SecretAccessKey),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ skipPubsub: true,
|
|
|
+ removeIfUndefined: true,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ const msg = `Error occurred in updating AWS S3 settings: ${err.message}`;
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(msg, 'update-fileUploadType-failed'));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if (fileUploadType === 'aws') {
|
|
|
- requestParams['aws:s3Region'] = req.body.s3Region;
|
|
|
- requestParams['aws:s3CustomEndpoint'] = req.body.s3CustomEndpoint;
|
|
|
- requestParams['aws:s3Bucket'] = req.body.s3Bucket;
|
|
|
- requestParams['aws:s3AccessKeyId'] = req.body.s3AccessKeyId;
|
|
|
- requestParams['aws:referenceFileWithRelayMode'] = req.body.s3ReferenceFileWithRelayMode;
|
|
|
+ if (fileUploadType === 'gcs') {
|
|
|
+ try {
|
|
|
+ await configManager.updateConfigs({
|
|
|
+ 'app:fileUploadType': fileUploadType,
|
|
|
+ 'gcs:referenceFileWithRelayMode': req.body.gcsReferenceFileWithRelayMode,
|
|
|
+ },
|
|
|
+ { skipPubsub: true });
|
|
|
+ await configManager.updateConfigs({
|
|
|
+ 'gcs:apiKeyJsonPath': toNonBlankStringOrUndefined(req.body.gcsApiKeyJsonPath),
|
|
|
+ 'gcs:bucket': toNonBlankStringOrUndefined(req.body.gcsBucket),
|
|
|
+ 'gcs:uploadNamespace': toNonBlankStringOrUndefined(req.body.gcsUploadNamespace),
|
|
|
+ },
|
|
|
+ { skipPubsub: true, removeIfUndefined: true });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ const msg = `Error occurred in updating GCS settings: ${err.message}`;
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(msg, 'update-fileUploadType-failed'));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (fileUploadType === 'azure') {
|
|
|
- requestParams['azure:tenantId'] = req.body.azureTenantId;
|
|
|
- requestParams['azure:clientId'] = req.body.azureClientId;
|
|
|
- requestParams['azure:clientSecret'] = req.body.azureClientSecret;
|
|
|
- requestParams['azure:storageAccountName'] = req.body.azureStorageAccountName;
|
|
|
- requestParams['azure:storageContainerName'] = req.body.azureStorageContainerName;
|
|
|
- requestParams['azure:referenceFileWithRelayMode'] = req.body.azureReferenceFileWithRelayMode;
|
|
|
+ try {
|
|
|
+ await configManager.updateConfigs({
|
|
|
+ 'app:fileUploadType': fileUploadType,
|
|
|
+ 'azure:referenceFileWithRelayMode': req.body.azureReferenceFileWithRelayMode,
|
|
|
+ },
|
|
|
+ { skipPubsub: true });
|
|
|
+ await configManager.updateConfigs({
|
|
|
+ 'azure:tenantId': toNonBlankStringOrUndefined(req.body.azureTenantId),
|
|
|
+ 'azure:clientId': toNonBlankStringOrUndefined(req.body.azureClientId),
|
|
|
+ 'azure:clientSecret': toNonBlankStringOrUndefined(req.body.azureClientSecret),
|
|
|
+ 'azure:storageAccountName': toNonBlankStringOrUndefined(req.body.azureStorageAccountName),
|
|
|
+ 'azure:storageContainerName': toNonBlankStringOrUndefined(req.body.azureStorageContainerName),
|
|
|
+ }, { skipPubsub: true, removeIfUndefined: true });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ const msg = `Error occurred in updating Azure settings: ${err.message}`;
|
|
|
+ logger.error('Error', err);
|
|
|
+ return res.apiv3Err(new ErrorV3(msg, 'update-fileUploadType-failed'));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- await configManager.updateConfigs(requestParams, { skipPubsub: true });
|
|
|
-
|
|
|
- const s3SecretAccessKey = req.body.s3SecretAccessKey;
|
|
|
- if (fileUploadType === 'aws' && s3SecretAccessKey != null && s3SecretAccessKey.trim() !== '') {
|
|
|
- await configManager.updateConfigs({ 'aws:s3SecretAccessKey': s3SecretAccessKey }, { skipPubsub: true });
|
|
|
- }
|
|
|
-
|
|
|
await crowi.setUpFileUpload(true);
|
|
|
crowi.fileUploaderSwitchService.publishUpdatedMessage();
|
|
|
|
|
|
@@ -959,7 +1007,7 @@ module.exports = (crowi) => {
|
|
|
return res.apiv3({ responseParams });
|
|
|
}
|
|
|
catch (err) {
|
|
|
- const msg = 'Error occurred in updating fileUploadType';
|
|
|
+ const msg = 'Error occurred in retrieving file upload configurations';
|
|
|
logger.error('Error', err);
|
|
|
return res.apiv3Err(new ErrorV3(msg, 'update-fileUploadType-failed'));
|
|
|
}
|