2025-08-18 12:50:32 +07:00
|
|
|
import { api, ApiResponse } from '../index';
|
|
|
|
|
|
|
|
|
|
export interface UploadResponse {
|
|
|
|
|
filename: string;
|
|
|
|
|
original_filename: string;
|
|
|
|
|
uploaded_path: string;
|
|
|
|
|
url: string;
|
|
|
|
|
size: number;
|
|
|
|
|
content_type: string;
|
|
|
|
|
file_type: string;
|
|
|
|
|
user_id: string;
|
|
|
|
|
email: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UploadService {
|
|
|
|
|
uploadFile(file: File): Promise<UploadResponse>;
|
|
|
|
|
uploadAvatar(file: File): Promise<UploadResponse>;
|
|
|
|
|
uploadCV(file: File): Promise<UploadResponse>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const uploadService: UploadService = {
|
|
|
|
|
async uploadFile(file: File) {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', file);
|
|
|
|
|
|
|
|
|
|
const response = await api.post<ApiResponse<UploadResponse>>('/users/upload', formData, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return response.data.data;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async uploadAvatar(file: File) {
|
|
|
|
|
if (!file.type.startsWith('image/')) {
|
|
|
|
|
throw new Error('File harus berupa gambar');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:44:17 +07:00
|
|
|
const maxSize = 5 * 1024 * 1024;
|
2025-08-18 12:50:32 +07:00
|
|
|
if (file.size > maxSize) {
|
|
|
|
|
throw new Error('Ukuran file maksimal 5MB');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.uploadFile(file);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async uploadCV(file: File) {
|
|
|
|
|
if (file.type !== 'application/pdf') {
|
|
|
|
|
throw new Error('CV harus berupa file PDF');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:44:17 +07:00
|
|
|
const maxSize = 10 * 1024 * 1024;
|
2025-08-18 12:50:32 +07:00
|
|
|
if (file.size > maxSize) {
|
|
|
|
|
throw new Error('Ukuran file maksimal 10MB');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.uploadFile(file);
|
|
|
|
|
},
|
|
|
|
|
};
|