false, 'error' => 'Invalid recordId']); exit; } try { // 1. Получаем filename из БД $db = new mysqli('localhost', 'ci20465_72new', 'EcY979Rn', 'ci20465_72new'); if ($db->connect_error) { throw new Exception('DB connection failed'); } $db->set_charset('utf8mb4'); $stmt = $db->prepare("SELECT filename FROM vtiger_notes WHERE notesid = ?"); $stmt->bind_param('i', $recordId); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); $db->close(); if (!$row || empty($row['filename'])) { throw new Exception('File not found in DB'); } $fileName = $row['filename']; // 2. Извлекаем S3 путь из URL $bucketId = 'f9825c87-4e3558f6-f9b6-405c-ad3d-d1535c49b61c'; $fileName = rawurldecode($fileName); $pos = strpos($fileName, $bucketId . '/'); if ($pos === false) { throw new Exception('Invalid S3 path in filename'); } $s3Path = substr($fileName, $pos + strlen($bucketId) + 1); // 3. Получаем FileID из Redis $redis = new Redis(); if (!$redis->connect('crm.clientright.ru', 6379)) { throw new Exception('Redis connection failed'); } $redis->auth('CRM_Redis_Pass_2025_Secure!'); $redisKey = "crm:nc:fileid:" . $s3Path; $cached = $redis->get($redisKey); if (!$cached) { $redis->close(); throw new Exception('FileID not found in Redis index. Key: ' . substr($redisKey, 0, 100)); } $data = json_decode($cached, true); $fileId = $data['fileId'] ?? null; $redis->close(); if (!$fileId) { throw new Exception('Invalid FileID data in Redis'); } // 4. Формируем URL для Nextcloud $nextcloudUrl = 'https://office.clientright.ru:8443'; $ncPath = '/crm/' . $s3Path; $dirPath = dirname($ncPath); $redirectUrl = $nextcloudUrl . '/apps/files/files/' . $fileId . '?dir=' . urlencode($dirPath) . '&openfile=true'; // 5. Возвращаем успешный ответ echo json_encode([ 'success' => true, 'fileId' => $fileId, 'redirectUrl' => $redirectUrl, 'source' => 'redis', 'recordId' => $recordId ]); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'recordId' => $recordId ]); } exit; ?>