ImageController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Controllers\Admin\Board;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Traits\AgentTrait;
  6. use App\Models\Board;
  7. use App\Models\EditorImage;
  8. use App\Models\DTO\SearchData;
  9. use Exception;
  10. class ImageController extends Controller
  11. {
  12. use AgentTrait;
  13. private Board $boardModel;
  14. private EditorImage $editorImageModel;
  15. public function __construct(Board $board, EditorImage $editorImage)
  16. {
  17. $this->boardModel = $board;
  18. $this->editorImageModel = $editorImage;
  19. }
  20. /**
  21. * 게시판 > 이미지 관리
  22. * @method GET
  23. * @see /admin/board/image
  24. */
  25. public function index(Request $request)
  26. {
  27. $params = SearchData::fromRequest($request);
  28. $editorImageData = $this->editorImageModel->data($params);
  29. if ($editorImageData->rows > 0) {
  30. $num = listNum($editorImageData->total, $params->page, $params->perPage);
  31. foreach ($editorImageData->list as $i => $row) {
  32. $row->num = $num--;
  33. $row->fileExists = file_exists($row->file_path);
  34. $row->targetStr = MAP_EDITOR_IMG_TYPE[$row->target_type];
  35. $row->editURL = route('admin.board.image.edit', $row->id);
  36. $row->fileLink = route('admin.board.image.show', $row->id);
  37. $editorImageData->list[$i] = $row;
  38. }
  39. }
  40. return view('admin.board.image.index', [
  41. 'editorImageData' => $editorImageData,
  42. 'params' => $params
  43. ]);
  44. }
  45. /**
  46. * 게시판 > 이미지 관리 삭제
  47. * @method DELETE
  48. * @see /admin/board/image/destroy
  49. */
  50. public function destroy(Request $request)
  51. {
  52. try {
  53. $chk = $request->post('chk');
  54. $fileName = $request->post('file_name');
  55. if ($chk) {
  56. foreach ($chk as $editorImageID) {
  57. $editorImage = $this->editorImageModel->findOrNew($editorImageID);
  58. if(!$editorImage->exists) {
  59. throw new Exception($editorImageID . '번 이미지는 존재하지 않습니다.');
  60. }
  61. if(!$this->editorImageModel->remove($editorImageID)) {
  62. throw new Exception($editorImageID . '번 ' . $fileName[$editorImageID] . " 파일을 삭제할 수 없습니다.");
  63. }
  64. }
  65. }
  66. $message = '에디터 이미지가 삭제되었습니다.';
  67. return redirect()->route('admin.board.image.index')->with('message', $message);
  68. } catch (Exception $e) {
  69. return back()->withErrors($e->getMessage())->withInput();
  70. }
  71. }
  72. /**
  73. * 게시판 > 이미지 관리 상세보기
  74. * @method GET
  75. * @see /admin/board/image/show/{pk}
  76. */
  77. public function show(int $editorImageID)
  78. {
  79. $imageModel = $this->editorImageModel->findOrFail($editorImageID);
  80. if(!file_exists($imageModel->file_path)) {
  81. return back()->with('message', '해당 파일이 존재하지 않습니다.');
  82. }
  83. return response()->download($imageModel->file_path, $imageModel->origin_name);
  84. }
  85. /**
  86. * 게시판 > 이미지 정보 상세 보기
  87. * @method GET
  88. * @see /admin/board/image/{pk}/edit
  89. */
  90. public function edit(int $editorImageID)
  91. {
  92. $image = $this->editorImageModel->findOrFail($editorImageID);
  93. $image->device = $this->device($image->user_agent);
  94. $image->platform = $this->platform($image->user_agent);
  95. $image->browser = $this->browser($image->user_agent);
  96. $image->fileLink = route('admin.board.image.show', $image->id);
  97. $image->fileSize = byteFormat($image->file_size, 2);
  98. $image->download = number_format($image->download);
  99. return view('admin.board.image.edit', [
  100. 'image' => $image
  101. ]);
  102. }
  103. }