PostFile.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Http\UploadedFile;
  7. use App\Models\DTO\SearchData;
  8. class PostFile extends Model
  9. {
  10. protected $table = 'tb_post_file';
  11. protected $primaryKey = 'id';
  12. public $keyType = 'int';
  13. public $incrementing = true;
  14. public $timestamps = true;
  15. const CREATED_AT = 'created_at';
  16. const UPDATED_AT = null;
  17. protected $guarded = [];
  18. public function board()
  19. {
  20. return $this->belongsTo(Board::class)->withDefault();
  21. }
  22. public function post()
  23. {
  24. return $this->belongsTo(Post::class)->withDefault();
  25. }
  26. public function postFileDownLog()
  27. {
  28. return $this->hasMany(PostFileDownLog::class);
  29. }
  30. public function user()
  31. {
  32. return $this->belongsTo(User::class)->withDefault();
  33. }
  34. /**
  35. * 게시글 첨부 파일 조회
  36. */
  37. public function data(SearchData $params): object
  38. {
  39. $query = $this->query();
  40. $query->select(
  41. 'tb_board.name AS boardName',
  42. 'tb_board.code',
  43. 'tb_post_file.*',
  44. 'tb_post.subject',
  45. 'users.sid',
  46. 'users.name'
  47. );
  48. if($params->keyword) {
  49. switch ($params->field) {
  50. case 'tb_post.id' :
  51. case 'tb_post_file.id' :
  52. case 'users.id' :
  53. case 'users.sid' :
  54. $query->where($params->field, '=', $params->keyword);
  55. break;
  56. case 'tb_post.subject' :
  57. case 'users.name' :
  58. case 'users.email' :
  59. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  60. break;
  61. }
  62. }
  63. if($params->startDate || $params->endDate) {
  64. if($params->startDate) {
  65. $query->where('tb_post_file.created_at', '>=', $params->startDate . ' 00:00:00');
  66. }
  67. if($params->endDate) {
  68. $query->where('tb_post_file.created_at', '<=', $params->endDate . ' 23:59:59');
  69. }
  70. }
  71. if($params->boardID) {
  72. $query->where('tb_post_file.board_id', '=', $params->boardID);
  73. }
  74. $query->join('tb_board', 'tb_board.id', '=', 'tb_post_file.board_id');
  75. $query->join('tb_post', 'tb_post.id', '=', 'tb_post_file.post_id');
  76. $query->leftJoin('users', 'users.id', '=', 'tb_post_file.user_id');
  77. $query->orderByDesc('tb_post_file.id');
  78. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  79. $total = $this->count();
  80. $rows = $list->count();
  81. return (object)[
  82. 'total' => $total,
  83. 'rows' => $rows,
  84. 'list' => $list
  85. ];
  86. }
  87. /**
  88. * 첨부파일 정보 조회
  89. */
  90. public function info(int $fileID): PostFile
  91. {
  92. return $this->findOrNew($fileID);
  93. }
  94. /**
  95. * 게시글 파일 삭제
  96. */
  97. public function remove(int $postFileID): mixed
  98. {
  99. return DB::transaction(function() use($postFileID) {
  100. $postFile = $this->info($postFileID);
  101. // 파일 존재 확인
  102. if(!$postFile->exists) {
  103. return false;
  104. }
  105. // 첨부파일 다운로드 기록 삭제
  106. $postFile->postFileDownLog->each(function($fileLog) {
  107. $fileLog->delete();
  108. });
  109. // 게시글 첨부파일 수 갱신
  110. $postFile->post->updateFileRows($postFile->post_id);
  111. // 파일 삭제
  112. Storage::delete($postFile->file_path);
  113. if(file_exists($postFile->file_path)) {
  114. unlink($postFile->file_path);
  115. }
  116. // 첨부파일 정보 삭제
  117. $postFile->delete();
  118. return true;
  119. });
  120. }
  121. /**
  122. * 게시글 파일 전부 삭제
  123. */
  124. public function truncate(int $postID): void
  125. {
  126. if($this->where('post_id', $postID)->delete()) {
  127. (new FileLib)->removePath(UPLOAD_PATH_POST, $postID);
  128. }
  129. }
  130. /**
  131. * 게시글 파일 첨부 등록
  132. */
  133. public function register(Post $post, UploadedFile|array|null $files): void
  134. {
  135. if ($files) {
  136. foreach ($files as $file) {
  137. $this->_upload($post, $file);
  138. }
  139. $post->updateFileRows($post->id);
  140. $post->updateImageRows($post->id);
  141. }
  142. }
  143. /**
  144. * 게시글 파일 첨부 수정, 삭제
  145. */
  146. public function updater(Post $post, array $filesIsDelete, UploadedFile|array|null $files)
  147. {
  148. // 삭제 요청이 있을 경우 삭제
  149. if($filesIsDelete) {
  150. foreach ($filesIsDelete as $fileID => $checked) {
  151. $this->remove($fileID);
  152. }
  153. }
  154. if($files) {
  155. foreach ($files as $fileID => $file)
  156. {
  157. $postFile = $this->where([
  158. ['id', $fileID],
  159. ['board_id', $post->board_id],
  160. ['post_id', $post->id]
  161. ])->firstOrNew();
  162. // 파일이 이미 있으면 기존 정보 삭제
  163. if($postFile->exists) {
  164. $this->remove($postFile);
  165. }
  166. $this->_upload($post->board_id, $post->id, $file);
  167. }
  168. }
  169. $post->updateFileRows($post->id);
  170. $post->updateImageRows($post->id);
  171. }
  172. /**
  173. * 파일 업로드 처리
  174. */
  175. private function _upload(Post $post, UploadedFile $file): bool
  176. {
  177. $fileInfo = (new FileLib)->upload($file, UPLOAD_PATH_POST, $post->id);
  178. return $this->insert([
  179. 'board_id' => $post->board_id,
  180. 'post_id' => $post->id,
  181. 'user_id' => UID,
  182. 'origin_name' => $fileInfo->originName,
  183. 'file_name' => $fileInfo->name,
  184. 'file_path' => $fileInfo->path,
  185. 'file_url' => $fileInfo->url,
  186. 'file_size' => $fileInfo->size,
  187. 'file_ext' => $fileInfo->extension,
  188. 'img_width' => $fileInfo->imageWidth,
  189. 'img_height' => $fileInfo->imageHeight,
  190. 'is_image' => $fileInfo->isImage,
  191. 'download' => 0,
  192. 'ip_address' => IP_ADDRESS,
  193. 'user_agent' => USER_AGENT,
  194. 'created_at' => now()
  195. ]);
  196. }
  197. /**
  198. * 게시글 다운로드 횟수 증가
  199. */
  200. public function increaseDownload(int $fileID): int
  201. {
  202. return $this->where('id', $fileID)->increment('download');
  203. }
  204. }