Skip to content

Commit

Permalink
Merge pull request #7701 from ever-co/fix/wasabi-storage-provider
Browse files Browse the repository at this point in the history
[Fix] Storage Provider ForcePathStyle
  • Loading branch information
evereq authored Mar 27, 2024
2 parents 8a5118a + f27e3e3 commit 768e98c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
15 changes: 10 additions & 5 deletions packages/core/src/core/file-storage/providers/s3.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ export class S3Provider extends Provider<S3Provider> {
if (filename) {
fileName = typeof filename === 'string' ? filename : filename(file, extension);
} else {
fileName = `${prefix}-${moment().unix()}-${parseInt(
'' + Math.random() * 1000,
10
)}.${extension}`;
fileName = `${prefix}-${moment().unix()}-${parseInt('' + Math.random() * 1000, 10)}.${extension}`;
}

// Replace double backslashes with single forward slashes
Expand Down Expand Up @@ -241,6 +238,9 @@ export class S3Provider extends Provider<S3Provider> {
*/
async putFile(fileContent: string, key: string = ''): Promise<UploadedFile> {
try {
// Replace double backslashes with single forward slashes
key = key.replace(/\\/g, '/');

const s3Client = this.getS3Instance();
const filename = basename(key);

Expand Down Expand Up @@ -332,7 +332,12 @@ export class S3Provider extends Provider<S3Provider> {
accessKeyId: this.config.aws_access_key_id,
secretAccessKey: this.config.aws_secret_access_key
},
region
region,
/**
* Whether to force path style URLs for S3 objects
* (e.g., https://s3.amazonaws.com/<bucketName>/<key> instead of https://<bucketName>.s3.amazonaws.com/<key>
*/
forcePathStyle: true
});

return s3Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,7 @@ export class WasabiS3Provider extends Provider<WasabiS3Provider> {
if (filename) {
fileName = typeof filename === 'string' ? filename : filename(file, extension);
} else {
fileName = `${prefix}-${moment().unix()}-${parseInt(
'' + Math.random() * 1000,
10
)}.${extension}`;
fileName = `${prefix}-${moment().unix()}-${parseInt('' + Math.random() * 1000, 10)}.${extension}`;
}

// Replace double backslashes with single forward slashes
Expand Down Expand Up @@ -356,6 +353,9 @@ export class WasabiS3Provider extends Provider<WasabiS3Provider> {
*/
async putFile(fileContent: string, key: string = ''): Promise<UploadedFile> {
try {
// Replace double backslashes with single forward slashes
key = key.replace(/\\/g, '/');

const s3Client = this.getWasabiInstance();

if (s3Client) {
Expand Down Expand Up @@ -460,7 +460,14 @@ export class WasabiS3Provider extends Provider<WasabiS3Provider> {
secretAccessKey: this.config.wasabi_aws_secret_access_key
},
region: this.config.wasabi_aws_default_region || 'us-east-1',
endpoint
endpoint,
/**
* Whether to force path style URLs for S3 objects
*
* https://s3.wasabisys.com
* (e.g., https://s3.wasabisys.com/<bucketName>/<key> instead of https://<bucketName>.s3.wasabisys.com/<key>
*/
forcePathStyle: true
});

return s3Client;
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/image-asset/image-asset.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ export class ImageAssetController extends CrudController<ImageAsset> {
const request: any = ctx.switchToHttp().getRequest();
const folder: string = request?.params?.folder || 'image_assets';

// Define the base directory for storing media
const baseDirectory = path.join('uploads', folder);

// Generate unique sub directories based on the current tenant
const subDirectory = path.join(RequestContext.currentTenantId() || uuid());

return new FileStorage().storage({
dest: () => path.join('uploads', folder, RequestContext.currentTenantId() || uuid())
dest: () => path.join(baseDirectory, subDirectory),
});
}
})
Expand Down Expand Up @@ -94,7 +100,10 @@ export class ImageAssetController extends CrudController<ImageAsset> {
const thumbName = `thumb-${file.filename}`;
const thumbDir = path.dirname(file.key);

thumbnail = await provider.putFile(data, path.join(thumbDir, thumbName));
// Replace double backslashes with single forward slashes
const fullPath = path.join(thumbDir, thumbName).replace(/\\/g, '/');

thumbnail = await provider.putFile(data, fullPath);
} catch (error) {
console.error('Error while uploading media asset into file storage provider:', error);
}
Expand Down

0 comments on commit 768e98c

Please sign in to comment.