Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions app/Http/Controllers/Api/IdentitasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Requests\UploadImageRequest;
use App\Http\Transformers\IdentitasTransformer;
use App\Models\Identitas;
use App\Services\SecureImageUploadService;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -60,33 +61,40 @@ public function update(IdentitasRequest $request, $id)
public function upload(UploadImageRequest $request, $id)
{
try {
$file = $request->file('file');

// Use secure image upload service
$secureService = new SecureImageUploadService(2048);
$result = $secureService->processSecureUpload($file, 'png', 'img');

// Resize for logo
$path = storage_path('app/public/img');
if (! file_exists($path)) {
mkdir($path, 755, true);
}
$filename = uniqid('img_');
$file = $request->file('file');

Image::make($file->path())->resize(150, 150,
function ($constraint) {

// Resize the processed image
Image::make(storage_path('app/public/' . $result['path']))
->resize(150, 150, function ($constraint) {
$constraint->aspectRatio();
})->save($path.'/'.$filename.'.png'); //create logo
})
->save(storage_path('app/public/img/' . $result['filename']));

Identitas::where('id', $id)->update([
'logo' => $filename.'.png',
'logo' => $result['filename'],
]);

return response()->json([
'success' => true,
'data' => asset('/storage/img/'.$filename.'.png'),
'data' => asset('/storage/img/' . $result['filename']),
], Response::HTTP_OK);
} catch (\Exception $e) {
report($e);

return response()->json([
'success' => false,
'message' => $e->getMessage(),
], Response::HTTP_INTERNAL_SERVER_ERROR);
'message' => 'Upload ditolak: ' . $e->getMessage(),
], Response::HTTP_BAD_REQUEST);
}
}

Expand All @@ -98,8 +106,14 @@ public function uploadFavicon(UploadImageRequest $request, $id)
mkdir($path, 755, true);
}
$file = $request->file('file');

$this->generateFaviconsFromImagePath($file->path(), $path);

// Use secure image upload service first
$secureService = new SecureImageUploadService(2048);
$result = $secureService->processSecureUpload($file, 'png', 'temp');

// Generate favicons from the processed (safe) image
$this->generateFaviconsFromImagePath(storage_path('app/public/' . $result['path']), $path);

Identitas::where('id', $id)->update([
'favicon' => 'favicon-96x96.png',
]);
Expand All @@ -113,8 +127,8 @@ public function uploadFavicon(UploadImageRequest $request, $id)

return response()->json([
'success' => false,
'message' => $e->getMessage(),
], Response::HTTP_INTERNAL_SERVER_ERROR);
'message' => 'Upload ditolak: ' . $e->getMessage(),
], Response::HTTP_BAD_REQUEST);
}
}

Expand Down
57 changes: 42 additions & 15 deletions app/Http/Controllers/CMS/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,27 @@ public function create()
public function store(CreateArticleRequest $request)
{
$input = $request->all();
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
}
$this->articleRepository->create($input);

try {
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
}

$this->articleRepository->create($input);

Session::flash('success', 'Artikel berhasil disimpan.');
Session::flash('success', 'Artikel berhasil disimpan.');

return redirect(route('articles.index'));
return redirect(route('articles.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['foto' => 'Gagal mengunggah gambar: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat menyimpan artikel. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down Expand Up @@ -108,20 +121,34 @@ public function update($id, UpdateArticleRequest $request)

return redirect(route('articles.index'));
}

$input = $request->all();
$removeThumbnail = $request->get('remove_thumbnail');
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
} else {
if ($removeThumbnail) {
$input['thumbnail'] = null;

try {
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
} else {
if ($removeThumbnail) {
$input['thumbnail'] = null;
}
}
}
$article = $this->articleRepository->update($input, $id);
$article = $this->articleRepository->update($input, $id);

Session::flash('success', 'Artikel berhasil diupdate.');
Session::flash('success', 'Artikel berhasil diupdate.');

return redirect(route('articles.index'));
return redirect(route('articles.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['foto' => 'Gagal mengunggah gambar: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat mengupdate artikel. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down
52 changes: 40 additions & 12 deletions app/Http/Controllers/CMS/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,28 @@ public function create()
public function store(CreateDownloadRequest $request)
{
$input = $request->all();
if ($request->file('download_file')) {
$input['url'] = $this->uploadFile($request, 'download_file');
}

try {
if ($request->file('download_file')) {
// Upload as generic file (not image)
$input['url'] = $this->uploadFile($request, 'download_file', null, 5120);
}

$this->downloadRepository->create($input);
$this->downloadRepository->create($input);

Session::flash('success', 'File berhasil disimpan.');
Session::flash('success', 'File berhasil disimpan.');

return redirect(route('downloads.index'));
return redirect(route('downloads.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['download_file' => 'Gagal mengunggah file: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat menyimpan file. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down Expand Up @@ -109,15 +122,30 @@ public function update($id, UpdateDownloadRequest $request)

return redirect(route('downloads.index'));
}

$input = $request->all();
if ($request->file('download_file')) {
$input['url'] = $this->uploadFile($request, 'download_file');
}
$download = $this->downloadRepository->update($input, $id);

try {
if ($request->file('download_file')) {
// Upload as generic file (not image)
$input['url'] = $this->uploadFile($request, 'download_file', null, 5120);
}

$download = $this->downloadRepository->update($input, $id);

Session::flash('success', 'File berhasil diupdate.');
Session::flash('success', 'File berhasil diupdate.');

return redirect(route('downloads.index'));
return redirect(route('downloads.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['download_file' => 'Gagal mengunggah file: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat mengupdate file. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down
59 changes: 43 additions & 16 deletions app/Http/Controllers/CMS/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,28 @@ public function create()
public function store(CreatePageRequest $request)
{
$input = $request->all();
if ($request->file('foto')) {
$this->pathFolder .= '/profile';
$input['thumbnail'] = $this->uploadFile($request, 'foto');
}
$this->pageRepository->create($input);

try {
if ($request->file('foto')) {
$this->pathFolder .= '/profile';
$input['thumbnail'] = $this->uploadFile($request, 'foto');
}

$this->pageRepository->create($input);

Session::flash('success', 'Halaman berhasil disimpan.');
Session::flash('success', 'Halaman berhasil disimpan.');

return redirect(route('pages.index'));
return redirect(route('pages.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['foto' => 'Gagal mengunggah gambar: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat menyimpan halaman. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down Expand Up @@ -110,20 +123,34 @@ public function update($id, UpdatePageRequest $request)

return redirect(route('pages.index'));
}

$input = $request->all();
$removeThumbnail = $request->get('remove_thumbnail');
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
} else {
if ($removeThumbnail) {
$input['thumbnail'] = null;

try {
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
} else {
if ($removeThumbnail) {
$input['thumbnail'] = null;
}
}
}
$page = $this->pageRepository->update($input, $id);
$page = $this->pageRepository->update($input, $id);

Session::flash('success', 'Halaman berhasil diupdate.');
Session::flash('success', 'Halaman berhasil diupdate.');

return redirect(route('pages.index'));
return redirect(route('pages.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['foto' => 'Gagal mengunggah gambar: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat mengupdate halaman. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down
56 changes: 41 additions & 15 deletions app/Http/Controllers/CMS/SlideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,27 @@ public function create()
public function store(CreateSlideRequest $request)
{
$input = $request->all();
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
}

try {
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
}

$this->slideRepository->create($input);
$this->slideRepository->create($input);

Session::flash('success', 'Slide berhasil disimpan.');
Session::flash('success', 'Slide berhasil disimpan.');

return redirect(route('slides.index'));
return redirect(route('slides.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['foto' => 'Gagal mengunggah gambar: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat menyimpan slide. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down Expand Up @@ -109,20 +121,34 @@ public function update($id, UpdateSlideRequest $request)

return redirect(route('slides.index'));
}

$input = $request->all();
$removeThumbnail = $request->get('remove_thumbnail');
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
} else {
if ($removeThumbnail) {
$input['thumbnail'] = null;

try {
if ($request->file('foto')) {
$input['thumbnail'] = $this->uploadFile($request, 'foto');
} else {
if ($removeThumbnail) {
$input['thumbnail'] = null;
}
}
}
$slide = $this->slideRepository->update($input, $id);
$slide = $this->slideRepository->update($input, $id);

Session::flash('success', 'Slide berhasil diupdate.');
Session::flash('success', 'Slide berhasil diupdate.');

return redirect(route('slides.index'));
return redirect(route('slides.index'));
} catch (\RuntimeException $e) {
// Convert to validation error so it appears in $errors
return redirect()->back()
->withInput()
->withErrors(['foto' => 'Gagal mengunggah gambar: ' . $e->getMessage()]);
} catch (\Exception $e) {
Session::flash('error', 'Terjadi kesalahan saat mengupdate slide. Silakan coba lagi.');
report($e);
return redirect()->back()->withInput();
}
}

/**
Expand Down
Loading
Loading