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

65 lines
2.2 KiB
PHP

<?php
// 입력값 받기
$fileId = $_POST['fileId'] ?? '';
$rotation = intval($_POST['rotation'] ?? 0);
header('Content-Type: application/json');
if (!$fileId) {
echo json_encode(['success' => false, 'msg' => 'fileId 누락']);
exit;
}
// 구글드라이브 이미지 다운로드 URL
$googleUrl = 'https://drive.google.com/uc?export=download&id=' . urlencode($fileId);
// 임시 디렉토리 및 파일 경로 설정
$tmpDir = $_SERVER['DOCUMENT_ROOT'] . '/tmpimg/';
if (!is_dir($tmpDir)) {
// 재귀적으로 디렉토리 생성 및 권한 설정
if (!mkdir($tmpDir, 0777, true)) {
echo json_encode(['success' => false, 'msg' => '임시 디렉토리 생성 실패']);
exit;
}
}
$tmpName = uniqid('img_', true) . '.jpg';
$tmpPath = $tmpDir . $tmpName;
$tmpUrl = '/tmpimg/' . $tmpName;
// cURL을 사용하여 파일 다운로드
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $googleUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 리다이렉션 따르기
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // SSL 인증서 검증 비활성화 (필요에 따라 사용)
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); // 연결 타임아웃 20초
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 전체 실행 타임아웃 60초
$imgData = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 다운로드 실패 확인
if ($imgData === false || $httpCode !== 200) {
// 실패 로그 추가 (디버깅용)
error_log("Google Drive Download Failed. HTTP Code: {$httpCode}, cURL Error: {$curlError}, File ID: {$fileId}");
echo json_encode(['success' => false, 'msg' => '구글드라이브 이미지 다운로드 실패']);
exit;
}
// 다운로드된 데이터를 파일에 저장
if (file_put_contents($tmpPath, $imgData) === false) {
echo json_encode(['success' => false, 'msg' => '임시 파일 저장 실패']);
exit;
}
// (회전은 프론트에서 CSS로 처리)
echo json_encode([
'success' => true,
'imgUrl' => $tmpUrl,
'rotation' => $rotation
]);