changes
This commit is contained in:
@@ -19,88 +19,68 @@ class ConvertDocToDocxTask
|
||||
{
|
||||
Log::info('[ConvertDocToDocxTask] Начало конвертации', [
|
||||
'file' => $file->getClientOriginalName(),
|
||||
'size' => $file->getSize(),
|
||||
'extension' => $file->getClientOriginalExtension(),
|
||||
]);
|
||||
|
||||
// Проверяем расширение
|
||||
if (strtolower($file->getClientOriginalExtension()) !== 'doc') {
|
||||
Log::warning('[ConvertDocToDocxTask] Неверное расширение', [
|
||||
'extension' => $file->getClientOriginalExtension(),
|
||||
]);
|
||||
Log::warning('[ConvertDocToDocxTask] Неверное расширение', ['extension' => $file->getClientOriginalExtension()]);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Сохраняем исходный файл во временную директорию
|
||||
// Сохраняем исходный файл
|
||||
$originalPath = $file->storeAs('temp/conversion', 'original_' . time() . '_' . Str::random(10) . '.doc', 'local');
|
||||
$absoluteOriginalPath = Storage::disk('local')->path($originalPath);
|
||||
|
||||
Log::info('[ConvertDocToDocxTask] Файл сохранен', [
|
||||
'original_path' => $originalPath,
|
||||
'absolute_path' => $absoluteOriginalPath,
|
||||
'file_exists' => file_exists($absoluteOriginalPath),
|
||||
]);
|
||||
|
||||
// Проверяем существование файла
|
||||
if (!file_exists($absoluteOriginalPath)) {
|
||||
Log::error('[ConvertDocToDocxTask] Файл не сохранен', [
|
||||
'path' => $absoluteOriginalPath,
|
||||
]);
|
||||
Log::error('[ConvertDocToDocxTask] Файл не сохранен', ['path' => $absoluteOriginalPath]);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Директория для вывода
|
||||
$outputDir = dirname($absoluteOriginalPath);
|
||||
|
||||
// Конвертируем через LibreOffice
|
||||
// Создаем уникальный путь для профиля LibreOffice в /tmp, чтобы избежать ошибок прав в /var/www
|
||||
$userProfileDir = '/tmp/libreoffice_profile_' . uniqid();
|
||||
|
||||
// Формируем команду с изоляцией профиля и установкой HOME
|
||||
// -env:UserInstallation указывает LibreOffice использовать временную папку вместо системной .cache
|
||||
$command = sprintf(
|
||||
'libreoffice --headless --convert-to docx --outdir %s %s 2>&1',
|
||||
'export HOME=/tmp && libreoffice -env:UserInstallation=file://%s --headless --convert-to docx --outdir %s %s 2>&1',
|
||||
escapeshellarg($userProfileDir),
|
||||
escapeshellarg($outputDir),
|
||||
escapeshellarg($absoluteOriginalPath)
|
||||
);
|
||||
|
||||
Log::info('[ConvertDocToDocxTask] Выполнение команды', [
|
||||
'command' => $command,
|
||||
]);
|
||||
Log::info('[ConvertDocToDocxTask] Выполнение команды', ['command' => $command]);
|
||||
|
||||
$output = shell_exec($command);
|
||||
|
||||
Log::info('[ConvertDocToDocxTask] Результат конвертации', [
|
||||
'output' => $output,
|
||||
]);
|
||||
|
||||
// Ищем конвертированный файл
|
||||
// Ожидаемый путь результата
|
||||
$docxPath = preg_replace('/\.doc$/i', '.docx', $absoluteOriginalPath);
|
||||
|
||||
Log::info('[ConvertDocToDocxTask] Проверка результата', [
|
||||
'expected_path' => $docxPath,
|
||||
'file_exists' => file_exists($docxPath),
|
||||
]);
|
||||
// Удаляем временный профиль LibreOffice сразу после выполнения
|
||||
if (is_dir($userProfileDir)) {
|
||||
shell_exec('rm -rf ' . escapeshellarg($userProfileDir));
|
||||
}
|
||||
|
||||
if (!file_exists($docxPath)) {
|
||||
// Логируем ошибку
|
||||
Log::error('[ConvertDocToDocxTask] Конвертация не удалась', [
|
||||
'file' => $file->getClientOriginalName(),
|
||||
'output' => $output,
|
||||
'expected_path' => $docxPath,
|
||||
]);
|
||||
|
||||
// Удаляем исходный файл
|
||||
Storage::disk('local')->delete($originalPath);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Удаляем исходный .doc файл
|
||||
// Удаляем исходный .doc
|
||||
Storage::disk('local')->delete($originalPath);
|
||||
|
||||
// Возвращаем относительный путь к .docx файлу
|
||||
$relativeDocxPath = preg_replace('/\.doc$/i', '.docx', $originalPath);
|
||||
|
||||
Log::info('[ConvertDocToDocxTask] Конвертация успешна', [
|
||||
'result_path' => $relativeDocxPath,
|
||||
]);
|
||||
Log::info('[ConvertDocToDocxTask] Конвертация успешна', ['result_path' => $relativeDocxPath]);
|
||||
|
||||
return $relativeDocxPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user