Etienne
2025-07-24 15:34:41 +02:00
committed by GitHub
parent 16fcaacbdb
commit 5c15a5954b
2 changed files with 25 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import { Logger } from '@nestjs/common';
import fs from 'fs'; import fs from 'fs';
import { mkdir } from 'fs/promises'; import { mkdir } from 'fs/promises';
import { join } from 'path'; import { join } from 'path';
@ -35,6 +37,7 @@ export interface S3DriverOptions extends S3ClientConfig {
export class S3Driver implements StorageDriver { export class S3Driver implements StorageDriver {
private s3Client: S3; private s3Client: S3;
private bucketName: string; private bucketName: string;
private readonly logger = new Logger(S3Driver.name);
constructor(options: S3DriverOptions) { constructor(options: S3DriverOptions) {
const { bucketName, region, endpoint, ...s3Options } = options; const { bucketName, region, endpoint, ...s3Options } = options;
@ -69,6 +72,8 @@ export class S3Driver implements StorageDriver {
// @ts-expect-error legacy noImplicitAny // @ts-expect-error legacy noImplicitAny
private async emptyS3Directory(folderPath) { private async emptyS3Directory(folderPath) {
this.logger.log(`${folderPath} - emptying folder`);
const listParams = { const listParams = {
Bucket: this.bucketName, Bucket: this.bucketName,
Prefix: folderPath, Prefix: folderPath,
@ -77,6 +82,13 @@ export class S3Driver implements StorageDriver {
const listObjectsCommand = new ListObjectsV2Command(listParams); const listObjectsCommand = new ListObjectsV2Command(listParams);
const listedObjects = await this.s3Client.send(listObjectsCommand); const listedObjects = await this.s3Client.send(listObjectsCommand);
this.logger.log(
`${folderPath} - listed objects`,
listedObjects.Contents,
listedObjects.IsTruncated,
listedObjects.Contents?.length,
);
if (listedObjects.Contents?.length === 0) return; if (listedObjects.Contents?.length === 0) return;
const deleteParams = { const deleteParams = {
@ -92,7 +104,11 @@ export class S3Driver implements StorageDriver {
await this.s3Client.send(deleteObjectCommand); await this.s3Client.send(deleteObjectCommand);
this.logger.log(`${folderPath} - objects deleted`);
if (listedObjects.IsTruncated) { if (listedObjects.IsTruncated) {
this.logger.log(`${folderPath} - folder is truncated`);
await this.emptyS3Directory(folderPath); await this.emptyS3Directory(folderPath);
} }
} }
@ -101,6 +117,10 @@ export class S3Driver implements StorageDriver {
folderPath: string; folderPath: string;
filename?: string; filename?: string;
}): Promise<void> { }): Promise<void> {
this.logger.log(
`${params.folderPath} - deleting file ${params.filename} from folder ${params.folderPath}`,
);
if (params.filename) { if (params.filename) {
const deleteCommand = new DeleteObjectCommand({ const deleteCommand = new DeleteObjectCommand({
Key: `${params.folderPath}/${params.filename}`, Key: `${params.folderPath}/${params.filename}`,
@ -110,6 +130,9 @@ export class S3Driver implements StorageDriver {
await this.s3Client.send(deleteCommand); await this.s3Client.send(deleteCommand);
} else { } else {
await this.emptyS3Directory(params.folderPath); await this.emptyS3Directory(params.folderPath);
this.logger.log(`${params.folderPath} - folder is empty`);
const deleteEmptyFolderCommand = new DeleteObjectCommand({ const deleteEmptyFolderCommand = new DeleteObjectCommand({
Key: `${params.folderPath}`, Key: `${params.folderPath}`,
Bucket: this.bucketName, Bucket: this.bucketName,

View File

@ -18,8 +18,9 @@ export class FileWorkspaceFolderDeletionJob {
try { try {
await this.fileService.deleteWorkspaceFolder(workspaceId); await this.fileService.deleteWorkspaceFolder(workspaceId);
} catch (error) { } catch (error) {
//todo: clean up error message once issue on workspace folder deletion is fixed + in s3 driver file
throw new Error( throw new Error(
`[${FileWorkspaceFolderDeletionJob.name}] Cannot delete workspace folder - ${workspaceId}`, `[${FileWorkspaceFolderDeletionJob.name}] Cannot delete workspace folder - ${workspaceId} - ${error?.message || error}`,
); );
} }
} }