From 144a6e469d7ee21e2b452269c842157ab25bee42 Mon Sep 17 00:00:00 2001 From: Nilesh Deepak Date: Fri, 7 Jul 2017 16:29:38 +0530 Subject: [PATCH] Updated cover image upload and delete function. --- app/Book.php | 18 ++++++++++ app/Http/Controllers/BookController.php | 32 ------------------ ...7_07_05_102650_add_cover_image_display.php | 2 +- public/default.png | Bin 0 -> 3338 bytes resources/assets/sass/styles.scss | 5 +++ resources/views/books/create.blade.php | 3 +- resources/views/books/edit.blade.php | 4 +-- resources/views/books/form.blade.php | 18 +++++++--- resources/views/books/grid-item.blade.php | 22 ++++++------ 9 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 public/default.png diff --git a/app/Book.php b/app/Book.php index 67bd4f5b2..a489acf8b 100644 --- a/app/Book.php +++ b/app/Book.php @@ -18,6 +18,24 @@ class Book extends Entity return baseUrl('/books/' . urlencode($this->slug)); } +public function getBookCover($size = 120) + { + $default = baseUrl('/default.png'); + $image = $this->image; + if ($image === 0 || $image === '0' || $image === null) + return $default; + try { + $cover = $this->cover ? baseUrl($this->cover->getThumb(120, 192, false)) : $default; + } catch (\Exception $err) { + $cover = $default; + } + return $cover; + } + +public function cover() + { + return $this->belongsTo(Image::class, 'image'); + } /* * Get the edit url for this book. * @return string diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index b64841220..eecb7839f 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -8,7 +8,6 @@ use BookStack\Services\ExportService; use Illuminate\Http\Request; use Illuminate\Http\Response; use Views; -use File; class BookController extends Controller { @@ -69,11 +68,7 @@ class BookController extends Controller 'name' => 'required|string|max:255', 'description' => 'string|max:1000' ]); - $image = $request->file('image'); - $path = $this->getBookCoverURL($image); $book = $this->entityRepo->createFromInput('book', $request->all()); - $book->image = $path; - $book->save(); Activity::add($book, 'book_create', $book->id); return redirect($book->getUrl()); } @@ -120,36 +115,11 @@ class BookController extends Controller 'name' => 'required|string|max:255', 'description' => 'string|max:1000' ]); - $image = $request->file('image'); - $path = $this->getBookCoverURL($image); $book = $this->entityRepo->updateFromInput('book', $book, $request->all()); - $book->image = $path; - $book->save(); Activity::add($book, 'book_update', $book->id); return redirect($book->getUrl()); } - /** - * Generate URL for book cover image - * @param $image - * @return $path - */ - private function getBookCoverURL($image) - { - if(is_null($image)) - { - return; - } - else - { - $input = time().'-'.$image->getClientOriginalName(); - $destinationPath = public_path('uploads/book/'); - $image->move($destinationPath, $input); - $path = baseUrl('/uploads/book/').'/'.$input; - return $path; - } - } - /** * Shows the page to confirm deletion * @param $bookSlug @@ -258,8 +228,6 @@ class BookController extends Controller $book = $this->entityRepo->getBySlug('book', $bookSlug); $this->checkOwnablePermission('book-delete', $book); Activity::addMessage('book_delete', 0, $book->name); - $file = basename($book->image); - File::delete('uploads/book/'.$file); $this->entityRepo->destroyBook($book); return redirect('/books'); } diff --git a/database/migrations/2017_07_05_102650_add_cover_image_display.php b/database/migrations/2017_07_05_102650_add_cover_image_display.php index d9a7f94ee..396112744 100644 --- a/database/migrations/2017_07_05_102650_add_cover_image_display.php +++ b/database/migrations/2017_07_05_102650_add_cover_image_display.php @@ -18,7 +18,7 @@ class AddCoverImageDisplay extends Migration }); Schema::table('books', function (Blueprint $table) { - $table->string('image'); + $table->integer('image'); }); } diff --git a/public/default.png b/public/default.png new file mode 100644 index 0000000000000000000000000000000000000000..1d7b737e7da36a919d9d389cbd980938cfae2435 GIT binary patch literal 3338 zcmd^BZCH|5AHS`o+j_B7&kS+FV!s8$aTJwEKV8HQ<(&?c) z7i3cQ!&kbY*6qhOrP-!c`Rv$r5JL$)W@B^w8K2qM=mOfV4~mKo*&M4V?zkllT*ZCa zdt$p^_#8nxJx@s1hKVpA3H$p|p`qdCxluv6lf~kdC4oCY{T{R)`g|Gw-!l07cKQ?1 z|LB(L*JP^Hav*zaRaMoD$*7}H-Yn$yX>|22#VKUZ>%ye_>Gs@s&2x=65fyVi;`(?+ zE@Dm(IkPZ$G%Rz9x@ZMz--^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U z^W0TcYF~ljCzM>b#i=)B8xU(yeP6dBK*tAlD{nWP_&gVtE}MugT1dBps>{TPN;dJr4c}1k7!?3Y^ zKBReJlfrAyFb>7yo&OhR>o)(oFsf5DAAO@InU8x!Hh@{gt6dj{-=Y{*L8L;^o1$ zTAFbI*<7Knm#Xi`fxkT1fA9YD3b^1(nZiKdEIuKMQxDhDy2?vxX)!Tfs2SyVs}#EQK$TV-ROJ?t@3GMjkO})8)+Lh32*mGG>Ze}Wp{|X$JN^9pmUCZ< zZa9C3Y5yBW)<3XqnBxw7PB`0=pOs3KHQ8qrK01|DGeitRIIJ6OzhsSOaV5$)g2Mny zg|cnv|G>v#9hUgIq54;O9qv$@qWYF~&;jH|Dx6rnT(3nF_)GM5ZyKJ6&%Ybp0d7X$ zzV*Z&ij&Of=O!yr$_ByRl#~>`xB%n-n&p&2KAJd%K#&&SZ9s+LKg3m$-_-eKb9=72 zN9o)XYS+EyEASsZPYLu!iaDAEwAs-}laKN-(wif4qWM+nJc%PXP}+dh@Hq&lS?jy7 zZ}%?jSP3m|Yk>+NXa)2>^vQo6{I#gRveTb2|DRhHKrdGPXS!kbW^ZUyU(rbxt~V6| OE#z=`VAB_;zWFz_;LFee literal 0 HcmV?d00001 diff --git a/resources/assets/sass/styles.scss b/resources/assets/sass/styles.scss index e15b14d96..aa3ac1069 100644 --- a/resources/assets/sass/styles.scss +++ b/resources/assets/sass/styles.scss @@ -63,6 +63,11 @@ body.dragging, body.dragging * { &.square { border-radius: 3px; } + &.cover { + height: 192px; + width: 120px; + border-radius: 3px; + } } // System wide notifications diff --git a/resources/views/books/create.blade.php b/resources/views/books/create.blade.php index 36c94af62..4d1edf78b 100644 --- a/resources/views/books/create.blade.php +++ b/resources/views/books/create.blade.php @@ -8,5 +8,6 @@ @include('books/form') - +


+ @include('components.image-manager', ['imageType' => 'cover']) @stop \ No newline at end of file diff --git a/resources/views/books/edit.blade.php b/resources/views/books/edit.blade.php index 02768b301..322f3d700 100644 --- a/resources/views/books/edit.blade.php +++ b/resources/views/books/edit.blade.php @@ -14,10 +14,10 @@

{{ trans('entities.books_edit') }}

-
+ @include('books/form', ['model' => $book])
- +@include('components.image-manager', ['imageType' => 'cover']) @stop \ No newline at end of file diff --git a/resources/views/books/form.blade.php b/resources/views/books/form.blade.php index afa6c48ea..4cc5d2ff3 100644 --- a/resources/views/books/form.blade.php +++ b/resources/views/books/form.blade.php @@ -9,11 +9,21 @@ @include('form/textarea', ['name' => 'description']) +
+ +

{{ trans('common.cover_image_description') }}

-
- - -
+ @include('components.image-picker', [ + 'resizeHeight' => '192', + 'resizeWidth' => '120', + 'showRemove' => true, + 'defaultImage' => baseUrl('/default.png'), + 'currentImage' => @isset($model) ? $model->getBookCover(80) : baseUrl('/default.png') , + 'currentId' => @isset($model) ? $model->image : 0, + 'name' => 'image', + 'imageClass' => 'avatar cover' + ]) +
{{ trans('common.cancel') }} diff --git a/resources/views/books/grid-item.blade.php b/resources/views/books/grid-item.blade.php index 095cccb24..d11a66eac 100644 --- a/resources/views/books/grid-item.blade.php +++ b/resources/views/books/grid-item.blade.php @@ -1,15 +1,15 @@
-

- {{$book->name}} -
- image === NULL) src="{{baseUrl('/default.jpg')}}" @else src="{{$book->image}}" @endif alt="{{$book->name}}"> -
-

- @if(isset($book->searchSnippet)) -

{!! $book->searchSnippet !!}

- @else +

+ {{$book->name}} +
+ {{$book->name}} +
+

+ @if(isset($book->searchSnippet)) +

{!! $book->searchSnippet !!}

+ @else

{{ $book->getExcerpt() }}

- @endif -
+ @endif +
\ No newline at end of file