파일업로드 추가
Some checks failed
Close Pull Request / main (pull_request_target) Has been cancelled

This commit is contained in:
yangsh
2026-01-28 16:09:52 +09:00
parent b51f2fddcf
commit d134b27614
5 changed files with 1330 additions and 257 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Controllers\Article;
use App\Controllers\BaseController;
use App\Libraries\Common;
use App\Libraries\MyUpload;
use App\Models\article\ReceiptModel;
use App\Models\common\CodeModel;
@@ -461,4 +462,331 @@ class Receipt extends BaseController
]);
}
}
// 파일업로드
public function uploadFile()
{
$lib = new MyUpload();
$common = new Common();
try {
$rcpt_key = $this->request->getPost('rcpt_key');
$rsrv_sq = $this->request->getPost('rsrv_sq');
$rcpt_sq = $this->request->getPost('rcpt_sq');
$img_type = $this->request->getPost('img_type');
$files = $this->request->getFiles();
$uploadPath = "/upload/result/" . $rsrv_sq . "/";
if (!isset($files['files'])) {
return $this->response->setJSON([
'success' => false,
'msg' => '파일 없음'
]);
}
$uploadFiles = $files['files'];
if ($uploadFiles instanceof \CodeIgniter\HTTP\Files\UploadedFile) {
$uploadFiles = [$uploadFiles];
}
// 매물정보 가져오기
$receipt = $this->model->getDetail($rcpt_key);
// 워터마크 이미지 조회
$watermark_info = [];
if (!empty($receipt) && ($receipt['comp_sq'] ?? '') == '2') {
$watermark_info = $this->model->getWatermarkList($receipt['rcpt_cpid']);
}
$arrUploadfile = [];
foreach ($uploadFiles as $file) {
log_message('info', '[Receipt::uploadFile] upload start name={name} size={size} type={type} error={error}:{errorString} path={path}', [
'name' => $file->getName(),
'size' => $file->getSize(),
'type' => $file->getMimeType(),
'error' => $file->getError(),
'errorString' => $file->getErrorString(),
'path' => $uploadPath,
]);
$uploadData = $lib->do_upload2($file, $uploadPath);
if ($uploadData !== false) {
$arrUploadfile[] = $uploadData;
log_message('info', '[Receipt::uploadFile] upload success name={name} stored={stored}', [
'name' => $file->getName(),
'stored' => $uploadData['file_name'] ?? '(unknown)',
]);
} else {
log_message('error', '[Receipt::uploadFile] do_upload2 failed name={name} error={error}:{errorString}', [
'name' => $file->getName(),
'error' => $file->getError(),
'errorString' => $file->getErrorString(),
]);
}
}
$gps_lat = null;
$gps_lon = null;
$camDate = null;
if (!empty($arrUploadfile)) {
foreach ($arrUploadfile as $key => $uploadFile) {
$object_storage_url = $uploadFile['object_storage_url'];
$imgWidth = '';
$imgHeight = '';
if ($img_type !== 'V1') {
$base = $uploadFile['base_name']; // xxxx
$dir = rtrim(dirname($uploadFile['object_key']), '/'); // upload/result/*
$thumbKey = $dir . '/' . $base . '_thumb.jpg';
$imageDataBlob = file_get_contents($object_storage_url);
$im = new \Imagick();
$im->readImageBlob($imageDataBlob);
$imgWidth = $im->getImageWidth();
$imgHeight = $im->getImageHeight();
$im->thumbnailImage(105, 80, false);
$thumb_im = $im->getImageBlob();
// 썸네일 s3 전송
$lib->upload_object_storage_imagick2($thumbKey, $thumb_im);
$im->destroy();
}
// 워터마크 삽입
$watermark_imgs = ['I3', 'I4'];
if (in_array($img_type, $watermark_imgs)) {
if (!empty($watermark_info)) {
$common->watermarking($object_storage_url, $watermark_info, '', $receipt['rcpt_cpid'], $uploadFile['object_key']);
}
}
// 촬영정보 가져오기
$arrExifData = @exif_read_data($object_storage_url);
if (!is_array($arrExifData)) {
$arrExifData = [];
}
if (!isset($arrExifData['COMPUTED']) || !is_array($arrExifData['COMPUTED'])) {
$arrExifData['COMPUTED'] = [];
}
$notFound = "Unavailable";
// Make - 카메라 제조사
if (@array_key_exists('Make', $arrExifData)) {
$camMake = $arrExifData['Make'];
} else {
$camMake = $notFound;
}
// Model - 카메라 모델
if (@array_key_exists('Model', $arrExifData)) {
$camModel = $arrExifData['Model'];
} else {
$camModel = $notFound;
}
// Date - 촬영일시
if (@array_key_exists('DateTime', $arrExifData)) {
$camDate = $arrExifData['DateTime'];
} else {
$camDate = $notFound;
}
// COMPUTED.Height - 세로크기
if (@array_key_exists('Height', $arrExifData['COMPUTED'])) {
$camHeight = $arrExifData['COMPUTED']['Height'];
} else {
$camHeight = $notFound;
}
// COMPUTED.Width - 가로크기
if (@array_key_exists('Width', $arrExifData['COMPUTED'])) {
$camWidth = $arrExifData['COMPUTED']['Width'];
} else {
$camWidth = $notFound;
}
$imageMetaData = "카메라 제조사: " . $camMake;
$imageMetaData .= "\n카메라 모델: " . $camModel;
$imageMetaData .= "\n촬영일시: " . $camDate;
$imageMetaData .= "\n가로: " . $camWidth;
$imageMetaData .= "\n세로: " . $camHeight;
$metaData = $imageMetaData;
/**
* 파일업로드 내용 저장
*/
$uploadParam = [
'rcpt_key' => $rcpt_key, // 접수번호
'rsrv_sq' => $rsrv_sq, // 예약일련번호
'rcpt_sq' => $rcpt_sq, // 접수일련번호
// 'gps_lat' => $gps_lat, // latitude
// 'gps_lon' => $gps_lon, // longitude
'origin_name' => $uploadFile['origin_name'], // 원본파일명
'file_name' => $uploadFile['file_name'], // 저장파일명
'upload_path' => $uploadPath, // 저장경로
'size' => $file->getSize(),
'width' => $imgWidth,
'height' => $imgHeight,
// 'thumb_name' => $base . '_thumb.jpg',
'cam_date' => $camDate, // 촬영일
'meta_data' => $metaData, // 이미지메타데이터
'receipt' => $receipt,
'img_type' => $img_type,
];
$this->model->saveImg($uploadParam);
}
}
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
} catch (\Exception $e) {
return $this->response->setJSON([
'code' => '9',
'msg' => $e->getMessage(),
]);
}
}
// 매물사진 일괄 다운로드 (zip)
public function downloadAllImages()
{
$rsrv_sq = $this->request->getGet('rsrv_sq');
$img_type = $this->request->getGet('img_type') ?? 'I4';
if (empty($rsrv_sq)) {
return $this->response->setStatusCode(400)->setBody('rsrv_sq required');
}
$images = $this->model->getImageListByType($rsrv_sq, $img_type);
if (empty($images)) {
return $this->response->setStatusCode(404)->setBody('No files');
}
$zip = new \ZipArchive();
$zipName = 'images_' . $rsrv_sq . '_' . $img_type . '_' . date('Ymd_His') . '.zip';
$zipPath = WRITEPATH . 'cache/' . $zipName;
if ($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
return $this->response->setStatusCode(500)->setBody('Failed to create zip');
}
foreach ($images as $index => $img) {
$filePath = $img['img_path'] . $img['img_filenm'];
$zipFileName = sprintf('%02d_%s', $index + 1, basename($img['img_filenm']));
if (($img['cloud_upload_yn'] ?? 'N') === 'Y') {
$sourceUrl = NCLOUD_OBJECT_STORAGE_URL . $filePath;
$content = @file_get_contents($sourceUrl);
if ($content !== false) {
$zip->addFromString($zipFileName, $content);
}
continue;
}
$localPath = FCPATH . ltrim($filePath, '/');
if (is_file($localPath)) {
$zip->addFile($localPath, $zipFileName);
continue;
}
$content = @file_get_contents($filePath);
if ($content !== false) {
$zip->addFromString($zipFileName, $content);
}
}
$zip->close();
if (!is_file($zipPath)) {
return $this->response->setStatusCode(500)->setBody('Zip not found');
}
register_shutdown_function(static function () use ($zipPath) {
if (is_file($zipPath)) {
@unlink($zipPath);
}
});
return $this->response->download($zipPath, null)->setFileName($zipName);
}
// 촬영위치 저장
public function saveImgLocation()
{
try {
$img_sq = $this->request->getPost('img_sq');
$rsrv_sq = $this->request->getPost('rsrv_sq');
$location = $this->request->getPost('location');
$this->model->saveImgLocation($img_sq, $rsrv_sq, $location);
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
} catch (\Exception $e) {
return $this->response->setJSON([
'code' => '9',
'msg' => $e->getMessage(),
]);
}
}
// 업로드파일 삭제
public function removeUploadFile()
{
try {
$rcpt_sq = $this->request->getPost('rcpt_sq');
$img_sq = $this->request->getPost('img_sq');
// 파일정보 조회
$file = $this->model->getUploadFileInfo($img_sq);
if (!empty($file)) {
$lib = new MyUpload();
$path = $file['img_path'] . "" . $file['img_filenm'];
$thumb = explode(".", $file['img_filenm']);
$thumbPath = $file['img_path'] . "" . $thumb[0] . "_thumb." . $thumb[1];
// if ($file['cloud_upload_yn'] == "Y") {
// $path = NCLOUD_OBJECT_STORAGE_URL . $path;
// }
$lib->deleteFile($path);
$lib->deleteFile($thumbPath);
}
$this->model->removeUploadFile($rcpt_sq, $img_sq);
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
} catch (\Exception $e) {
return $this->response->setJSON([
'code' => '0',
'msg' => 'success'
]);
}
}
}