Updated cover image upload and delete function.
This commit is contained in:
		
							parent
							
								
									c5f11e4516
								
							
						
					
					
						commit
						144a6e469d
					
				
							
								
								
									
										18
									
								
								app/Book.php
								
								
								
								
							
							
						
						
									
										18
									
								
								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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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');
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,7 +18,7 @@ class AddCoverImageDisplay extends Migration
 | 
			
		|||
        });
 | 
			
		||||
 | 
			
		||||
        Schema::table('books', function (Blueprint $table) {
 | 
			
		||||
            $table->string('image');
 | 
			
		||||
            $table->integer('image');
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 3.3 KiB  | 
| 
						 | 
				
			
			@ -63,6 +63,11 @@ body.dragging, body.dragging * {
 | 
			
		|||
  &.square {
 | 
			
		||||
    border-radius: 3px;
 | 
			
		||||
  }
 | 
			
		||||
  &.cover {
 | 
			
		||||
    height: 192px;
 | 
			
		||||
    width: 120px;
 | 
			
		||||
    border-radius: 3px;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// System wide notifications
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,5 +8,6 @@
 | 
			
		|||
        @include('books/form')
 | 
			
		||||
    </form>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<p class="margin-top large"><br></p>
 | 
			
		||||
    @include('components.image-manager', ['imageType' => 'cover'])
 | 
			
		||||
@stop
 | 
			
		||||
| 
						 | 
				
			
			@ -14,10 +14,10 @@
 | 
			
		|||
 | 
			
		||||
    <div class="container small" ng-non-bindable>
 | 
			
		||||
        <h1>{{ trans('entities.books_edit') }}</h1>
 | 
			
		||||
        <form action="{{ $book->getUrl() }}" method="POST" enctype="multipart/form-data">
 | 
			
		||||
        <form action="{{ $book->getUrl() }}" method="POST">
 | 
			
		||||
            <input type="hidden" name="_method" value="PUT">
 | 
			
		||||
            @include('books/form', ['model' => $book])
 | 
			
		||||
        </form>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
@include('components.image-manager', ['imageType' => 'cover'])
 | 
			
		||||
@stop
 | 
			
		||||
| 
						 | 
				
			
			@ -9,11 +9,21 @@
 | 
			
		|||
    <label for="description">{{ trans('common.description') }}</label>
 | 
			
		||||
    @include('form/textarea', ['name' => 'description'])
 | 
			
		||||
</div>
 | 
			
		||||
<div class="form-group" id="logo-control">
 | 
			
		||||
                        <label for="user-avatar">{{ trans('common.cover_image') }}</label>
 | 
			
		||||
                        <p class="small">{{ trans('common.cover_image_description') }}</p>
 | 
			
		||||
 | 
			
		||||
<div class="form-group">
 | 
			
		||||
    <label for="image">{{ trans('common.cover_image') }}</label>
 | 
			
		||||
    <input type="file" name="image">
 | 
			
		||||
</div>
 | 
			
		||||
                        @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'
 | 
			
		||||
                          ])
 | 
			
		||||
                    </div>
 | 
			
		||||
<div class="form-group">
 | 
			
		||||
    <a href="{{ isset($book) ? $book->getUrl() : baseUrl('/books') }}" class="button muted">{{ trans('common.cancel') }}</a>
 | 
			
		||||
    <button type="submit" class="button pos">{{ trans('entities.books_save') }}</button>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@
 | 
			
		|||
    <h3>
 | 
			
		||||
        <a class="text-book entity-list-item-link" href="{{$book->getUrl()}}"><i class="zmdi zmdi-book"></i><span class="entity-list-item-name">{{$book->name}}</span>
 | 
			
		||||
        <br>
 | 
			
		||||
            <img @if($book->image === NULL) src="{{baseUrl('/default.jpg')}}" @else src="{{$book->image}}" @endif alt="{{$book->name}}">
 | 
			
		||||
        <img src="{{$book->getBookCover(192)}}" alt="{{$book->name}}">
 | 
			
		||||
        </a>
 | 
			
		||||
    </h3>
 | 
			
		||||
    @if(isset($book->searchSnippet))
 | 
			
		||||
| 
						 | 
				
			
			@ -11,5 +11,5 @@
 | 
			
		|||
    @else
 | 
			
		||||
        <p class="text-muted">{{ $book->getExcerpt() }}</p>
 | 
			
		||||
    @endif
 | 
			
		||||
    </div>
 | 
			
		||||
</div>
 | 
			
		||||
</div>
 | 
			
		||||
		Loading…
	
		Reference in New Issue