Files
sam-kd/sales_manager_scenario/download_consultation.php
hskwon aca1767eb9 초기 커밋: 5130 레거시 시스템
- URL 하드코딩 → .env APP_URL 기반 동적 URL로 변경
- DB 연결 하드코딩 → .env 기반으로 변경
- MySQL strict mode DATE 오류 수정
2025-12-10 20:14:31 +09:00

113 lines
3.4 KiB
PHP

<?php
// 출력 버퍼링 시작
ob_start();
error_reporting(0);
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
// 권한 체크
if (!isset($user_id) || $level > 5) {
header('HTTP/1.0 403 Forbidden');
die('접근 권한이 없습니다.');
}
// 녹음 파일 ID 확인
$consultation_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$manager_id = $user_id;
if ($consultation_id <= 0) {
header('HTTP/1.0 400 Bad Request');
die('잘못된 요청입니다.');
}
try {
$pdo = db_connect();
$sql = "SELECT audio_file_path, created_at
FROM manager_consultations
WHERE id = ? AND manager_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$consultation_id, $manager_id]);
$consultation = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$consultation || empty($consultation['audio_file_path'])) {
header('HTTP/1.0 404 Not Found');
die('오디오 파일을 찾을 수 없습니다.');
}
// GCS URI인 경우 처리 불가 (직접 다운로드 불가)
if (strpos($consultation['audio_file_path'], 'gs://') === 0) {
header('HTTP/1.0 400 Bad Request');
die('GCS에 저장된 파일은 직접 다운로드할 수 없습니다.');
}
// 파일 경로 구성
$file_path = $_SERVER['DOCUMENT_ROOT'] . $consultation['audio_file_path'];
// 경로 정규화
$file_path = str_replace('\\', '/', $file_path);
$file_path = preg_replace('#/+#', '/', $file_path);
// 파일 존재 확인
if (!file_exists($file_path)) {
header('HTTP/1.0 404 Not Found');
die('오디오 파일이 서버에 존재하지 않습니다.');
}
// 파일 확장자 확인
$file_extension = pathinfo($file_path, PATHINFO_EXTENSION) ?: 'webm';
$mime_types = [
'webm' => 'audio/webm',
'wav' => 'audio/wav',
'mp3' => 'audio/mpeg',
'ogg' => 'audio/ogg',
'm4a' => 'audio/mp4'
];
$content_type = isset($mime_types[$file_extension])
? $mime_types[$file_extension]
: 'audio/webm';
// 다운로드 파일명 생성
$date = date('Ymd_His', strtotime($consultation['created_at']));
$download_filename = '상담녹음_' . $date . '.' . $file_extension;
// 출력 버퍼 비우기
ob_clean();
$file_size = filesize($file_path);
if ($file_size === false || $file_size == 0) {
header('HTTP/1.0 500 Internal Server Error');
die('파일을 읽을 수 없습니다.');
}
// 헤더 설정
header('Content-Type: ' . $content_type);
header('Content-Disposition: attachment; filename="' . $download_filename . '"');
header('Content-Length: ' . $file_size);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
// 파일 출력
$handle = @fopen($file_path, 'rb');
if ($handle === false) {
header('HTTP/1.0 500 Internal Server Error');
die('파일을 열 수 없습니다.');
}
while (!feof($handle)) {
$chunk = fread($handle, 8192);
if ($chunk === false) break;
echo $chunk;
flush();
}
fclose($handle);
exit;
} catch (Exception $e) {
header('HTTP/1.0 500 Internal Server Error');
die('파일 다운로드 중 오류가 발생했습니다.');
}