Skip to content

Commit

Permalink
🐛 fix: improve s3 path-style url (lobehub#3567)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Aug 23, 2024
1 parent 653bb34 commit 96bb38a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/server/utils/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { fileEnv } from '@/config/file';

import { getFullFileUrl } from './files';

const config = {
S3_ENABLE_PATH_STYLE: false,
S3_PUBLIC_DOMAIN: 'https://example.com',
S3_BUCKET: 'my-bucket',
};

vi.mock('@/config/file', () => ({
get fileEnv() {
return config;
},
}));

describe('getFullFileUrl', () => {
it('should return empty string for null or undefined input', () => {
expect(getFullFileUrl(null)).toBe('');
expect(getFullFileUrl(undefined)).toBe('');
});

it('should return correct URL when S3_ENABLE_PATH_STYLE is false', () => {
const url = 'path/to/file.jpg';
expect(getFullFileUrl(url)).toBe('https://example.com/path/to/file.jpg');
});

it('should return correct URL when S3_ENABLE_PATH_STYLE is true', () => {
config.S3_ENABLE_PATH_STYLE = true;
const url = 'path/to/file.jpg';
expect(getFullFileUrl(url)).toBe('https://example.com/my-bucket/path/to/file.jpg');
config.S3_ENABLE_PATH_STYLE = false;
});
});
4 changes: 4 additions & 0 deletions src/server/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ import { fileEnv } from '@/config/file';
export const getFullFileUrl = (url?: string | null) => {
if (!url) return '';

if (fileEnv.S3_ENABLE_PATH_STYLE) {
return urlJoin(fileEnv.S3_PUBLIC_DOMAIN!, fileEnv.S3_BUCKET!, url);
}

return urlJoin(fileEnv.S3_PUBLIC_DOMAIN!, url);
};

0 comments on commit 96bb38a

Please sign in to comment.