Commit c9fa7794 by Aan Choesni Herlingga

bug fix upload file on post master

parent 9d3db05f
......@@ -8,6 +8,7 @@ use App\Models\Webprofile\Categories;
use App\Repositories\Webprofile\En\PostRepository as EnPostRepository;
use App\Repositories\Webprofile\PostRepository;
use Statickidz\GoogleTranslate;
use Illuminate\Support\Str;
class PostController extends Controller
{
......@@ -67,8 +68,17 @@ class PostController extends Controller
array_key_exists('post_status', $data) ? $data['post_status'] = 1 : $data['post_status'] = 0;
array_key_exists('cover_status', $data) ? $data['cover_status'] = 1 : $data['cover_status'] = 0;
$data['slug'] = str_slug($request->input('title'));
$save = $this->repo->store($request, 'thumbnail', 'off');
$save = $this->repo->store($data);
$tipe = 'thumbnail';
if ($request->hasFile($tipe)) {
$img[$tipe] = $save->id . '.' . $request->file($tipe)->guessClientExtension();
$this->repo->upload($img[$tipe], $request, $tipe);
$this->repo->update($img, $save);
}
if (webprofilesetting()['auto_translate'] == 1) {
// save translate
......@@ -110,12 +120,14 @@ class PostController extends Controller
*/
public function edit($id)
{
$setting = webprofilesetting();
$data = $this->repo->findId($id, ['rEn']);
$categories = Categories::pluck('name', 'id');
$data = [
'data' => $data,
'categories' => $categories,
'setting' => $setting,
];
return view('webprofile.backend.posts.edit', $data)->withTitle(trans('feature.edit_post'));
......@@ -135,9 +147,18 @@ class PostController extends Controller
array_key_exists('post_status', $data) ? $data['post_status'] = 1 : $data['post_status'] = 0;
array_key_exists('cover_status', $data) ? $data['cover_status'] = 1 : $data['cover_status'] = 0;
$data['slug'] = str_slug($request->input('title'));
$post = $this->repo->findId($id, ['rEn']);
$edit = $this->repo->update($request, $post, 'thumbnail', 'off');
$edit = $this->repo->update($data, $post);
$tipe = 'thumbnail';
if ($request->hasFile($tipe)) {
$img[$tipe] = $post->id . '.' . $request->file($tipe)->guessClientExtension();
$this->repo->upload($img[$tipe], $request, $tipe);
$this->repo->update($img, $post);
}
$this->updateEn($dataEn, $post);
......@@ -162,6 +183,7 @@ class PostController extends Controller
{
$data = $this->repo->findId($id, ['rEn']);
$this->repo->destroy($data, 'thumbnail');
$this->repo->deletefile($data, 'thumbnail');
if ($data->rEn) {
$this->repoEn->destroy($data->rEn);
......
......@@ -2,6 +2,8 @@
namespace App\Repositories;
use Storage;
abstract class Repository
{
protected $model;
......@@ -74,18 +76,35 @@ abstract class Repository
return $model->delete();
}
public function upload($name, $request, $tipe, $setting)
public function upload($name, $request, $tipe)
{
$setting = webprofilesetting();
$cover = $request->file($tipe);
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $name, file_get_contents($cover->getRealPath()));
} else {
Storage::disk('local')->put('public/' . $tipe . '/' . $name, file_get_contents($cover->getRealPath()));
}
return $name;
}
/**
* Remove the specified resource from storage.
*
* @param Model $model
* @return \Illuminate\Http\Response
*/
public function deletefile($model, $tipe)
{
if ($request->hasFile($tipe)) {
$cover = $request->file($tipe);
$extension = $cover->guessClientExtension();
$filename = $name . '.' . $extension;
$setting = webprofilesetting();
if ($model->$tipe) {
if ($setting['external_storage'] == 1) {
Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
Storage::disk('storage')->delete($setting['directory'] . '/' . $tipe . '/' . $model->$tipe);
} else {
Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
Storage::disk('local')->delete('public/' . $tipe . '/' . $model->$tipe);
}
return $filename;
}
}
}
......@@ -3,10 +3,10 @@
namespace App\Repositories\Webprofile;
use App\Models\Webprofile\Posts;
use App\Repositories\StorageRepository;
use App\Repositories\Repository;
use DataTables;
class PostRepository extends StorageRepository
class PostRepository extends Repository
{
protected $model;
......
......@@ -135,7 +135,15 @@
<div class="col-md-12">
<center>
<div class="form-group">
<img id="uploadPreview" style="width: 200px; height: 100%;" src="https://statik.unesa.ac.id/profileunesa_konten_statik/images/preview.png"/><br>
@if ($data->thumbnail)
@if ($setting['external_storage'] == 1)
<img id="uploadPreview" style="width: 200px; height: 100%;" src="{{ $setting['url_static'] . '/' . $setting['directory'] . '/thumbnail/' . $data->thumbnail }}"><br>
@else
<img id="uploadPreview" style="width: 200px; height: 100%;" src="{{ url('/storage/thumbnail/' . $data->thumbnail) }}"><br>
@endif
@else
<img id="uploadPreview" style="width: 200px; height: 100%;" src="https://statik.unesa.ac.id/profileunesa_konten_statik/images/preview.png"/><br>
@endif
</div>
<div class="form-group">
{{ Form::file('thumbnail', array('class'=>'fileinput btn-danger', 'id'=>'uploadImage', 'data-filename-placement'=>'inside', 'title'=>app('translator')->getFromJson('label.cover_image'), 'onchange'=>'PreviewImage();')) }}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment