2026-01-26 22:43:41 +05:30
|
|
|
import api from './api';
|
|
|
|
|
|
2026-02-24 03:30:35 +05:30
|
|
|
interface PresignedUrlResponse {
|
|
|
|
|
uploadUrl: string;
|
|
|
|
|
publicUrl: string;
|
|
|
|
|
key: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 22:43:41 +05:30
|
|
|
class UploadService {
|
|
|
|
|
private basePath = '/upload';
|
|
|
|
|
|
2026-02-24 03:30:35 +05:30
|
|
|
/**
|
|
|
|
|
* Get a presigned upload URL for S3
|
|
|
|
|
*/
|
|
|
|
|
async getPresignedUploadUrl(filename: string, contentType: string, folder: string): Promise<PresignedUrlResponse> {
|
|
|
|
|
const response = await api.post<{ data: PresignedUrlResponse }>(
|
|
|
|
|
`${this.basePath}/presigned-url`,
|
|
|
|
|
{ filename, contentType, folder }
|
|
|
|
|
);
|
|
|
|
|
return response.data.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Upload a file to S3 using a presigned URL
|
|
|
|
|
*/
|
|
|
|
|
async uploadFileToS3(uploadUrl: string, file: File): Promise<void> {
|
|
|
|
|
await fetch(uploadUrl, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
body: file,
|
|
|
|
|
headers: { 'Content-Type': file.type },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 22:43:41 +05:30
|
|
|
/**
|
|
|
|
|
* Get a presigned download URL for an S3 key
|
|
|
|
|
*/
|
|
|
|
|
async getPresignedDownloadUrl(key: string): Promise<string> {
|
|
|
|
|
const response = await api.get<{ data: { url: string } }>(
|
|
|
|
|
`${this.basePath}/presigned-download-url`,
|
|
|
|
|
{ params: { key } }
|
|
|
|
|
);
|
|
|
|
|
return response.data.data.url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a URL is an S3 key (not a full URL or local path)
|
|
|
|
|
*/
|
|
|
|
|
isS3Key(url: string | null | undefined): boolean {
|
|
|
|
|
if (!url) return false;
|
|
|
|
|
// S3 keys don't start with http or /
|
|
|
|
|
return !url.startsWith('http') && !url.startsWith('/');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const uploadService = new UploadService();
|