This commit is contained in:
F4ilji
2026-03-23 12:05:08 +05:00
parent 1256adba98
commit c350f712dc
2 changed files with 32 additions and 9 deletions
@@ -139,13 +139,36 @@ class DownloadAttachmentsTask
*/ */
private function generateUniqueFilename(string $originalName): string private function generateUniqueFilename(string $originalName): string
{ {
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION)); // Декодируем MIME-кодировку (=?UTF-8?B?...?=)
$basename = pathinfo($originalName, PATHINFO_FILENAME); $decodedName = mb_decode_mimeheader($originalName) ?: $originalName;
// Если не декодировалось, пробуем другой метод
if ($decodedName === $originalName && str_contains($originalName, '=?')) {
$decodedName = iconv_mime_decode($originalName, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8') ?: $originalName;
}
// Очищаем имя от специальных символов // Очищаем имя от специальных символов
$basename = preg_replace('/[^a-zA-Z0-9_\-\p{L}]/u', '_', $basename); $decodedName = preg_replace('/[^a-zA-Z0-9_\-\p{L}.]/u', '_', $decodedName);
// Добавляем timestamp для уникальности // Удаляем множественные подчёркивания
return $basename . '_' . time() . '_' . bin2hex(random_bytes(4)) . '.' . $extension; $decodedName = preg_replace('/_+/', '_', $decodedName);
// Получаем расширение
$extension = strtolower(pathinfo($decodedName, PATHINFO_EXTENSION));
// Если расширение пустое, пробуем определить из оригинального имени
if (empty($extension) && str_contains($originalName, '.docx')) {
$extension = 'docx';
} elseif (empty($extension) && str_contains($originalName, '.doc')) {
$extension = 'doc';
} elseif (empty($extension) && str_contains($originalName, '.pdf')) {
$extension = 'pdf';
}
// Генерируем уникальное имя
$basename = pathinfo($decodedName, PATHINFO_FILENAME);
$basename = mb_substr($basename, 0, 100); // Ограничиваем длину
return $basename . '_' . time() . '_' . bin2hex(random_bytes(4)) . '.' . ($extension ?: 'bin');
} }
} }
@@ -26,7 +26,7 @@ class VkPostPublisher
if ($settings['vk']) { if ($settings['vk']) {
$text = $this->generateContentForVk($post); $text = $this->generateContentForVk($post);
$imagesFromPost = $post->images; $imagesFromPost = $post->images ?? [];
$imagesFromContent = $this->extractImagesFromContent($post->content); $imagesFromContent = $this->extractImagesFromContent($post->content);
$allImages = array_merge($imagesFromPost, $imagesFromContent); $allImages = array_merge($imagesFromPost, $imagesFromContent);
$imageLinks = $this->generateImageLinksForVk($allImages); $imageLinks = $this->generateImageLinksForVk($allImages);
@@ -51,7 +51,7 @@ class VkPostPublisher
if ($settings['vk']) { if ($settings['vk']) {
$text = $this->generateContentForVk($post); $text = $this->generateContentForVk($post);
$imagesFromPost = $post->images; $imagesFromPost = $post->images ?? [];
$imagesFromContent = $this->extractImagesFromContent($post->content); $imagesFromContent = $this->extractImagesFromContent($post->content);
$allImages = array_merge($imagesFromPost, $imagesFromContent); $allImages = array_merge($imagesFromPost, $imagesFromContent);
$imageLinks = $this->generateImageLinksForVk($allImages); $imageLinks = $this->generateImageLinksForVk($allImages);