website refactor
This commit is contained in:
67
apps/website/lib/adapters/MediaProxyAdapter.ts
Normal file
67
apps/website/lib/adapters/MediaProxyAdapter.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* MediaProxyAdapter
|
||||
*
|
||||
* Handles direct HTTP proxy operations for media assets.
|
||||
* This is a special case where direct fetch is needed for binary responses.
|
||||
*/
|
||||
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
|
||||
export type MediaProxyError =
|
||||
| { type: 'notFound'; message: string }
|
||||
| { type: 'serverError'; message: string }
|
||||
| { type: 'networkError'; message: string };
|
||||
|
||||
/**
|
||||
* Proxy media request to backend API
|
||||
*
|
||||
* @param mediaPath - The API path to fetch media from (e.g., "/media/avatar/123")
|
||||
* @returns Result with ArrayBuffer on success, or error on failure
|
||||
*/
|
||||
export async function proxyMediaRequest(
|
||||
mediaPath: string
|
||||
): Promise<Result<ArrayBuffer, MediaProxyError>> {
|
||||
try {
|
||||
const baseUrl = process.env.API_BASE_URL || 'http://localhost:3000';
|
||||
const response = await fetch(`${baseUrl}${mediaPath}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
return Result.err({
|
||||
type: 'notFound',
|
||||
message: `Media not found: ${mediaPath}`
|
||||
});
|
||||
}
|
||||
return Result.err({
|
||||
type: 'serverError',
|
||||
message: `HTTP ${response.status}: ${response.statusText}`
|
||||
});
|
||||
}
|
||||
|
||||
const buffer = await response.arrayBuffer();
|
||||
return Result.ok(buffer);
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
return Result.err({
|
||||
type: 'networkError',
|
||||
message: `Failed to fetch media: ${errorMessage}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content type for media path
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function getMediaContentType(mediaPath: string): string {
|
||||
return 'image/png';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache control header value
|
||||
*/
|
||||
export function getMediaCacheControl(): string {
|
||||
return 'public, max-age=3600';
|
||||
}
|
||||
Reference in New Issue
Block a user