This commit is contained in:
Dwi Swandhana
2026-08-02 05:31:32 +07:00
parent 16f541ca77
commit 334728e16e
3 changed files with 72 additions and 12 deletions
@@ -3,6 +3,7 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
@@ -13,7 +14,10 @@ class BackupUploadFileCommand extends Command
{--url= : Base URL server cloud backup}
{--token= : Token API cloud backup}
{--name= : Nama backup}
{--chunk-size= : Ukuran chunk dalam byte}';
{--chunk-size= : Ukuran chunk dalam byte}
{--retries=5 : Jumlah percobaan ulang setiap chunk}
{--retry-delay=10 : Jeda percobaan ulang dalam detik}
{--timeout=300 : Timeout request upload dalam detik}';
protected $description = 'Upload file backup ke duidev.com secara bertahap/chunked.';
@@ -29,6 +33,9 @@ class BackupUploadFileCommand extends Command
$token = (string) ($this->option('token') ?: config('cloud_backup.api_token'));
$backupName = (string) ($this->option('name') ?: config('cloud_backup.backup_name', 'lismikro'));
$chunkSize = (int) ($this->option('chunk-size') ?: config('cloud_backup.chunk_size', 5 * 1024 * 1024));
$retries = max(1, (int) $this->option('retries'));
$retryDelay = max(0, (int) $this->option('retry-delay'));
$timeout = max(30, (int) $this->option('timeout'));
if ($baseUrl === '' || $token === '') {
$this->error('CLOUD_BACKUP_API_URL dan CLOUD_BACKUP_API_TOKEN wajib diisi.');
@@ -47,6 +54,7 @@ class BackupUploadFileCommand extends Command
$endpoint = $baseUrl . '/api/backups/chunk';
$this->info('Upload backup: ' . basename($file));
$this->info('Upload ID: ' . $uploadId);
$this->info('Total chunk: ' . $totalChunks);
$handle = fopen($file, 'rb');
@@ -63,10 +71,12 @@ class BackupUploadFileCommand extends Command
return Command::FAILURE;
}
$response = Http::withToken($token)
->timeout(300)
->attach('chunk', $chunk, 'chunk_' . $index . '.part')
->post($endpoint, [
$response = $this->uploadChunk(
endpoint: $endpoint,
token: $token,
chunk: $chunk,
chunkIndex: $index,
payload: [
'backup_name' => $backupName,
'upload_id' => $uploadId,
'filename' => basename($file),
@@ -74,11 +84,15 @@ class BackupUploadFileCommand extends Command
'total_chunks' => $totalChunks,
'total_size' => $size,
'checksum' => $checksum,
]);
],
retries: $retries,
retryDelay: $retryDelay,
timeout: $timeout
);
if (!$response->successful()) {
$this->error('Upload chunk ke-' . $index . ' gagal. HTTP ' . $response->status());
$this->line($response->body());
if (!$response || !$response->successful()) {
$this->error('Upload chunk ke-' . $index . ' gagal. HTTP ' . ($response?->status() ?: '-'));
$this->line($response?->body() ?: 'Tidak ada response dari server.');
return Command::FAILURE;
}
@@ -92,4 +106,40 @@ class BackupUploadFileCommand extends Command
return Command::SUCCESS;
}
private function uploadChunk(
string $endpoint,
string $token,
string $chunk,
int $chunkIndex,
array $payload,
int $retries,
int $retryDelay,
int $timeout
): ?Response {
$response = null;
for ($attempt = 1; $attempt <= $retries; $attempt++) {
try {
$response = Http::withToken($token)
->timeout($timeout)
->attach('chunk', $chunk, 'chunk_' . $chunkIndex . '.part')
->post($endpoint, $payload);
if ($response->successful()) {
return $response;
}
$this->warn('Chunk ' . ($chunkIndex + 1) . ' gagal percobaan ' . $attempt . '/' . $retries . '. HTTP ' . $response->status());
} catch (\Throwable $e) {
$this->warn('Chunk ' . ($chunkIndex + 1) . ' gagal percobaan ' . $attempt . '/' . $retries . ': ' . $e->getMessage());
}
if ($attempt < $retries && $retryDelay > 0) {
sleep($retryDelay);
}
}
return $response;
}
}