Skip to content

Commit

Permalink
Feature/gallery update (#98)
Browse files Browse the repository at this point in the history
* added media library
  • Loading branch information
th0rn0 authored Jul 25, 2024
1 parent 18b4152 commit b0e4a07
Show file tree
Hide file tree
Showing 27 changed files with 1,074 additions and 162 deletions.
21 changes: 20 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
volumes:
- ./src:/var/www/html
env_file: $PWD/src/.env
user: "1000"
networks:
- internal
extra_hosts:
Expand Down Expand Up @@ -80,7 +81,25 @@ services:
target: php-base
entrypoint: [ "php", "artisan" ]
command: [ "--version" ]
# user: "1000"
user: "1000"
volumes:
- ./src:/var/www/html
env_file: $PWD/src/.env
networks:
- internal

queue-worker:
deploy:
mode: replicated
replicas: 5
restart: always
build:
context: .
dockerfile: resources/docker/php/Dockerfile
target: php-base
entrypoint: [ "php", "artisan" ]
command: [ "queue:work" ]
user: "1000"
volumes:
- ./src:/var/www/html
env_file: $PWD/src/.env
Expand Down
2 changes: 1 addition & 1 deletion resources/docker/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ server {
gzip on;
gzip_types text/html text/css application/javascript image/*;

client_max_body_size 100M;
client_max_body_size 4096M;

location ~ \.php$ {
try_files $uri =404;
Expand Down
7 changes: 5 additions & 2 deletions resources/docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ RUN apt-get update && apt-get install -y \
libicu-dev \
libzip-dev \
libxml2-dev \
libonig-dev \
&& docker-php-ext-install mysqli pdo_mysql gd bcmath intl zip xml mbstring
libonig-dev

RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg

RUN docker-php-ext-install mysqli pdo_mysql gd bcmath intl zip xml mbstring exif

COPY src/ /var/www/html

Expand Down
6 changes: 3 additions & 3 deletions resources/docker/php/uploads.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
file_uploads = On
memory_limit = 500M
upload_max_filesize = 100M
post_max_size = 100M
memory_limit = 4096M
upload_max_filesize = 4096M
post_max_size = 4096M
max_execution_time = 600
max_file_uploads = 50000
max_execution_time = 5000
Expand Down
76 changes: 19 additions & 57 deletions src/app/Http/Controllers/Admin/GalleryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Image;
use Session;
use File;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Storage;

use App\Models\GalleryAlbum;
use App\Models\GalleryAlbumImage;
Expand All @@ -15,6 +17,10 @@
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;

use Spatie\Image\Manipulations;

use App\Jobs\ProcessUploadedImages;

class GalleryController extends Controller
{
/**
Expand All @@ -36,7 +42,7 @@ public function show(GalleryAlbum $album)
{
return view('admin.gallery.show')
->withAlbum($album)
->withImages($album->images()->paginate(10))
->withImages($album->getMedia('images'))
;
}

Expand Down Expand Up @@ -129,7 +135,7 @@ public function destroy(GalleryAlbum $album)
}

Session::flash('alert-success', 'Successfully deleted Gallery!');
return Redirect::back();
return Redirect::to('admin/gallery');
}

/**
Expand All @@ -148,40 +154,12 @@ public function uploadImage(GalleryAlbum $album, Request $request)
];
$this->validate($request, $rules, $messages);

$files = Input::file('images');
//Keep a count of uploaded files
$fileCount = count($files);
//Counter for uploaded files
$uploadcount = 0;
$destinationPath = '/storage/images/gallery/' . $album->slug . '/';
if (Input::file('images') && !File::exists(public_path() . $destinationPath)) {
File::makeDirectory(public_path() . $destinationPath, 0777, true);
}
foreach ($files as $file) {
$imageName = $file->getClientOriginalName();

$image = new GalleryAlbumImage();
$image->display_name = $imageName;
$image->nice_name = $image->url = strtolower(str_replace(' ', '-', $imageName));
$image->gallery_album_id = $album->id;
Image::make($file)
->resize(null, 1080, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
->save(public_path() . $destinationPath . $imageName)
;
$image->path = $destinationPath . $imageName;
$image->save();

$uploadcount++;
}
if ($uploadcount != $fileCount) {
Session::flash('alert-danger', 'Upload unsuccessful!');
return Redirect::to('admin/gallery/' . $album->slug);
}
$fileAdders = $album->addMultipleMediaFromRequest(['images'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('images');
});

Session::flash('alert-success', 'Upload successful!');
Session::flash('alert-success', 'Upload successful! Processing images please wait!');
return Redirect::to('admin/gallery/' . $album->slug);
}

Expand All @@ -191,7 +169,7 @@ public function uploadImage(GalleryAlbum $album, Request $request)
* @param GalleryAlbumImage $image
* @return Redirect
*/
public function destroyImage(GalleryAlbum $album, GalleryAlbumImage $image)
public function destroyImage(GalleryAlbum $album, Media $image)
{
if (!$image->delete()) {
Session::flash('alert-danger', 'Cannot delete Image!');
Expand All @@ -203,29 +181,13 @@ public function destroyImage(GalleryAlbum $album, GalleryAlbumImage $image)
}

/**
* Update Image from Gallery
* Ingest images from upload dir
* @param GalleryAlbum $album
* @param GalleryAlbumImage $image
* @param Request $request
* @return Redirect
*/
public function updateImage(GalleryAlbum $album, GalleryAlbumImage $image, Request $request)
{
//DEBUG - Refactor - replace iamge name as well!
$image->display_name = $request->name;
$image->nice_name = strtolower(str_replace(' ', '-', $request->name));
$image->desc = $request->desc;

if (isset($request->album_cover) && $request->album_cover) {
$album->setAlbumCover($image->id);
}

if (!$image->save()) {
Session::flash('alert-danger', 'Could not update!');
return Redirect::back();
}

Session::flash('alert-success', 'Successfully updated!');
return Redirect::back();
public function ingestImages(GalleryAlbum $album) {
ProcessUploadedImages::dispatch($album);
Session::flash('alert-info', 'Upload Started! Please check back in a while...');
return Redirect::to('admin/gallery/' . $album->slug);
}
}
2 changes: 1 addition & 1 deletion src/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@
Route::get('/admin/gallery/{album}', 'Admin\GalleryController@show');
Route::post('/admin/gallery/{album}', 'Admin\GalleryController@update');
Route::delete('/admin/gallery/{album}', 'Admin\GalleryController@destroy');
Route::get('/admin/gallery/{album}/ingest', 'Admin\GalleryController@ingestImages');
Route::post('/admin/gallery/{album}/upload', 'Admin\GalleryController@uploadImage');
Route::post('/admin/gallery/{album}/{image}', 'Admin\GalleryController@updateImage');
Route::delete('/admin/gallery/{album}/{image}', 'Admin\GalleryController@destroyImage');

/**
Expand Down
44 changes: 44 additions & 0 deletions src/app/Jobs/ProcessUploadedImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Jobs;

use Storage;
use App\Models\GalleryAlbum;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessUploadedImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $file;
protected $album;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GalleryAlbum $album, $file)
{
$this->album = $album;
$this->file = $file;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (in_array(Storage::disk('gallery-ingest')->mimeType($this->file), ['image/jpeg','image/png'])) {
$this->album->addMediaFromDisk($this->file, 'gallery-ingest')->toMediaCollection('images');
}
}
}
45 changes: 45 additions & 0 deletions src/app/Jobs/ProcessUploadedImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Jobs;

use Storage;
use App\Models\GalleryAlbum;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessUploadedImages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $album;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GalleryAlbum $album)
{
$this->album = $album;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$files = Storage::disk('gallery-ingest')->allFiles();
foreach($files as $file) {
if (in_array(Storage::disk('gallery-ingest')->mimeType($file), ['image/jpeg','image/png'])) {
ProcessUploadedImage::dispatch($this->album, $file);
}
}
}
}
41 changes: 41 additions & 0 deletions src/app/Libraries/PathGenerators/GalleryPathGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Libraries\PathGenerators;

use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class GalleryPathGenerator implements PathGenerator
{
/*
* Get the path for the given media, relative to the root storage path.
*/
public function getPath(Media $media): string
{
return $this->getBasePath($media).'/';
}

/*
* Get the path for conversions of the given media, relative to the root storage path.
*/
public function getPathForConversions(Media $media): string
{
return $this->getBasePath($media).'/conversions/';
}

/*
* Get the path for responsive images of the given media, relative to the root storage path.
*/
public function getPathForResponsiveImages(Media $media): string
{
return $this->getBasePath($media).'/responsive/';
}

/*
* Get a unique base path for the given media.
*/
protected function getBasePath(Media $media): string
{
return "images/galleries/" . $media->model_id . "/" . $media->getKey();
}
}
Loading

0 comments on commit b0e4a07

Please sign in to comment.