UploadController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class UploadController extends BaseApiController
  5. {
  6. /**
  7. * Upload general file
  8. */
  9. public function uploadFile()
  10. {
  11. $auth = $this->requireAuth();
  12. if ($auth instanceof ResponseInterface) {
  13. return $auth;
  14. }
  15. $file = $this->request->getFile('file');
  16. if (!$file) {
  17. return $this->respondError('파일이 전송되지 않았습니다.');
  18. }
  19. if (!$file->isValid()) {
  20. return $this->respondError('유효하지 않은 파일입니다.');
  21. }
  22. // Generate unique filename
  23. $newName = $file->getRandomName();
  24. $uploadPath = FCPATH . 'uploads/files/';
  25. // Create directory if not exists
  26. if (!is_dir($uploadPath)) {
  27. mkdir($uploadPath, 0755, true);
  28. }
  29. try {
  30. $file->move($uploadPath, $newName);
  31. $fileData = [
  32. 'name' => $file->getClientName(),
  33. 'url' => '/uploads/files/' . $newName,
  34. 'path' => $uploadPath . $newName,
  35. 'size' => $file->getSize(),
  36. 'type' => $file->getClientMimeType()
  37. ];
  38. return $this->respondSuccess($fileData, '파일 업로드 성공');
  39. } catch (\Exception $e) {
  40. return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  41. }
  42. }
  43. /**
  44. * Upload image
  45. */
  46. public function uploadImage()
  47. {
  48. $auth = $this->requireAuth();
  49. if ($auth instanceof ResponseInterface) {
  50. return $auth;
  51. }
  52. $file = $this->request->getFile('file');
  53. if (!$file) {
  54. return $this->respondError('이미지가 전송되지 않았습니다.');
  55. }
  56. if (!$file->isValid()) {
  57. return $this->respondError('유효하지 않은 이미지입니다.');
  58. }
  59. // Validate image type
  60. $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
  61. if (!in_array($file->getClientMimeType(), $allowedTypes)) {
  62. return $this->respondError('허용되지 않는 이미지 형식입니다.');
  63. }
  64. // Generate unique filename
  65. $newName = $file->getRandomName();
  66. $uploadPath = FCPATH . 'uploads/images/';
  67. // Create directory if not exists
  68. if (!is_dir($uploadPath)) {
  69. mkdir($uploadPath, 0755, true);
  70. }
  71. try {
  72. $file->move($uploadPath, $newName);
  73. // Optional: Resize image
  74. // $image = \Config\Services::image()
  75. // ->withFile($uploadPath . $newName)
  76. // ->resize(800, 800, true, 'height')
  77. // ->save($uploadPath . $newName);
  78. $imageData = [
  79. 'name' => $file->getClientName(),
  80. 'url' => '/uploads/images/' . $newName,
  81. 'path' => $uploadPath . $newName,
  82. 'size' => $file->getSize(),
  83. 'type' => $file->getClientMimeType()
  84. ];
  85. return $this->respondSuccess($imageData, '이미지 업로드 성공');
  86. } catch (\Exception $e) {
  87. return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  88. }
  89. }
  90. /**
  91. * Upload staff image (영업사원 이미지)
  92. */
  93. public function uploadStaffImage()
  94. {
  95. $auth = $this->requireAuth();
  96. if ($auth instanceof ResponseInterface) {
  97. return $auth;
  98. }
  99. $file = $this->request->getFile('file');
  100. if (!$file) {
  101. return $this->respondError('이미지가 전송되지 않았습니다.');
  102. }
  103. if (!$file->isValid()) {
  104. return $this->respondError('유효하지 않은 이미지입니다.');
  105. }
  106. // Validate image type
  107. $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
  108. if (!in_array($file->getClientMimeType(), $allowedTypes)) {
  109. return $this->respondError('허용되지 않는 이미지 형식입니다.');
  110. }
  111. // Generate unique filename
  112. $newName = $file->getRandomName();
  113. $uploadPath = FCPATH . 'uploads/staff/';
  114. // Create directory if not exists
  115. if (!is_dir($uploadPath)) {
  116. mkdir($uploadPath, 0755, true);
  117. }
  118. try {
  119. $file->move($uploadPath, $newName);
  120. $imageData = [
  121. 'name' => $file->getClientName(),
  122. 'url' => '/uploads/staff/' . $newName,
  123. 'path' => $uploadPath . $newName,
  124. 'size' => $file->getSize(),
  125. 'type' => $file->getClientMimeType()
  126. ];
  127. return $this->respondSuccess($imageData, '직원 이미지 업로드 성공');
  128. } catch (\Exception $e) {
  129. return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  130. }
  131. }
  132. /**
  133. * Upload advisor image (어드바이저 이미지)
  134. */
  135. public function uploadAdvisorImage()
  136. {
  137. $auth = $this->requireAuth();
  138. if ($auth instanceof ResponseInterface) {
  139. return $auth;
  140. }
  141. $file = $this->request->getFile('file');
  142. if (!$file) {
  143. return $this->respondError('이미지가 전송되지 않았습니다.');
  144. }
  145. if (!$file->isValid()) {
  146. return $this->respondError('유효하지 않은 이미지입니다.');
  147. }
  148. // Validate image type
  149. $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
  150. if (!in_array($file->getClientMimeType(), $allowedTypes)) {
  151. return $this->respondError('허용되지 않는 이미지 형식입니다.');
  152. }
  153. // Generate unique filename
  154. $newName = $file->getRandomName();
  155. $uploadPath = FCPATH . 'uploads/advisor/';
  156. // Create directory if not exists
  157. if (!is_dir($uploadPath)) {
  158. mkdir($uploadPath, 0755, true);
  159. }
  160. try {
  161. $file->move($uploadPath, $newName);
  162. $imageData = [
  163. 'name' => $file->getClientName(),
  164. 'url' => '/uploads/advisor/' . $newName,
  165. 'path' => $uploadPath . $newName,
  166. 'size' => $file->getSize(),
  167. 'type' => $file->getClientMimeType()
  168. ];
  169. return $this->respondSuccess($imageData, '어드바이저 이미지 업로드 성공');
  170. } catch (\Exception $e) {
  171. return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  172. }
  173. }
  174. /**
  175. * Upload board file (게시판 파일)
  176. */
  177. public function uploadBoardFile()
  178. {
  179. $auth = $this->requireAuth();
  180. if ($auth instanceof ResponseInterface) {
  181. return $auth;
  182. }
  183. $file = $this->request->getFile('file');
  184. if (!$file) {
  185. return $this->respondError('파일이 전송되지 않았습니다.');
  186. }
  187. if (!$file->isValid()) {
  188. return $this->respondError('유효하지 않은 파일입니다.');
  189. }
  190. // Generate unique filename
  191. $newName = $file->getRandomName();
  192. $uploadPath = FCPATH . 'uploads/bbs/';
  193. // Create directory if not exists
  194. if (!is_dir($uploadPath)) {
  195. mkdir($uploadPath, 0755, true);
  196. }
  197. try {
  198. $file->move($uploadPath, $newName);
  199. $fileData = [
  200. 'name' => $file->getClientName(),
  201. 'url' => '/uploads/bbs/' . $newName,
  202. 'path' => $uploadPath . $newName,
  203. 'size' => $file->getSize(),
  204. 'type' => $file->getClientMimeType()
  205. ];
  206. return $this->respondSuccess($fileData, '게시판 파일 업로드 성공');
  207. } catch (\Exception $e) {
  208. return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  209. }
  210. }
  211. /**
  212. * Upload event file (이벤트 게시판 파일)
  213. */
  214. public function uploadEventFile()
  215. {
  216. $auth = $this->requireAuth();
  217. if ($auth instanceof ResponseInterface) {
  218. return $auth;
  219. }
  220. $file = $this->request->getFile('file');
  221. if (!$file) {
  222. return $this->respondError('파일이 전송되지 않았습니다.');
  223. }
  224. if (!$file->isValid()) {
  225. return $this->respondError('유효하지 않은 파일입니다.');
  226. }
  227. // Generate unique filename
  228. $newName = $file->getRandomName();
  229. $uploadPath = FCPATH . 'uploads/bbs/event/';
  230. // Create directory if not exists
  231. if (!is_dir($uploadPath)) {
  232. mkdir($uploadPath, 0755, true);
  233. }
  234. try {
  235. $file->move($uploadPath, $newName);
  236. $fileData = [
  237. 'name' => $file->getClientName(),
  238. 'url' => '/uploads/bbs/event/' . $newName,
  239. 'path' => $uploadPath . $newName,
  240. 'size' => $file->getSize(),
  241. 'type' => $file->getClientMimeType()
  242. ];
  243. return $this->respondSuccess($fileData, '이벤트 파일 업로드 성공');
  244. } catch (\Exception $e) {
  245. return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  246. }
  247. }
  248. /**
  249. * Upload news file (뉴스 게시판 파일)
  250. */
  251. public function uploadNewsFile()
  252. {
  253. $auth = $this->requireAuth();
  254. if ($auth instanceof ResponseInterface) {
  255. return $auth;
  256. }
  257. $file = $this->request->getFile('file');
  258. if (!$file) {
  259. return $this->respondError('파일이 전송되지 않았습니다.');
  260. }
  261. if (!$file->isValid()) {
  262. return $this->respondError('유효하지 않은 파일입니다.');
  263. }
  264. // Generate unique filename
  265. $newName = $file->getRandomName();
  266. $uploadPath = FCPATH . 'uploads/bbs/news/';
  267. // Create directory if not exists
  268. if (!is_dir($uploadPath)) {
  269. mkdir($uploadPath, 0755, true);
  270. }
  271. try {
  272. $file->move($uploadPath, $newName);
  273. $fileData = [
  274. 'name' => $file->getClientName(),
  275. 'url' => '/uploads/bbs/news/' . $newName,
  276. 'path' => $uploadPath . $newName,
  277. 'size' => $file->getSize(),
  278. 'type' => $file->getClientMimeType()
  279. ];
  280. return $this->respondSuccess($fileData, '뉴스 파일 업로드 성공');
  281. } catch (\Exception $e) {
  282. return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  283. }
  284. }
  285. /**
  286. * Upload branch manager image (지점장 이미지)
  287. */
  288. public function uploadBManagerImage()
  289. {
  290. $auth = $this->requireAuth();
  291. if ($auth instanceof ResponseInterface) {
  292. return $auth;
  293. }
  294. $file = $this->request->getFile('file');
  295. if (!$file) {
  296. return $this->respondError('이미지가 전송되지 않았습니다.');
  297. }
  298. if (!$file->isValid()) {
  299. return $this->respondError('유효하지 않은 이미지입니다.');
  300. }
  301. // Validate image type
  302. $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
  303. if (!in_array($file->getClientMimeType(), $allowedTypes)) {
  304. return $this->respondError('허용되지 않는 이미지 형식입니다.');
  305. }
  306. // Generate unique filename
  307. $newName = $file->getRandomName();
  308. $uploadPath = FCPATH . 'uploads/bmanager/';
  309. // Create directory if not exists
  310. if (!is_dir($uploadPath)) {
  311. mkdir($uploadPath, 0755, true);
  312. }
  313. try {
  314. $file->move($uploadPath, $newName);
  315. $imageData = [
  316. 'name' => $file->getClientName(),
  317. 'url' => '/uploads/bmanager/' . $newName,
  318. 'path' => $uploadPath . $newName,
  319. 'size' => $file->getSize(),
  320. 'type' => $file->getClientMimeType()
  321. ];
  322. return $this->respondSuccess($imageData, '지점장 이미지 업로드 성공');
  323. } catch (\Exception $e) {
  324. return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
  325. }
  326. }
  327. }