Skip to content

Commit

Permalink
Add module to fix the missing file sizes from size variants (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Apr 6, 2024
1 parent 8901c34 commit cb409b5
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 0 deletions.
93 changes: 93 additions & 0 deletions app/Livewire/Components/Modules/Maintenance/MissingFileSizes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Livewire\Components\Modules\Maintenance;

use App\Livewire\Traits\Notify;
use App\Models\Configs;
use App\Models\SizeVariant;
use App\Policies\SettingsPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Livewire\Component;

/**
* We may be missing some file sizes because of generation problems,
* transfer of files, or other.
* This module aims to solve this issue.
*/
class MissingFileSizes extends Component
{
use Notify;

/**
* Mount depending of the path.
*
* @return void
*/
public function mount(): void
{
Gate::authorize(SettingsPolicy::CAN_UPDATE, Configs::class);
}

/**
* Rendering of the front-end.
*
* @return View
*/
public function render(): View
{
Gate::authorize(SettingsPolicy::CAN_UPDATE, Configs::class);

return view('livewire.modules.maintenance.fill-filesize-sizevariants');
}

/**
* Fetch the file size of existing size variants when the data is not in the DB
* Process by chunks of 500.
*
* @return void
*/
public function do(): void
{
Gate::authorize(SettingsPolicy::CAN_UPDATE, Configs::class);

$variants_query = SizeVariant::query()
->where('filesize', '=', 0)
// TODO: remove s3 support here.
->orderBy('id');
// Internally, only holds $limit entries at once
$variants = $variants_query->lazyById(500);

$generated = 0;

foreach ($variants as $variant) {
$variantFile = $variant->getFile();
if ($variantFile->exists()) {
$variant->filesize = $variantFile->getFilesize();
if (!$variant->save()) {
Log::error('Failed to update filesize for ' . $variantFile->getRelativePath() . '.');
} else {
$generated++;
}
} else {
Log::error('No file found at ' . $variantFile->getRelativePath() . '.');
}
}

$this->notify(sprintf(__('maintenance.fill-filesize-sizevariants.success'), $generated));
}

/**
* Check how many images needs to be created.
*
* @return int
*/
public function getNumberOfMissingSizeProperty(): int
{
return SizeVariant::query()
->where('filesize', '=', 0)
// TODO: remove s3 support here.
->count();
}
}
6 changes: 6 additions & 0 deletions lang/cz/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/de/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/el/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/en/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/es/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/fr/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Généré!',
'success' => 'Nous avons créé %d %s avec succès.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Statistique d’arbres',
'Oddness' => 'Imparité',
Expand Down
6 changes: 6 additions & 0 deletions lang/hu/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/it/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/nl/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/no/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/pl/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/pt/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/ru/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/sk/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/sv/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/vi/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/zh_CN/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
6 changes: 6 additions & 0 deletions lang/zh_TW/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
'button' => 'Generate!',
'success' => 'Successfully generated %d %s.',
],
'fill-filesize-sizevariants' => [
'title' => 'File sizes missing',
'description' => 'Found %d small variants without file size.',
'button' => 'Fetch data!',
'success' => 'Successfully computed sizes of %d small variants.',
],
'fix-tree' => [
'title' => 'Tree statistics',
'Oddness' => 'Oddness',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<x-maintenance.card :disabled="$this->number_of_missing_size <= 0">
<x-maintenance.h1>{{ __('maintenance.fill-filesize-sizevariants.title') }}</x-maintenance.h1>
<x-maintenance.p>{!! sprintf(__('maintenance.fill-filesize-sizevariants.description'), $this->number_of_missing_size) !!}</x-maintenance.p>
<x-maintenance.button wire:click="do">{{ __('maintenance.fill-filesize-sizevariants.button') }}</x-maintenance.button>
<x-maintenance.loading />
</x-maintenance.card>
1 change: 1 addition & 0 deletions resources/views/livewire/pages/maintenance.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<livewire:modules.maintenance.gen-size-variants :type="\App\Enum\SizeVariantType::SMALL2X->value" />
<livewire:modules.maintenance.gen-size-variants :type="\App\Enum\SizeVariantType::MEDIUM->value" />
<livewire:modules.maintenance.gen-size-variants :type="\App\Enum\SizeVariantType::MEDIUM2X->value" />
<livewire:modules.maintenance.missing-file-sizes />
</div>
</div>
</div>
Expand Down

0 comments on commit cb409b5

Please sign in to comment.