Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image deletion; #1484

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions backend/src/config/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 8 additions & 2 deletions backend/src/controllers/events-state-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions backend/src/controllers/image-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
6 changes: 3 additions & 3 deletions backend/src/externalServices/storage/createPresignedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};
4 changes: 2 additions & 2 deletions backend/src/externalServices/storage/deleteFileOnS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
4 changes: 2 additions & 2 deletions backend/src/externalServices/storage/uploadFileToS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion backend/src/rest/v1/image/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading