Skip to content

Commit

Permalink
feat: let uploads:import continue when a file path is corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Jun 28, 2024
1 parent b0295b5 commit d87a703
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"ext-zip": "*",
"guzzlehttp/guzzle": "^7.0",
"illuminate/collections": "^8.0|^9.0|^10.0",
"league/flysystem": "^2.0|^3.0",
"league/flysystem": "^2.1.1|^3.0",
"league/flysystem-ftp": "^2.0|^3.0",
"league/flysystem-sftp-v3": "^2.0|^3.0",
"nesbot/carbon": "^2.40",
Expand Down
31 changes: 22 additions & 9 deletions src/Command/Uploads/ImportUploadsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use League\Flysystem\CorruptedPathDetected;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\Ftp\FtpAdapter;
Expand Down Expand Up @@ -107,9 +108,10 @@ protected function perform(Input $input, Output $output)

$progressBar = new ProgressBar($output);
$progressBar->setFormat("Importing file (<comment>%filename%</comment>)\nTotal files imported: <comment>%total%</comment>\n");
$progressBar->setMessage('0', 'total');

$corruptedFilePaths = [];
$total = 0;
$progressBar->setMessage((string) $total, 'total');

if (!$adapter instanceof LocalFilesystemAdapter) {
$output->infoWithWarning('Scanning remote "uploads" directory', 'takes a few seconds');
Expand All @@ -125,21 +127,32 @@ protected function perform(Input $input, Output $output)
}
})->chunk($size)->mapWithKeys(function (Enumerable $chunkedFiles) use ($environment) {
return $this->getSignedUploadRequest($environment, $chunkedFiles);
})->map(function (array $request, string $filePath) use ($filesystem, $progressBar, &$total) {
$request['body'] = $filesystem->readStream(mb_convert_encoding($filePath, 'UTF-8'));
})->map(function (array $request, string $filePath) use (&$corruptedFilePaths, $filesystem, $progressBar, &$total) {
try {
$request['body'] = $filesystem->readStream(mb_convert_encoding($filePath, 'UTF-8'));

++$total;
++$total;

$progressBar->setMessage($filePath, 'filename');
$progressBar->setMessage((string) $total, 'total');
$progressBar->advance();
$progressBar->setMessage($filePath, 'filename');
$progressBar->setMessage((string) $total, 'total');
$progressBar->advance();

return $request;
});
return $request;
} catch (CorruptedPathDetected $exception) {
$corruptedFilePaths[] = $filePath;

return null;
}
})->filter();

$this->uploader->batch('PUT', $requests);

$output->info(sprintf('Files imported successfully to the "<comment>%s</comment>" environment "uploads" directory', $environment));

if (!empty($corruptedFilePaths)) {
$output->warning('The following files were not imported because their paths are corrupted:');
$output->list($corruptedFilePaths);
}
}

/**
Expand Down

0 comments on commit d87a703

Please sign in to comment.