Advanced
Penggunaan Lanjutan
Best practices, error handling, dan integrasi advanced
Best Practices
Implementasi Retry
Gunakan exponential backoff untuk request yang gagal
Validasi File
Validasi ukuran dan tipe file sebelum upload
Streaming Download
Gunakan streaming untuk file besar
Progress Upload
Implementasi progress indicator untuk upload
Penanganan Error
javascript
// Logika Retry
async function uploadWithRetry(file, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const formData = new FormData();
formData.append('file', file);
const response = await fetch('https://upload.ylizidev.tech/api/upload/file', {
method: 'POST',
body: formData
});
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After');
const delay = retryAfter ? parseInt(retryAfter) * 1000 : Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
if (attempt === maxRetries) throw error;
await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 1000));
}
}
}
// Validasi File
function validateFile(file) {
const maxSize = 100 * 1024 * 1024; // 100MB
const allowedTypes = ['image/', 'video/', 'text/', 'application/'];
if (file.size > maxSize) {
throw new Error('File too large');
}
if (!allowedTypes.some(type => file.type.startsWith(type))) {
throw new Error('File type not allowed');
}
return true;
}Integrasi SDK
javascript SDK
// Yilzi Share SDK - JavaScript
class YilziClient {
constructor(baseUrl = 'https://upload.ylizidev.tech') {
this.baseUrl = baseUrl;
}
async uploadFile(file, uploaderName = '') {
const formData = new FormData();
formData.append('file', file);
if (uploaderName) {
formData.append('uploader_name', uploaderName);
}
const response = await fetch(`${this.baseUrl}/api/upload/file`, {
method: 'POST',
headers: { 'Accept': 'application/json' },
body: formData
});
if (!response.ok) {
throw new Error(`Upload failed: ${response.status}`);
}
return response.json();
}
async getRecentFiles(limit = 24) {
const response = await fetch(
`${this.baseUrl}/api/files/recent?limit=${limit}`
);
return response.json();
}
async getStats() {
const response = await fetch(`${this.baseUrl}/api/stats`);
return response.json();
}
async checkLimits() {
const response = await fetch(`${this.baseUrl}/api/check-limit`);
return response.json();
}
async getUsage() {
const response = await fetch(`${this.baseUrl}/api/usage`);
return response.json();
}
async getHealth() {
const response = await fetch(`${this.baseUrl}/api/health`);
return response.json();
}
}
// Usage
const client = new YilziClient();
const result = await client.uploadFile(fileInput.files[0]);
console.log('File uploaded:', result);Optimasi Performa
Gunakan compression untuk file teks sebelum upload
Implementasi caching untuk request yang sering dilakukan
Gunakan CDN untuk distribusi file global
Batch multiple operations ketika memungkinkan