diff --git a/.env.example b/.env.example index de90e952..4ad4d45b 100644 --- a/.env.example +++ b/.env.example @@ -81,3 +81,8 @@ FCM_LOG_CHANNEL=stack BAROBILL_CERT_KEY= BAROBILL_CORP_NUM= BAROBILL_TEST_MODE=true + +# Google Cloud Storage (음성 녹음 백업) +GCS_BUCKET_NAME= +GCS_SERVICE_ACCOUNT_PATH=/var/www/sales/apikey/google_service_account.json +GCS_USE_DB_CONFIG=true diff --git a/app/Services/GoogleCloudStorageService.php b/app/Services/GoogleCloudStorageService.php index 2b79e8cf..c00e30b2 100644 --- a/app/Services/GoogleCloudStorageService.php +++ b/app/Services/GoogleCloudStorageService.php @@ -8,13 +8,18 @@ /** * Google Cloud Storage 업로드 서비스 * - * DB 설정(ai_configs 테이블)을 우선 사용하고, 없으면 레거시 파일 설정을 사용합니다. + * 우선순위: + * 1. DB 설정 (ui에서 오버라이드) + * 2. 환경변수 (.env) + * 3. 레거시 파일 (/sales/apikey/) + * * JWT 인증 방식 사용. */ class GoogleCloudStorageService { private ?string $bucketName = null; private ?array $serviceAccount = null; + private string $configSource = 'none'; public function __construct() { @@ -26,30 +31,49 @@ public function __construct() * * 우선순위: * 1. DB 설정 (ai_configs 테이블의 활성화된 gcs provider) - * 2. 레거시 파일 설정 (/sales/apikey/) + * 2. 환경변수 (.env의 GCS_BUCKET_NAME, GCS_SERVICE_ACCOUNT_PATH) + * 3. 레거시 파일 설정 (/sales/apikey/) */ private function loadConfig(): void { - // 1. DB 설정 확인 - $dbConfig = AiConfig::getActiveGcs(); + // 1. DB 설정 확인 (GCS_USE_DB_CONFIG=true일 때만) + if (config('gcs.use_db_config', true)) { + $dbConfig = AiConfig::getActiveGcs(); - if ($dbConfig) { - $this->bucketName = $dbConfig->getBucketName(); + if ($dbConfig) { + $this->bucketName = $dbConfig->getBucketName(); - // 서비스 계정: JSON 직접 입력 또는 파일 경로 - if ($dbConfig->getServiceAccountJson()) { - $this->serviceAccount = $dbConfig->getServiceAccountJson(); - } elseif ($dbConfig->getServiceAccountPath() && file_exists($dbConfig->getServiceAccountPath())) { - $this->serviceAccount = json_decode(file_get_contents($dbConfig->getServiceAccountPath()), true); + // 서비스 계정: JSON 직접 입력 또는 파일 경로 + if ($dbConfig->getServiceAccountJson()) { + $this->serviceAccount = $dbConfig->getServiceAccountJson(); + } elseif ($dbConfig->getServiceAccountPath() && file_exists($dbConfig->getServiceAccountPath())) { + $this->serviceAccount = json_decode(file_get_contents($dbConfig->getServiceAccountPath()), true); + } + + if ($this->bucketName && $this->serviceAccount) { + $this->configSource = 'db'; + Log::debug('GCS 설정 로드: DB (활성화된 설정: ' . $dbConfig->name . ')'); + return; + } } + } + + // 2. 환경변수 (.env) 설정 + $envBucket = config('gcs.bucket_name'); + $envServiceAccountPath = config('gcs.service_account_path'); + + if ($envBucket && $envServiceAccountPath && file_exists($envServiceAccountPath)) { + $this->bucketName = $envBucket; + $this->serviceAccount = json_decode(file_get_contents($envServiceAccountPath), true); if ($this->serviceAccount) { - Log::debug('GCS 설정 로드: DB (활성화된 설정: ' . $dbConfig->name . ')'); + $this->configSource = 'env'; + Log::debug('GCS 설정 로드: 환경변수 (.env)'); return; } } - // 2. 레거시 파일 설정 (fallback) + // 3. 레거시 파일 설정 (fallback) $gcsConfigPath = base_path('../sales/apikey/gcs_config.txt'); if (file_exists($gcsConfigPath)) { $config = parse_ini_file($gcsConfigPath); @@ -62,10 +86,19 @@ private function loadConfig(): void } if ($this->bucketName && $this->serviceAccount) { + $this->configSource = 'legacy'; Log::debug('GCS 설정 로드: 레거시 파일'); } } + /** + * 현재 설정 소스 반환 + */ + public function getConfigSource(): string + { + return $this->configSource; + } + /** * GCS가 사용 가능한지 확인 */ diff --git a/config/gcs.php b/config/gcs.php new file mode 100644 index 00000000..44078e43 --- /dev/null +++ b/config/gcs.php @@ -0,0 +1,21 @@ + env('GCS_BUCKET_NAME'), + + // 서비스 계정 파일 경로 + 'service_account_path' => env('GCS_SERVICE_ACCOUNT_PATH', '/var/www/sales/apikey/google_service_account.json'), + + // DB 설정 사용 여부 (false면 .env만 사용) + 'use_db_config' => env('GCS_USE_DB_CONFIG', true), +];