diff --git a/src/server/utils/files.test.ts b/src/server/utils/files.test.ts new file mode 100644 index 000000000000..2c3dacc5a5c9 --- /dev/null +++ b/src/server/utils/files.test.ts @@ -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; + }); +}); diff --git a/src/server/utils/files.ts b/src/server/utils/files.ts index a421af64c86b..537fe2213efe 100644 --- a/src/server/utils/files.ts +++ b/src/server/utils/files.ts @@ -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); };