Files
adminpanel/src/services/upload.service.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

import api from './api';
interface PresignedUrlResponse {
uploadUrl: string;
publicUrl: string;
key: string;
}
class UploadService {
private basePath = '/upload';
/**
* 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 },
});
}
/**
* 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();