아파트단지 상세 파일업로드 추가
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
namespace App\Libraries;
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
use Aws\Exception\AwsException;
|
||||
use Aws\Credentials\CredentialProvider;
|
||||
use Aws\Credentials\Credentials;
|
||||
use CodeIgniter\HTTP\Files\UploadedFile;
|
||||
|
||||
class MyUpload
|
||||
{
|
||||
@@ -49,12 +53,76 @@ class MyUpload
|
||||
return $this;
|
||||
}
|
||||
|
||||
// CI3 스타일: do_upload()
|
||||
/**
|
||||
* 파일 업로드 요청
|
||||
* 추가일 2025.12.24
|
||||
* 작성자 - yangsh
|
||||
*/
|
||||
public function do_upload2(UploadedFile $file, $filePath = null): array|false
|
||||
{
|
||||
if (!$file->isValid()) {
|
||||
$this->set_error('upload_invalid_file');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 업로드 전인데 hasMoved()가 true면 비정상 상태
|
||||
if ($file->hasMoved()) {
|
||||
$this->set_error('upload_file_already_moved');
|
||||
return false;
|
||||
}
|
||||
|
||||
$newName = $file->getRandomName();
|
||||
|
||||
// ✅ PHP 임시 업로드 파일 경로 (writable로 move() 필요 없음)
|
||||
$tmpFile = $file->getTempName();
|
||||
if (!is_file($tmpFile)) {
|
||||
$this->set_error('upload_temp_file_missing');
|
||||
log_message('error', 'do_upload2 temp file missing: ' . $tmpFile);
|
||||
return false;
|
||||
}
|
||||
|
||||
// ✅ 클라우드에 올라갈 "Key"를 직접 만든다 (로컬 경로 절대 넣지 말기)
|
||||
// 예시: upload/tmp/랜덤파일명 또는 upload/apt_file/{rcpt_no}/...
|
||||
$objectKey = $filePath . $newName;
|
||||
|
||||
$up = $this->upload_object_storage($objectKey, $tmpFile, 'file');
|
||||
if ($up === false) {
|
||||
$this->set_error('upload_destination_cloud_error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// (선택) tmp 파일 삭제
|
||||
@unlink($tmpFile);
|
||||
|
||||
$this->s3_data = [
|
||||
'object_key' => $objectKey,
|
||||
'object_storage_url' => $up['object_storage_url'] ?? null,
|
||||
'origin_name' => $file->getClientName(),
|
||||
'file_name' => basename($objectKey), // xxxx.jpg
|
||||
'base_name' => pathinfo($objectKey, PATHINFO_FILENAME), // xxxx
|
||||
'ext' => pathinfo($objectKey, PATHINFO_EXTENSION), // jpg
|
||||
];
|
||||
|
||||
|
||||
log_message('debug', 's3_data=' . json_encode($this->s3_data ?? null, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
|
||||
|
||||
return $this->s3_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* S3(NCLOUD) 파일 업로드
|
||||
* 추가일 2025.12.24
|
||||
* 작성자 - yangsh
|
||||
*/
|
||||
public function do_upload(string $field = 'userfile'): bool
|
||||
{
|
||||
|
||||
$request = service('request');
|
||||
$file = $request->getFile($field);
|
||||
|
||||
var_dump($file);
|
||||
|
||||
if (!$file) {
|
||||
$this->set_error('upload_no_file_selected');
|
||||
return false;
|
||||
@@ -222,10 +290,13 @@ class MyUpload
|
||||
$this->errors[] = $msg;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// === 너의 기존 S3 메서드들 (CI4용으로 client 생성만 보강) ===
|
||||
|
||||
public function upload_object_storage(string $key, string $temp_file, string $type = 'file'): bool
|
||||
/**
|
||||
* S3(NCLOUD) 파일 업로드
|
||||
* 수정일 2025.12.24
|
||||
* 작성자 - yangsh
|
||||
*/
|
||||
public function upload_object_storage(string $key, string $temp_file, string $type = 'file'): array|false
|
||||
{
|
||||
// CI3 코드의 경로 치환 로직 유지 (FCPATH는 CI4에도 존재)
|
||||
$object_storage_upload_path = str_replace(FCPATH, '/', $key);
|
||||
@@ -236,27 +307,77 @@ class MyUpload
|
||||
$s3Client = $this->makeS3Client();
|
||||
|
||||
try {
|
||||
$body = file_get_contents($temp_file);
|
||||
// $body = file_get_contents($temp_file);
|
||||
|
||||
// $response = $s3Client->putObject([
|
||||
// 'Bucket' => NCLOUD_S3_BUCKET,
|
||||
// 'Key' => ltrim($object_storage_upload_path, '/'),
|
||||
// 'Body' => $body,
|
||||
// 'ACL' => 'public-read',
|
||||
// ]);
|
||||
|
||||
$response = $s3Client->putObject([
|
||||
'Bucket' => NCLOUD_S3_BUCKET,
|
||||
'Key' => ltrim($object_storage_upload_path, '/'),
|
||||
'Body' => $body,
|
||||
'SourceFile' => $temp_file, // ✅ 동영상도 OK
|
||||
'ACL' => 'public-read',
|
||||
|
||||
// (선택) 타입별 ContentType 지정 (브라우저 재생/다운로드에 중요)
|
||||
// 'ContentType' => $this->guessMime($temp_file, $type),
|
||||
]);
|
||||
|
||||
$this->s3_data = [
|
||||
'object_storage_upload_path' => $object_storage_upload_path,
|
||||
// $this->s3_data = [
|
||||
// 'object_storage_upload_path' => $object_storage_upload_path,
|
||||
// 'object_storage_url' => $response['ObjectURL'] ?? null,
|
||||
// ];
|
||||
|
||||
return [
|
||||
'object_storage_url' => $response['ObjectURL'] ?? null,
|
||||
];
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// 운영에서는 echo 지양. 로그로 남기는 걸 추천
|
||||
// log_message('error', $e->getMessage());
|
||||
log_message('error', '[S3 UPLOAD FAIL] ' . $e->getMessage());
|
||||
log_message('error', '[S3 UPLOAD TRACE] ' . $e->getTraceAsString());
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* S3(NCLOUD) 파일 업로드 - 썸네일
|
||||
* 추가일 2025.12.24
|
||||
* 작성자 - yangsh
|
||||
*/
|
||||
public function upload_object_storage_imagick2($key, $blobData): bool
|
||||
{
|
||||
$object_storage_upload_path = str_replace(FCPATH, '/', $key);
|
||||
$object_storage_upload_path = str_replace('/image/confirms_upload/', '/upload/', $object_storage_upload_path);
|
||||
$object_storage_upload_path = str_replace('//', '/', $object_storage_upload_path);
|
||||
$object_storage_upload_path = str_replace('/home/www/upload/confirms_upload/', '/upload/', $object_storage_upload_path);
|
||||
|
||||
$s3Client = $this->makeS3Client();
|
||||
|
||||
try {
|
||||
|
||||
$response = $s3Client->putObject([
|
||||
'Bucket' => NCLOUD_S3_BUCKET,
|
||||
'Key' => ltrim($object_storage_upload_path, '/'),
|
||||
'Body' => $blobData,
|
||||
'ACL' => 'public-read',
|
||||
]);
|
||||
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// 운영에서는 echo 지양. 로그로 남기는 걸 추천
|
||||
log_message('error', '[S3 UPLOAD FAIL] ' . $e->getMessage());
|
||||
log_message('error', '[S3 UPLOAD TRACE] ' . $e->getTraceAsString());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public function upload_object_storage_imagick(string $key, $blobData): bool
|
||||
@@ -306,28 +427,18 @@ class MyUpload
|
||||
|
||||
protected function makeS3Client(): S3Client
|
||||
{
|
||||
// AWS SDK v2: S3Client::factory / v3+: new S3Client
|
||||
if (method_exists(S3Client::class, 'factory')) {
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
return S3Client::factory([
|
||||
'key' => NCLOUD_S3_KEY,
|
||||
'secret' => NCLOUD_S3_SECRET,
|
||||
'endpoint' => NCLOUD_S3_ENDPOINT,
|
||||
'debug' => true,
|
||||
'ssl.certificate_authority' => false,
|
||||
]);
|
||||
}
|
||||
$region = defined('NCLOUD_S3_REGION') ? NCLOUD_S3_REGION : 'ap-northeast-2';
|
||||
|
||||
// ✅ credentials를 provider로 강제하면 provider-chain(IMDS) 안 탐
|
||||
$creds = new Credentials(NCLOUD_S3_KEY, NCLOUD_S3_SECRET);
|
||||
$provider = CredentialProvider::fromCredentials($creds);
|
||||
|
||||
return new S3Client([
|
||||
'endpoint' => NCLOUD_S3_ENDPOINT,
|
||||
'credentials' => [
|
||||
'key' => NCLOUD_S3_KEY,
|
||||
'secret' => NCLOUD_S3_SECRET,
|
||||
],
|
||||
'region' => defined('NCLOUD_S3_REGION') ? NCLOUD_S3_REGION : 'kr-standard',
|
||||
'version' => 'latest',
|
||||
// (보안주의) 필요하면 verify 설정을 추가
|
||||
// 'http' => ['verify' => false],
|
||||
'region' => $region,
|
||||
'endpoint' => NCLOUD_S3_ENDPOINT,
|
||||
'credentials' => $provider,
|
||||
'use_path_style_endpoint' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user