Skip to content

Commit 96bb38a

Browse files
authored
🐛 fix: improve s3 path-style url (#3567)
1 parent 653bb34 commit 96bb38a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/server/utils/files.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
import { fileEnv } from '@/config/file';
4+
5+
import { getFullFileUrl } from './files';
6+
7+
const config = {
8+
S3_ENABLE_PATH_STYLE: false,
9+
S3_PUBLIC_DOMAIN: 'https://example.com',
10+
S3_BUCKET: 'my-bucket',
11+
};
12+
13+
vi.mock('@/config/file', () => ({
14+
get fileEnv() {
15+
return config;
16+
},
17+
}));
18+
19+
describe('getFullFileUrl', () => {
20+
it('should return empty string for null or undefined input', () => {
21+
expect(getFullFileUrl(null)).toBe('');
22+
expect(getFullFileUrl(undefined)).toBe('');
23+
});
24+
25+
it('should return correct URL when S3_ENABLE_PATH_STYLE is false', () => {
26+
const url = 'path/to/file.jpg';
27+
expect(getFullFileUrl(url)).toBe('https://example.com/path/to/file.jpg');
28+
});
29+
30+
it('should return correct URL when S3_ENABLE_PATH_STYLE is true', () => {
31+
config.S3_ENABLE_PATH_STYLE = true;
32+
const url = 'path/to/file.jpg';
33+
expect(getFullFileUrl(url)).toBe('https://example.com/my-bucket/path/to/file.jpg');
34+
config.S3_ENABLE_PATH_STYLE = false;
35+
});
36+
});

src/server/utils/files.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ import { fileEnv } from '@/config/file';
55
export const getFullFileUrl = (url?: string | null) => {
66
if (!url) return '';
77

8+
if (fileEnv.S3_ENABLE_PATH_STYLE) {
9+
return urlJoin(fileEnv.S3_PUBLIC_DOMAIN!, fileEnv.S3_BUCKET!, url);
10+
}
11+
812
return urlJoin(fileEnv.S3_PUBLIC_DOMAIN!, url);
913
};

0 commit comments

Comments
 (0)