From c706b8368cf8958697b367c78c50c85f61b13e0e Mon Sep 17 00:00:00 2001 From: Spitfire Date: Thu, 1 Aug 2024 12:01:05 -0600 Subject: [PATCH 1/7] Entity Assets now use Gallery --- .../Controllers/Entity/AssetController.php | 4 +- app/Models/EntityAsset.php | 20 ++++++++++ app/Models/Image.php | 5 +++ app/Models/Relations/EntityRelations.php | 1 + app/Observers/EntityAssetObserver.php | 5 +++ app/Services/Caches/CharacterCacheService.php | 9 +++-- app/Services/Campaign/ExportService.php | 3 ++ app/Services/Campaign/GalleryService.php | 1 - .../Campaign/Import/Mappers/EntityMapper.php | 37 +++++++++++------- .../Campaign/Import/Mappers/GalleryMapper.php | 2 + app/Services/EntityFileService.php | 39 ++++++++++++++++--- ...08_update_entity_assets_add_image_uuid.php | 30 ++++++++++++++ .../entities/components/assets.blade.php | 2 +- resources/views/gallery/_image.blade.php | 2 +- .../views/gallery/file/_actions.blade.php | 2 +- resources/views/gallery/file/_form.blade.php | 4 +- 16 files changed, 137 insertions(+), 29 deletions(-) create mode 100644 database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php diff --git a/app/Http/Controllers/Entity/AssetController.php b/app/Http/Controllers/Entity/AssetController.php index 55451ab2da..3f22ca0b7a 100644 --- a/app/Http/Controllers/Entity/AssetController.php +++ b/app/Http/Controllers/Entity/AssetController.php @@ -30,7 +30,7 @@ public function index(Campaign $campaign, Entity $entity) ); } - $assets = $entity->assets; + $assets = $entity->assets()->with('image')->get(); return view('entities.pages.assets.index', compact( 'campaign', @@ -102,7 +102,7 @@ protected function storeFile(StoreEntityAsset $request, Campaign $campaign, Enti $file = $service ->entity($entity) ->campaign($campaign) - ->upload($request, 'file', 'w/' . $campaign->id . '/entity-assets'); + ->upload($request, 'file'); return redirect() ->route('entities.entity_assets.index', [$campaign, $entity]) diff --git a/app/Models/EntityAsset.php b/app/Models/EntityAsset.php index c448000c7e..936a555109 100644 --- a/app/Models/EntityAsset.php +++ b/app/Models/EntityAsset.php @@ -16,11 +16,13 @@ use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasOne; /** * @property int $id * @property int $type_id * @property int $entity_id + * @property string|null $image_uuid * @property string $name * @property array $metadata * @property Carbon $created_at @@ -49,6 +51,7 @@ class EntityAsset extends Model 'metadata', 'visibility_id', 'is_pinned', + 'image_uuid', ]; public $casts = [ @@ -104,6 +107,10 @@ public function isImage(): bool */ public function imageUrl(): string { + if ($this->image) { + return $this->image->getUrl(120, 80); + } + return Img::crop(120, 80)->url($this->metadata['path']); } @@ -126,6 +133,7 @@ public function getIconAttribute(): mixed /** * A virtual getter for the image path for the image observer delete loop */ + //check this public function getImagePathAttribute(): string { return (string) $this->metadata['path']; @@ -157,6 +165,10 @@ public function urlDomain(): string public function url(): string { + if ($this->image_uuid) { + return $this->image->getUrl(); + } + $path = $this->metadata['path']; $cloudfront = config('filesystems.disks.cloudfront.url'); if ($cloudfront) { @@ -164,4 +176,12 @@ public function url(): string } return Storage::url($path); } + + /** + * Image stored in the gallery + */ + public function image(): HasOne + { + return $this->hasOne('App\Models\Image', 'id', 'image_uuid'); + } } diff --git a/app/Models/Image.php b/app/Models/Image.php index c3cc63c920..c9e61ac90d 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -290,6 +290,11 @@ public function isFont(): bool return in_array($this->ext, ['woff', 'woff2']); } + public function isFile(): bool + { + return in_array($this->ext, ['pdf', 'gif', 'webp', 'pdf', 'xls', 'xlsx', 'csv', 'mp3', 'ogg', 'json']); + } + public function getUrl(?int $sizeX = null, ?int $sizeY = null): string { if ($this->isSvg()) { diff --git a/app/Models/Relations/EntityRelations.php b/app/Models/Relations/EntityRelations.php index c1fcf33f09..60717dac05 100644 --- a/app/Models/Relations/EntityRelations.php +++ b/app/Models/Relations/EntityRelations.php @@ -290,6 +290,7 @@ public function pinnedFiles(): HasMany { return $this->files() ->where('is_pinned', 1) + ->with('image') ; } diff --git a/app/Observers/EntityAssetObserver.php b/app/Observers/EntityAssetObserver.php index 0f0ca989ad..8a369c1a23 100644 --- a/app/Observers/EntityAssetObserver.php +++ b/app/Observers/EntityAssetObserver.php @@ -2,6 +2,7 @@ namespace App\Observers; +use App\Facades\CampaignCache; use App\Models\EntityAsset; use App\Facades\Images; @@ -12,6 +13,10 @@ class EntityAssetObserver */ public function saved(EntityAsset $entityAsset) { + if ($entityAsset->isFile()) { + CampaignCache::clear(); + } + // When adding or changing an asset to an entity, we want to update the // last updated date to reflect changes in the dashboard. $entityAsset->entity->child->touchQuietly(); diff --git a/app/Services/Caches/CharacterCacheService.php b/app/Services/Caches/CharacterCacheService.php index dbc89a5914..eba0637988 100644 --- a/app/Services/Caches/CharacterCacheService.php +++ b/app/Services/Caches/CharacterCacheService.php @@ -38,9 +38,12 @@ public function genderSuggestion(): array */ public function clearSuggestion(): self { - $this->forget( - $this->genderSuggestionKey() - ); + if (isset($this->campaign)) { + $this->forget( + $this->genderSuggestionKey() + ); + } + return $this; } diff --git a/app/Services/Campaign/ExportService.php b/app/Services/Campaign/ExportService.php index 207da086f3..2fe25121dd 100644 --- a/app/Services/Campaign/ExportService.php +++ b/app/Services/Campaign/ExportService.php @@ -315,6 +315,9 @@ protected function process(string $entity, $model): self /** @var EntityAsset $file */ foreach ($model->entity->files as $file) { + if (!isset($file->metadata['path'])) { + continue; + } $path = $file->metadata['path']; if (!Storage::exists($path)) { continue; diff --git a/app/Services/Campaign/GalleryService.php b/app/Services/Campaign/GalleryService.php index 02ef2d4b83..d60d472681 100644 --- a/app/Services/Campaign/GalleryService.php +++ b/app/Services/Campaign/GalleryService.php @@ -105,7 +105,6 @@ public function storageInfo(): array ]; } - /** * Total size in mb */ diff --git a/app/Services/Campaign/Import/Mappers/EntityMapper.php b/app/Services/Campaign/Import/Mappers/EntityMapper.php index 325a946173..d03aefa826 100644 --- a/app/Services/Campaign/Import/Mappers/EntityMapper.php +++ b/app/Services/Campaign/Import/Mappers/EntityMapper.php @@ -154,12 +154,26 @@ protected function migrateToGallery(string $old): self return $this; } + $image = $this->migrateImage($img); + + if ($old == 'image_path') { + $this->entity->image_uuid = ImportIdMapper::getGallery($image->id); + } else { + $this->entity->header_uuid = ImportIdMapper::getGallery($image->id); + } + + return $this; + } + + protected function migrateImage(string $img): Image + { $imageExt = Str::after($img, '.'); //We need to create a new Image to migrate to the new system. $image = new Image(); $image->campaign_id = $this->campaign->id; $image->ext = $imageExt; + $image->created_by = $this->user->id; $image->name = $this->entity->name; $image->visibility_id = 1; $size = Storage::disk('local')->size($this->path . $img); @@ -170,13 +184,7 @@ protected function migrateToGallery(string $old): self Storage::writeStream($image->path, Storage::disk('local')->readStream($this->path . $img)); ImportIdMapper::putGallery($image->id, $image->id); - if ($old == 'image_path') { - $this->entity->image_uuid = ImportIdMapper::getGallery($image->id); - } else { - $this->entity->header_uuid = ImportIdMapper::getGallery($image->id); - } - - return $this; + return $image; } protected function gallery(): self @@ -213,7 +221,7 @@ protected function posts(): self foreach ($import as $field) { $post->$field = $data[$field]; } - if (!empty($data['location_id'])) { + if (!empty($data['location_id']) && ImportIdMapper::has('locations', $data['location_id'])) { $locationID = ImportIdMapper::get('locations', $data['location_id']); if (!empty($locationID)) { $post->location_id = $locationID; @@ -257,20 +265,23 @@ protected function assets(): self if (!empty($data['metadata'])) { if (!empty($data['metadata']['path'])) { $img = $data['metadata']['path']; - $ext = Str::afterLast($img, '.'); - $destination = 'w/' . $this->campaign->id . '/entity-assets/' . uniqid() . '.' . $ext; - if (!Storage::disk('local')->exists($this->path . $img)) { //dd('image ' . $this->path . $img . ' doesnt exist'); continue; } - Storage::writeStream($destination, Storage::disk('local')->readStream($this->path . $img)); - $data['metadata']['path'] = $destination; + + $image = $this->migrateImage($img); + $asset->image_uuid = $image->id; + unset($data['metadata']['path']); $asset->metadata = $data['metadata']; + } else { $asset->metadata = $data['metadata']; } } + if (isset($data['image_uuid'])) { + $asset->image_uuid = ImportIdMapper::getGallery($data['image_uuid']); + } $asset->created_by = $this->user->id; $asset->save(); } diff --git a/app/Services/Campaign/Import/Mappers/GalleryMapper.php b/app/Services/Campaign/Import/Mappers/GalleryMapper.php index bafaaafa72..d6b6e6e9ea 100644 --- a/app/Services/Campaign/Import/Mappers/GalleryMapper.php +++ b/app/Services/Campaign/Import/Mappers/GalleryMapper.php @@ -45,6 +45,8 @@ public function prepare(): self public function import(): void { $this->image = new Image(); + //Need to set the id before saving otherwise it crashes + $this->image->id = Str::uuid(); $this->image->campaign_id = $this->campaign->id; $this->mapping[$this->data['id']] = $this->image->id; ImportIdMapper::putGallery($this->data['id'], $this->image->id); diff --git a/app/Services/EntityFileService.php b/app/Services/EntityFileService.php index 39f44671e8..2eb34d2659 100644 --- a/app/Services/EntityFileService.php +++ b/app/Services/EntityFileService.php @@ -5,8 +5,12 @@ use App\Exceptions\EntityFileException; use App\Http\Requests\StoreEntityAsset; use App\Models\EntityAsset; +use App\Models\Image; +use App\Services\Campaign\GalleryService; use App\Traits\CampaignAware; use App\Traits\EntityAware; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Str; class EntityFileService { @@ -16,33 +20,56 @@ class EntityFileService /** * @throws EntityFileException */ - public function upload(StoreEntityAsset $request, string $field = 'file', string $folder = 'entities/files'): EntityAsset + public function upload(StoreEntityAsset $request, string $field = 'file'): EntityAsset { + /** @var GalleryService $service */ + $service = app()->make(GalleryService::class); + + // Prepare the file for the journey + $uploadedFile = $request->file($field); + // Already above max capacity? - if ($this->entity->files->count() >= $this->campaign->maxEntityFiles()) { + if ($this->entity->files->count() >= $this->campaign->maxEntityFiles() || $service->campaign($this->campaign)->available() < $uploadedFile->getSize() / 1024) { throw new EntityFileException('max'); } - // Prepare the file for the journey - $uploadedFile = $request->file($field); - $path = $request->file($field)->storePublicly($folder); $name = $request->get('name'); if (empty($name)) { $name = $uploadedFile->getClientOriginalName(); + $name = Str::beforeLast($name, '.'); } + $image = new Image(); + $image->campaign_id = $this->campaign->id; + $image->created_by = auth()->user()->id; + $image->ext = $uploadedFile->extension(); + $image->size = (int) ceil($uploadedFile->getSize() / 1024); // kb + $image->name = mb_substr($name, 0, 45); + $image->visibility_id = $this->campaign->defaultVisibilityID(); + $image->save(); + + $uploadedFile + ->storePubliclyAs( + $image->folder, + $image->file, + ['disk' => 's3'] + ); + $file = new EntityAsset(); $file->type_id = EntityAsset::TYPE_FILE; $file->entity_id = $this->entity->id; $file->metadata = [ - 'path' => $path, 'size' => $uploadedFile->getSize(), 'type' => $uploadedFile->getMimeType(), ]; $file->name = $name; $file->visibility_id = $request->get('visibility_id', 1); $file->is_pinned = $request->get('is_pinned', 1); + $file->image_uuid = $image->id; $file->save(); + + Cache::forget('campaign_' . $this->campaign->id . '_gallery'); + return $file; } } diff --git a/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php b/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php new file mode 100644 index 0000000000..e09b7fb482 --- /dev/null +++ b/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php @@ -0,0 +1,30 @@ +uuid('image_uuid')->nullable(); + $table->foreign('image_uuid')->references('id')->on('images')->cascadeOnDelete(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('entity_assets', function (Blueprint $table) { + $table->dropForeign('entity_assets_image_uuid_foreign'); + $table->dropColumn('image_uuid'); + }); + } +}; diff --git a/resources/views/entities/components/assets.blade.php b/resources/views/entities/components/assets.blade.php index c6f774a643..a528cd3beb 100644 --- a/resources/views/entities/components/assets.blade.php +++ b/resources/views/entities/components/assets.blade.php @@ -5,7 +5,7 @@ */ ?> @foreach ($model->entity->pinnedFiles as $asset) - + {{ $asset->name }} @endforeach diff --git a/resources/views/gallery/_image.blade.php b/resources/views/gallery/_image.blade.php index 43fc344aa1..4d414eaa83 100644 --- a/resources/views/gallery/_image.blade.php +++ b/resources/views/gallery/_image.blade.php @@ -22,7 +22,7 @@ @else - @if ($image->isFont()) + @if ($image->isFont() || $image->isFile())
diff --git a/resources/views/gallery/file/_actions.blade.php b/resources/views/gallery/file/_actions.blade.php index 3743b17b31..5c9f84b8cf 100644 --- a/resources/views/gallery/file/_actions.blade.php +++ b/resources/views/gallery/file/_actions.blade.php @@ -1,6 +1,6 @@ @can('edit', [$image, $campaign]) - @if (!$image->isFont() && !$image->isFolder()) + @if (!$image->isFont() && !$image->isFolder() && !$image->isFile()) + @else +
+ +
@endif
@include('icons.visibility', ['icon' => $image->visibilityIcon()]) diff --git a/resources/views/gallery/file/_actions.blade.php b/resources/views/gallery/file/_actions.blade.php index 5c9f84b8cf..be9dc2a70c 100644 --- a/resources/views/gallery/file/_actions.blade.php +++ b/resources/views/gallery/file/_actions.blade.php @@ -1,6 +1,6 @@ @can('edit', [$image, $campaign]) - @if (!$image->isFont() && !$image->isFolder() && !$image->isFile()) + @if ($image->hasThumbnail())
@else - @if ($image->isFont()) - - @elseif ($image->isFile()) - - @else + @if ($image->hasThumbnail())
{{ $image->name }}
+ @else + @endif
- @if (!$image->isFont() && !$image->isFile()) + @if (!$image->isFont())

{{ trans_choice('campaigns/gallery.fields.image_used_in', $image->inEntitiesCount(), ['count' => $image->inEntitiesCount()]) }}

From fbcc0f40ac6fd5a992acd871afc935fc53f7546f Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Sat, 3 Aug 2024 00:31:08 +0000 Subject: [PATCH 4/7] Fix styling --- app/Models/Image.php | 2 +- app/Observers/EntityAssetObserver.php | 1 - app/Services/Campaign/Import/Mappers/GalleryMapper.php | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Models/Image.php b/app/Models/Image.php index 430b6de345..bdef9715fd 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -168,7 +168,7 @@ public function inEntities(): array if (isset($entities[$asset->entity->id])) { continue; } - $entities[$asset->entity->id] = $asset->entity; + $entities[$asset->entity->id] = $asset->entity; } return $entities; diff --git a/app/Observers/EntityAssetObserver.php b/app/Observers/EntityAssetObserver.php index 7877b1e631..0f0ca989ad 100644 --- a/app/Observers/EntityAssetObserver.php +++ b/app/Observers/EntityAssetObserver.php @@ -2,7 +2,6 @@ namespace App\Observers; -use App\Facades\CampaignCache; use App\Models\EntityAsset; use App\Facades\Images; diff --git a/app/Services/Campaign/Import/Mappers/GalleryMapper.php b/app/Services/Campaign/Import/Mappers/GalleryMapper.php index af3d9b261c..d26454ae19 100644 --- a/app/Services/Campaign/Import/Mappers/GalleryMapper.php +++ b/app/Services/Campaign/Import/Mappers/GalleryMapper.php @@ -8,7 +8,6 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Str; class GalleryMapper { From 520daedd2f15565d243e42f5928edbbc15e47452 Mon Sep 17 00:00:00 2001 From: ilestis Date: Sat, 3 Aug 2024 16:24:04 -0600 Subject: [PATCH 5/7] Fix merge --- app/Models/EntityAsset.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Models/EntityAsset.php b/app/Models/EntityAsset.php index b7fea4481e..176370aa63 100644 --- a/app/Models/EntityAsset.php +++ b/app/Models/EntityAsset.php @@ -16,7 +16,6 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasOne; /** From b017a98b48e97d3380b04b6f7ac7a97efb1fafba Mon Sep 17 00:00:00 2001 From: ilestis Date: Sat, 3 Aug 2024 16:25:18 -0600 Subject: [PATCH 6/7] Fix for laravel 11 --- .../2024_07_26_160208_update_entity_assets_add_image_uuid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php b/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php index 46ff3a1108..9e6319fed4 100644 --- a/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php +++ b/database/migrations/2024_07_26_160208_update_entity_assets_add_image_uuid.php @@ -11,7 +11,7 @@ public function up(): void { Schema::table('entity_assets', function (Blueprint $table) { - $table->uuid('image_uuid')->nullable(); + $table->char('image_uuid', 36)->nullable(); $table->foreign('image_uuid')->references('id')->on('images')->cascadeOnDelete(); }); } From 65fd592ea55d41463eb3bb843be7cd67d48264c9 Mon Sep 17 00:00:00 2001 From: ilestis Date: Sat, 3 Aug 2024 19:46:25 -0600 Subject: [PATCH 7/7] Fix url when clicking on an asset stored in the gallery --- app/Models/EntityAsset.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Models/EntityAsset.php b/app/Models/EntityAsset.php index 176370aa63..136d68a1f0 100644 --- a/app/Models/EntityAsset.php +++ b/app/Models/EntityAsset.php @@ -29,6 +29,7 @@ * @property Carbon $updated_at * @property Entity|null $entity * @property bool $is_pinned + * @property ?Image $image * */ class EntityAsset extends Model @@ -169,7 +170,7 @@ public function urlDomain(): string public function url(): string { if ($this->image_uuid) { - return $this->image->getUrl(); + return $this->image->url(); } $path = $this->metadata['path'];