diff --git a/backend/src/config/storage.ts b/backend/src/config/storage.ts index a614d3ab8..842070bc9 100644 --- a/backend/src/config/storage.ts +++ b/backend/src/config/storage.ts @@ -13,11 +13,13 @@ if (!isStorageSetupFull) { console.log('S3 credentials not entered correctly'); } -export const s3Client = new S3Client({ - region: vars.s3.region, - endpoint: vars.s3.endpoint, - credentials: { - accessKeyId: vars.s3.secrets.accessKeyId, - secretAccessKey: vars.s3.secrets.secretAccessKey - } -}); +export const s3Client = isStorageSetupFull + ? new S3Client({ + region: vars.s3.region, + endpoint: vars.s3.endpoint, + credentials: { + accessKeyId: vars.s3.secrets.accessKeyId, + secretAccessKey: vars.s3.secrets.secretAccessKey + } + }) + : undefined; diff --git a/backend/src/controllers/events-state-controller.ts b/backend/src/controllers/events-state-controller.ts index 7c4d2eca0..9ea91fb0f 100644 --- a/backend/src/controllers/events-state-controller.ts +++ b/backend/src/controllers/events-state-controller.ts @@ -5,6 +5,7 @@ import { CommonErrorsEnum, SupportedLanguages } from '../../../common/const'; import { EventModel } from '../models/event.model'; import { imageController } from './image-controller'; import { countriesAndCitiesController } from './countries-and-cities.controller'; +import { vars } from '../config/vars'; class EventsStateController { async addEvent(event: EventOnPoster) { @@ -167,9 +168,14 @@ class EventsStateController { const event = await EventModel.findOne({ id }); - if (event?.image && !event.image.includes('https://') && !event.image.includes('http://')) + if ( + (event?.image && + !event.image.includes('https://') && + !event.image.includes('http://')) || + event?.image.includes(`https://${vars.s3.bucket}.fsn1.your-objectstorage.com/`) + ) try { - await imageController.deleteImg(`.${event.image}`); + await imageController.deleteImg(event.image); } catch (e) { // eslint-disable-next-line no-console console.log(e); diff --git a/backend/src/controllers/image-controller.ts b/backend/src/controllers/image-controller.ts index 290afbfbe..67653bce1 100644 --- a/backend/src/controllers/image-controller.ts +++ b/backend/src/controllers/image-controller.ts @@ -44,12 +44,17 @@ export class FileDbController { async deleteImg(path: string) { if (isStorageSetupFull) { - await deleteFileOnS3( - path.replace(`https://${vars.s3.bucket}.fsn1.your-objectstorage.com/`, '') - ); - return undefined; + try { + await deleteFileOnS3( + path.replace(`https://${vars.s3.bucket}.fsn1.your-objectstorage.com/`, '') + ); + return undefined; + } catch (e) { + // eslint-disable-next-line no-console + console.error(e); + } } - const realPath = path.replace('image/', 'assets/img/'); + const realPath = path.replace('image/', './assets/img/'); return fs.unlink(resolve(realPath)); } } diff --git a/backend/src/externalServices/storage/createPresignedUrl.ts b/backend/src/externalServices/storage/createPresignedUrl.ts index 49a05a1da..5859d1b33 100644 --- a/backend/src/externalServices/storage/createPresignedUrl.ts +++ b/backend/src/externalServices/storage/createPresignedUrl.ts @@ -3,12 +3,12 @@ import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { s3Client } from '../../config/storage'; import { vars } from '../../config/vars'; -export const createPresignedUrlWithClient = (path: string) => { +export const createPresignedUrlWithClient = async (path: string) => { + if (!s3Client) return; const params: PutObjectCommandInput = { Bucket: vars.s3.bucket, Key: path }; const command = new PutObjectCommand(params); - const res = getSignedUrl(s3Client, command, { expiresIn: 3600 }); - return res; + await getSignedUrl(s3Client, command, { expiresIn: 3600 }); }; diff --git a/backend/src/externalServices/storage/deleteFileOnS3.ts b/backend/src/externalServices/storage/deleteFileOnS3.ts index 32721ef6f..f3c91c04b 100644 --- a/backend/src/externalServices/storage/deleteFileOnS3.ts +++ b/backend/src/externalServices/storage/deleteFileOnS3.ts @@ -3,11 +3,11 @@ import { s3Client } from '../../config/storage'; import { vars } from '../../config/vars'; export const deleteFileOnS3 = async (path: string) => { + if (!s3Client) return; const params = { Bucket: vars.s3.bucket, Key: path }; const command = new DeleteObjectCommand(params); - const res = await s3Client.send(command); - return res; + await s3Client.send(command); }; diff --git a/backend/src/externalServices/storage/uploadFileToS3.ts b/backend/src/externalServices/storage/uploadFileToS3.ts index 08ee455ec..eb6db419f 100644 --- a/backend/src/externalServices/storage/uploadFileToS3.ts +++ b/backend/src/externalServices/storage/uploadFileToS3.ts @@ -3,12 +3,12 @@ import { s3Client } from '../../config/storage'; import { vars } from '../../config/vars'; export const uploadFileToS3 = async (path: string, file: PutObjectCommandInput['Body']) => { + if (!s3Client) return; const params: PutObjectCommandInput = { Bucket: vars.s3.bucket, Key: path, Body: file }; const command = new PutObjectCommand(params); - const res = await s3Client.send(command); - return res; + await s3Client.send(command); }; diff --git a/backend/src/rest/v1/image/controller.ts b/backend/src/rest/v1/image/controller.ts index c1aa1250c..2e30f49c3 100644 --- a/backend/src/rest/v1/image/controller.ts +++ b/backend/src/rest/v1/image/controller.ts @@ -7,7 +7,7 @@ export const deleteImage: IDeleteImageHandlerProps = async (request) => { if (!filePath) throw new Error(CommonErrorsEnum.NO_IMAGE_TO_DELETE); try { - await imageController.deleteImg(`.${filePath}`); + await imageController.deleteImg(filePath); return undefined; } catch (e) { throw new Error(CommonErrorsEnum.IMAGE_DELETION_ERROR);