Skip to content

A free open source IT asset/license management system

Updated move items to new disk script to use file enums

snipe5f41dc00b180
diff --git a/app/Console/Commands/MoveUploadsToNewDisk.php b/app/Console/Commands/MoveUploadsToNewDisk.php
index 154a11b2be..1e64cd4647 100644
--- a/app/Console/Commands/MoveUploadsToNewDisk.php
+++ b/app/Console/Commands/MoveUploadsToNewDisk.php
@@ -6,168 +6,354 @@ use App\Enums\FileStorage;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
+use Throwable;
+
+use function Laravel\Prompts\confirm;
+use function Laravel\Prompts\error;
+use function Laravel\Prompts\info;
+use function Laravel\Prompts\note;
+use function Laravel\Prompts\progress;
+use function Laravel\Prompts\table;
+use function Laravel\Prompts\warning;
class MoveUploadsToNewDisk extends Command
{
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'snipeit:move-uploads {delete_local?}';
+ protected $signature = 'snipeit:move-uploads
+ {--delete-local : Delete the local source files after a successful copy. Prompts if omitted.}
+ {--overwrite : Overwrite destination files that already exist. Prompts if omitted.}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'This will move your locally uploaded files to whatever your current disk is.';
+ protected $description = 'Move locally uploaded files to the currently configured storage disk.';
/**
- * Create a new command instance.
+ * Per-type tally rendered in the summary table at the end. One row
+ * per FileStorage case the sweep touched, so admins can eyeball
+ * what moved without reading the full per-file stream.
*
- * @return void
+ * @var array<string, array{copied: int, skipped: int, private: int, private_skipped: int, errors: int}>
*/
- public function __construct()
+ private array $summary = [];
+
+ private bool $overwrite = false;
+
+ public function handle(): int
{
- parent::__construct();
+ if (config('filesystems.default') === 'local') {
+ error('Current disk is set to local. Nothing to move.');
+ note('Set PUBLIC_FILESYSTEM_DISK=s3_public and PRIVATE_FILESYSTEM_DISK=s3_private in your .env, then rerun.');
+
+ return self::FAILURE;
+ }
+
+ $publicDisk = config('filesystems.disks.public.driver', '?');
+ $privateDisk = config('filesystems.default', '?');
+ info("Target public disk: {$publicDisk}. Target private disk: {$privateDisk}.");
+
+ $this->overwrite = $this->option('overwrite')
+ || confirm(
+ label: 'Overwrite destination files that already exist? Choose "No" to skip them and keep whatever is already on the destination.',
+ default: false,
+ );
+
+ $publicSources = $this->collectPublicSources();
+ $privateSources = $this->collectPrivateSources();
+ $logoSources = glob('public/uploads/setting*.*') ?: [];
+
+ $this->copyPublic($publicSources);
+ $this->copyLogos($logoSources);
+ $this->copyPrivate($privateSources);
+
+ $this->renderSummary();
+
+ $deleteLocal = $this->option('delete-local')
+ || confirm(
+ label: 'Also delete the local source files? This cannot be undone.',
+ default: false,
+ );
+
+ if ($deleteLocal) {
+ $this->deleteLocalSources($publicSources, $privateSources, $logoSources);
+ }
+
+ return self::SUCCESS;
}
/**
- * Execute the console command.
- *
- * @return mixed
+ * @return array<string, array<int, string>>
*/
- public function handle()
+ private function collectPublicSources(): array
{
- if (config('filesystems.default') == 'local') {
- $this->error('Your current disk is set to local so we cannot proceed.');
- $this->warn("Please configure your .env settings for S3. \nChange your PUBLIC_FILESYSTEM_DISK value to 's3_public' and your PRIVATE_FILESYSTEM_DISK to s3_private.");
-
- return false;
- }
- $delete_local = $this->argument('delete_local');
-
- // Public upload directories, resolved through FileStorage.
- $public_uploads = [];
+ $sources = [];
foreach (FileStorage::cases() as $case) {
- if (! $case->hasPublicScope()) {
+ if (! $case->hasPublicScope() || $case === FileStorage::Barcodes) {
continue;
}
- if ($case === FileStorage::Barcodes) {
+ $sources[$case->value] = glob($case->publicDir().'/*.*') ?: [];
+ }
+
+ return $sources;
+ }
+
+ /**
+ * @return array<string, array<int, string>>
+ */
+ private function collectPrivateSources(): array
+ {
+ $sources = [];
+ foreach (FileStorage::cases() as $case) {
+ if (! $case->hasPrivateScope()) {
continue;
}
- $public_uploads[$case->value] = glob($case->publicDir().'/*.*');
+ $sources[$case->value] = glob($case->privateDir().'/*.*') ?: [];
}
- // iterate files
- foreach ($public_uploads as $public_type => $public_upload) {
- $type_count = 0;
- $this->info('- There are '.count($public_upload).' PUBLIC '.$public_type.' files.');
+ return $sources;
+ }
+
+ /**
+ * @param array<string, array<int, string>> $sources
+ */
+ private function copyPublic(array $sources): void
+ {
+ $disk = Storage::disk('public');
- for ($i = 0; $i < count($public_upload); $i++) {
- $type_count++;
- $filename = basename($public_upload[$i]);
+ foreach ($sources as $type => $files) {
+ if ($files === []) {
+ continue;
+ }
- try {
+ $copied = 0;
+ $skipped = 0;
+ $errors = 0;
+
+ progress(
+ label: "Copying public {$type}",
+ steps: $files,
+ callback: function (string $filepath) use ($type, $disk, &$copied, &$skipped, &$errors): void {
+ $filename = basename($filepath);
// The `public` disk is already rooted at the uploads
// directory on local and the per-install bucket root
// on S3, so the destination key is `<type>/<file>`
// without an `uploads/` prefix. The application reads
- // image URLs via `FileStorage::<Case>->publicPath()`
+ // image URLs via FileStorage::<Case>->publicPath()
// which emits `<type>/`. Prefixing here would land
// the file at a key the app never resolves.
- Storage::disk('public')->put($public_type.'/'.$filename, file_get_contents($public_upload[$i]));
- $new_url = Storage::disk('public')->url($public_type.'/'.$filename);
- $this->info($type_count.'. PUBLIC: '.$filename.' was copied to '.$new_url);
- } catch (\Exception $e) {
- Log::debug($e);
- $this->error($e);
- }
- }
- }
+ $key = $type.'/'.$filename;
+
+ if (! $this->overwrite && $disk->exists($key)) {
+ $skipped++;
- $logos = glob('public/uploads/setting*.*');
- $this->info('- There are '.count($logos).' files that might be logos.');
- $type_count = 0;
-
- foreach ($logos as $logo) {
- $this->info($logo);
- $type_count++;
- $filename = basename($logo);
- // Branding logos (logo.*, favicon.*, Setting-*) live at the
- // root of the public disk, so no subdirectory prefix.
- Storage::disk('public')->put($filename, file_get_contents($logo));
- $this->info($type_count.'. LOGO: '.$filename.' was copied to '.Storage::disk('public')->url($filename));
+ return;
+ }
+
+ try {
+ $disk->put($key, file_get_contents($filepath));
+ $copied++;
+ } catch (Throwable $e) {
+ Log::debug($e);
+ $errors++;
+ }
+ },
+ );
+
+ $this->tally($type, copied: $copied, skipped: $skipped, errors: $errors);
}
+ }
- // Private upload directories, resolved through FileStorage.
- $private_uploads = [];
- foreach (FileStorage::cases() as $case) {
- if (! $case->hasPrivateScope()) {
- continue;
- }
- $private_uploads[$case->value] = glob($case->privateDir().'/*.*');
+ /**
+ * @param array<int, string> $logoSources
+ */
+ private function copyLogos(array $logoSources): void
+ {
+ if ($logoSources === []) {
+ return;
}
- foreach ($private_uploads as $private_type => $private_upload) {
+ $disk = Storage::disk('public');
+ $copied = 0;
+ $skipped = 0;
+ $errors = 0;
- $this->info('- There are '.count($private_upload).' PRIVATE '.$private_type.' files.');
+ progress(
+ label: 'Copying branding logos',
+ steps: $logoSources,
+ callback: function (string $filepath) use ($disk, &$copied, &$skipped, &$errors): void {
+ $filename = basename($filepath);
+ // Branding logos (logo.*, favicon.*, Setting-*) live at
+ // the root of the public disk, so no subdirectory
+ // prefix.
+ if (! $this->overwrite && $disk->exists($filename)) {
+ $skipped++;
- $type_count = 0;
- for ($x = 0; $x < count($private_upload); $x++) {
- $type_count++;
- $filename = basename($private_upload[$x]);
+ return;
+ }
try {
- Storage::put($private_type.'/'.$filename, file_get_contents($private_upload[$x]));
- $new_url = Storage::url($private_type.'/'.$filename);
- $this->info($type_count.'. PRIVATE: '.$filename.' was copied to '.$new_url);
- } catch (\Exception $e) {
+ $disk->put($filename, file_get_contents($filepath));
+ $copied++;
+ } catch (Throwable $e) {
Log::debug($e);
- $this->error($e);
+ $errors++;
}
+ },
+ );
+
+ $this->tally('logos', copied: $copied, skipped: $skipped, errors: $errors);
+ }
+
+ /**
+ * @param array<string, array<int, string>> $sources
+ */
+ private function copyPrivate(array $sources): void
+ {
+ foreach ($sources as $type => $files) {
+ if ($files === []) {
+ continue;
}
- if ($delete_local == 'true') {
- $public_delete_count = 0;
- $private_delete_count = 0;
-
- $this->info("\n\n");
- $this->error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
- $this->warn("\nTHIS WILL DELETE ALL OF YOUR LOCAL UPLOADED FILES. \n\nThis cannot be undone, so you should take a backup of your system before you proceed.\n");
- $this->error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
-
- if ($this->confirm('Do you wish to continue?')) {
- foreach ($public_uploads as $public_type => $public_upload) {
- for ($i = 0; $i < count($public_upload); $i++) {
- $filename = $public_upload[$i];
- try {
- unlink($filename);
- $public_delete_count++;
- } catch (\Exception $e) {
- Log::debug($e);
- $this->error($e);
- }
- }
+ $copied = 0;
+ $skipped = 0;
+ $errors = 0;
+
+ progress(
+ label: "Copying private {$type}",
+ steps: $files,
+ callback: function (string $filepath) use ($type, &$copied, &$skipped, &$errors): void {
+ $filename = basename($filepath);
+ $key = $type.'/'.$filename;
+
+ if (! $this->overwrite && Storage::exists($key)) {
+ $skipped++;
+
+ return;
}
- foreach ($private_uploads as $private_type => $private_upload) {
- for ($i = 0; $i < count($private_upload); $i++) {
- $filename = $private_upload[$i];
- try {
- unlink($filename);
- $private_delete_count++;
- } catch (\Exception $e) {
- Log::debug($e);
- $this->error($e);
- }
- }
+ try {
+ Storage::put($key, file_get_contents($filepath));
+ $copied++;
+ } catch (Throwable $e) {
+ Log::debug($e);
+ $errors++;
}
+ },
+ );
+
+ $this->tally($type, private: $copied, private_skipped: $skipped, errors: $errors);
+ }
+ }
+
+ /**
+ * @param array<string, array<int, string>> $publicSources
+ * @param array<string, array<int, string>> $privateSources
+ * @param array<int, string> $logoSources
+ */
+ private function deleteLocalSources(array $publicSources, array $privateSources, array $logoSources): void
+ {
+ warning('Deleting local source files. This cannot be undone.');
+
+ $publicDeleted = 0;
+ $privateDeleted = 0;
+ $logoDeleted = 0;
+
+ foreach ($publicSources as $files) {
+ foreach ($files as $filepath) {
+ if ($this->tryUnlink($filepath)) {
+ $publicDeleted++;
+ }
+ }
+ }
- $this->info($public_delete_count.' PUBLIC local files and '.$private_delete_count.' PRIVATE local files were deleted from your filesystem.');
+ foreach ($privateSources as $files) {
+ foreach ($files as $filepath) {
+ if ($this->tryUnlink($filepath)) {
+ $privateDeleted++;
}
}
}
+
+ foreach ($logoSources as $filepath) {
+ if ($this->tryUnlink($filepath)) {
+ $logoDeleted++;
+ }
+ }
+
+ info("Deleted {$publicDeleted} public, {$privateDeleted} private, {$logoDeleted} branding file(s) from the local filesystem.");
+ }
+
+ private function tryUnlink(string $filepath): bool
+ {
+ try {
+ return @unlink($filepath);
+ } catch (Throwable $e) {
+ Log::debug($e);
+
+ return false;
+ }
+ }
+
+ /**
+ * Separate public / private counters so the summary table can show
+ * both axes without conflating them. "copied" and "skipped" are
+ * the public-side tallies, "private" and "private_skipped" the
+ * matching pair for the private disk. "errors" aggregates both.
+ */
+ private function tally(string $type, int $copied = 0, int $skipped = 0, int $private = 0, int $private_skipped = 0, int $errors = 0): void
+ {
+ if (! isset($this->summary[$type])) {
+ $this->summary[$type] = [
+ 'copied' => 0,
+ 'skipped' => 0,
+ 'private' => 0,
+ 'private_skipped' => 0,
+ 'errors' => 0,
+ ];
+ }
+ $this->summary[$type]['copied'] += $copied;
+ $this->summary[$type]['skipped'] += $skipped;
+ $this->summary[$type]['private'] += $private;
+ $this->summary[$type]['private_skipped'] += $private_skipped;
+ $this->summary[$type]['errors'] += $errors;
+ }
+
+ private function renderSummary(): void
+ {
+ if ($this->summary === []) {
+ info('No files found to move.');
+
+ return;
+ }
+
+ ksort($this->summary);
+
+ $rows = [];
+ $totals = [
+ 'copied' => 0,
+ 'skipped' => 0,
+ 'private' => 0,
+ 'private_skipped' => 0,
+ 'errors' => 0,
+ ];
+ foreach ($this->summary as $type => $counts) {
+ $rows[] = [
+ $type,
+ $counts['copied'],
+ $counts['skipped'],
+ $counts['private'],
+ $counts['private_skipped'],
+ $counts['errors'],
+ ];
+ foreach ($totals as $k => $_) {
+ $totals[$k] += $counts[$k];
+ }
+ }
+ $rows[] = [
+ 'TOTAL',
+ $totals['copied'],
+ $totals['skipped'],
+ $totals['private'],
+ $totals['private_skipped'],
+ $totals['errors'],
+ ];
+
+ table(['Type', 'Public copied', 'Public skipped', 'Private copied', 'Private skipped', 'Errors'], $rows);
}
}