| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class UploadController extends BaseApiController
- {
- /**
- * Upload general file
- */
- public function uploadFile()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('파일이 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 파일입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/files/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $fileData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/files/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($fileData, '파일 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload image
- */
- public function uploadImage()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('이미지가 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 이미지입니다.');
- }
- // Validate image type
- $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
- if (!in_array($file->getClientMimeType(), $allowedTypes)) {
- return $this->respondError('허용되지 않는 이미지 형식입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/images/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- // Optional: Resize image
- // $image = \Config\Services::image()
- // ->withFile($uploadPath . $newName)
- // ->resize(800, 800, true, 'height')
- // ->save($uploadPath . $newName);
- $imageData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/images/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($imageData, '이미지 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload staff image (영업사원 이미지)
- */
- public function uploadStaffImage()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('이미지가 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 이미지입니다.');
- }
- // Validate image type
- $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
- if (!in_array($file->getClientMimeType(), $allowedTypes)) {
- return $this->respondError('허용되지 않는 이미지 형식입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/staff/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $imageData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/staff/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($imageData, '직원 이미지 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload advisor image (어드바이저 이미지)
- */
- public function uploadAdvisorImage()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('이미지가 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 이미지입니다.');
- }
- // Validate image type
- $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
- if (!in_array($file->getClientMimeType(), $allowedTypes)) {
- return $this->respondError('허용되지 않는 이미지 형식입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/advisor/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $imageData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/advisor/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($imageData, '어드바이저 이미지 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload board file (게시판 파일)
- */
- public function uploadBoardFile()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('파일이 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 파일입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/bbs/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $fileData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/bbs/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($fileData, '게시판 파일 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload event file (이벤트 게시판 파일)
- */
- public function uploadEventFile()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('파일이 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 파일입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/bbs/event/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $fileData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/bbs/event/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($fileData, '이벤트 파일 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload news file (뉴스 게시판 파일)
- */
- public function uploadNewsFile()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('파일이 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 파일입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/bbs/news/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $fileData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/bbs/news/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($fileData, '뉴스 파일 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('파일 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- /**
- * Upload branch manager image (지점장 이미지)
- */
- public function uploadBManagerImage()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $file = $this->request->getFile('file');
- if (!$file) {
- return $this->respondError('이미지가 전송되지 않았습니다.');
- }
- if (!$file->isValid()) {
- return $this->respondError('유효하지 않은 이미지입니다.');
- }
- // Validate image type
- $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
- if (!in_array($file->getClientMimeType(), $allowedTypes)) {
- return $this->respondError('허용되지 않는 이미지 형식입니다.');
- }
- // Generate unique filename
- $newName = $file->getRandomName();
- $uploadPath = FCPATH . 'uploads/bmanager/';
- // Create directory if not exists
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- try {
- $file->move($uploadPath, $newName);
- $imageData = [
- 'name' => $file->getClientName(),
- 'url' => '/uploads/bmanager/' . $newName,
- 'path' => $uploadPath . $newName,
- 'size' => $file->getSize(),
- 'type' => $file->getClientMimeType()
- ];
- return $this->respondSuccess($imageData, '지점장 이미지 업로드 성공');
- } catch (\Exception $e) {
- return $this->respondError('이미지 업로드 중 오류가 발생했습니다: ' . $e->getMessage());
- }
- }
- }
|