'tcp', 'host' => EnvLoader::getRequired('REDIS_HOST'), 'port' => (int)EnvLoader::getRequired('REDIS_PORT'), 'password' => EnvLoader::getRequired('REDIS_PASSWORD') ]); // Получаем маппинг $mapping = $redis->get("crm:onlyoffice:key:$key"); if (!$mapping) { error_log("ERROR: No mapping found for key $key"); // Сохраняем в резервную папку $s3Path = 'onlyoffice_saved/' . $key . '_' . date('Y-m-d_H-i-s') . '.docx'; } else { $mappingData = json_decode($mapping, true); $s3Path = $mappingData['s3_path']; error_log("Found mapping: $key → $s3Path"); } // Скачиваем файл от OnlyOffice $fileContent = file_get_contents($downloadUrl); if ($fileContent === false) { throw new Exception("Failed to download file from OnlyOffice"); } error_log("Downloaded: " . strlen($fileContent) . " bytes"); // Инициализируем S3 клиент $s3Client = new Aws\S3\S3Client([ 'version' => 'latest', 'region' => 'ru-1', 'endpoint' => 'https://s3.twcstorage.ru', 'use_path_style_endpoint' => true, 'credentials' => [ 'key' => EnvLoader::getRequired('S3_ACCESS_KEY'), 'secret' => EnvLoader::getRequired('S3_SECRET_KEY') ], 'suppress_php_deprecation_warning' => true ]); $bucket = 'f9825c87-4e3558f6-f9b6-405c-ad3d-d1535c49b61c'; // Загружаем в S3 (ПЕРЕЗАПИСЫВАЕМ оригинальный файл!) $result = $s3Client->putObject([ 'Bucket' => $bucket, 'Key' => $s3Path, 'Body' => $fileContent, 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'Metadata' => [ 'saved_by' => 'onlyoffice', 'saved_at' => date('Y-m-d H:i:s') ] ]); error_log("✅ File saved to S3: $s3Path"); // Публикуем событие в Redis $redis->publish('crm:file:events', json_encode([ 'type' => 'file_modified', 'source' => 'onlyoffice', 'timestamp' => date('c'), 'path' => $s3Path, 'size' => strlen($fileContent), 'action' => 'updated_via_onlyoffice' ])); http_response_code(200); echo json_encode(['error' => 0]); exit; } catch (Exception $e) { error_log("ERROR: " . $e->getMessage()); http_response_code(500); echo json_encode(['error' => 1, 'message' => $e->getMessage()]); exit; } } // Другие статусы http_response_code(200); echo json_encode(['error' => 0]); exit; } http_response_code(200); echo json_encode(['error' => 0]); ?>