Changes
This commit is contained in:
Regular → Executable
+25
-105
@@ -26,12 +26,11 @@ class CreateVkPostJob implements ShouldQueue
|
||||
protected $message;
|
||||
protected $images;
|
||||
protected $videos;
|
||||
protected $documents;
|
||||
protected $publish_date;
|
||||
protected $public_id;
|
||||
|
||||
public function __construct(
|
||||
int $post_id, string $title, string $message, array $images = [], array $videos = [], array $documents = [], ?int $publish_date = null
|
||||
int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null
|
||||
)
|
||||
{
|
||||
$this->post_id = $post_id;
|
||||
@@ -39,7 +38,6 @@ class CreateVkPostJob implements ShouldQueue
|
||||
$this->message = $message;
|
||||
$this->images = $images;
|
||||
$this->videos = $videos;
|
||||
$this->documents = $documents;
|
||||
$this->publish_date = $publish_date;
|
||||
$this->public_id = config('services.vk.public_id');
|
||||
}
|
||||
@@ -54,43 +52,35 @@ class CreateVkPostJob implements ShouldQueue
|
||||
|
||||
$from_group = 1;
|
||||
|
||||
$doc_attachments = !empty($this->documents) ? $this->prepareDocuments($this->documents) : '';
|
||||
$doc_list = $doc_attachments ? explode(',', $doc_attachments) : [];
|
||||
$doc_count = count($doc_list);
|
||||
// Документы больше не загружаются — они добавляются как ссылки в тексте поста
|
||||
// Считаем только видео и изображения
|
||||
$video_attachments = !empty($this->videos) ? $this->prepareVideos($this->videos) : '';
|
||||
$video_list = $video_attachments ? explode(',', $video_attachments) : [];
|
||||
|
||||
// Все 10 слотов доступны для видео и изображений
|
||||
$available_slots = 10 - count($video_list);
|
||||
$attached_videos = array_slice($video_list, 0, count($video_list));
|
||||
$remaining_slots = $available_slots;
|
||||
|
||||
Log::info("{$logPrefix} Загружено документов: {$doc_count}");
|
||||
Log::info("{$logPrefix} Видео: " . count($attached_videos) . ", Осталось слотов для фото: {$remaining_slots}");
|
||||
|
||||
if ($doc_count >= 10) {
|
||||
$selected_attachments = array_slice($doc_list, 0, 10);
|
||||
$image_list = [];
|
||||
Log::info("{$logPrefix} Лимит вложений заполнен документами.");
|
||||
$image_list = [];
|
||||
if ($remaining_slots > 0 && !empty($this->images)) {
|
||||
$images_to_attach = array_slice($this->images, 0, $remaining_slots);
|
||||
|
||||
Log::info("{$logPrefix} Начинаем подготовку фото для стены", ['files' => $images_to_attach]);
|
||||
|
||||
$image_attachments = $this->prepareWallPhotos($images_to_attach);
|
||||
|
||||
Log::info("{$logPrefix} Результат подготовки фото (строка): '{$image_attachments}'");
|
||||
|
||||
$image_list = $image_attachments ? explode(',', $image_attachments) : [];
|
||||
} else {
|
||||
$video_attachments = !empty($this->videos) ? $this->prepareVideos($this->videos) : '';
|
||||
$video_list = $video_attachments ? explode(',', $video_attachments) : [];
|
||||
$available_slots = 10 - $doc_count;
|
||||
$attached_videos = array_slice($video_list, 0, min(count($video_list), $available_slots));
|
||||
$remaining_slots = $available_slots - count($attached_videos);
|
||||
|
||||
Log::info("{$logPrefix} Видео: " . count($attached_videos) . ", Осталось слотов для фото: {$remaining_slots}");
|
||||
|
||||
if ($remaining_slots > 0 && !empty($this->images)) {
|
||||
$images_to_attach = array_slice($this->images, 0, $remaining_slots);
|
||||
|
||||
Log::info("{$logPrefix} Начинаем подготовку фото для стены", ['files' => $images_to_attach]);
|
||||
|
||||
$image_attachments = $this->prepareWallPhotos($images_to_attach);
|
||||
|
||||
Log::info("{$logPrefix} Результат подготовки фото (строка): '{$image_attachments}'");
|
||||
|
||||
$image_list = $image_attachments ? explode(',', $image_attachments) : [];
|
||||
} else {
|
||||
$image_list = [];
|
||||
Log::info("{$logPrefix} Фото не будут добавлены (нет слотов или нет фото).");
|
||||
}
|
||||
|
||||
$selected_attachments = array_merge($doc_list, $attached_videos, $image_list);
|
||||
Log::info("{$logPrefix} Фото не будут добавлены (нет слотов или нет фото).");
|
||||
}
|
||||
|
||||
$selected_attachments = array_merge($attached_videos, $image_list);
|
||||
|
||||
if (count($this->images) > count($image_list)) {
|
||||
Log::info("{$logPrefix} Не все фото вошли в пост. Создаем альбом.");
|
||||
|
||||
@@ -303,76 +293,6 @@ class CreateVkPostJob implements ShouldQueue
|
||||
return $uploadedVideos;
|
||||
}
|
||||
|
||||
private function prepareDocuments(array $documents): string
|
||||
{
|
||||
// Ваш старый код, добавил только лог ошибки
|
||||
$baseUrl = config('app.url');
|
||||
try {
|
||||
$documentPaths = array_map(function($doc) use ($baseUrl) {
|
||||
return $baseUrl . $doc;
|
||||
}, $documents);
|
||||
// Log::info(json_encode($documentPaths)); // Старый лог
|
||||
$uploadedDocs = $this->uploadDocuments($documentPaths, $this->public_id);
|
||||
return implode(',', $uploadedDocs);
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Ошибка при подготовке документов: " . $e->getMessage());
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private function uploadDocuments(array $documentPaths, ?int $groupId = null): array
|
||||
{
|
||||
// Ваш старый код
|
||||
$uploadedDocs = [];
|
||||
|
||||
foreach ($documentPaths as $docPath) {
|
||||
try {
|
||||
$uploadServerResponse = Http::get('https://api.vk.ru/method/docs.getWallUploadServer', [
|
||||
'group_id' => $groupId,
|
||||
'access_token' => (new VkAuthService())->getToken()->access_token,
|
||||
'v' => '5.131',
|
||||
]);
|
||||
|
||||
$uploadServerData = $uploadServerResponse->json();
|
||||
// Log::info($uploadServerData); // Старый лог
|
||||
if (!isset($uploadServerData['response']['upload_url'])) {
|
||||
throw new \Exception('Не удалось получить сервер для загрузки документа: ' . $docPath);
|
||||
}
|
||||
$uploadUrl = $uploadServerData['response']['upload_url'];
|
||||
|
||||
$uploadResponse = Http::attach(
|
||||
'file',
|
||||
file_get_contents($docPath),
|
||||
basename($docPath)
|
||||
)->post($uploadUrl);
|
||||
|
||||
$uploadData = $uploadResponse->json();
|
||||
if (!isset($uploadData['file'])) {
|
||||
throw new \Exception('Не удалось загрузить документ: ' . $docPath);
|
||||
}
|
||||
|
||||
$saveResponse = Http::get('https://api.vk.ru/method/docs.save', [
|
||||
'file' => $uploadData['file'],
|
||||
'access_token' => (new VkAuthService())->getToken()->access_token,
|
||||
'v' => '5.131',
|
||||
]);
|
||||
|
||||
$saveData = $saveResponse->json();
|
||||
|
||||
if (!isset($saveData['response']['doc'])) {
|
||||
throw new \Exception('Не удалось сохранить документ: ' . $docPath);
|
||||
}
|
||||
|
||||
$doc = $saveData['response']['doc'];
|
||||
$attachment = "doc{$doc['owner_id']}_{$doc['id']}";
|
||||
$uploadedDocs[] = $attachment;
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Ошибка при загрузке документа {$docPath}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
return $uploadedDocs;
|
||||
}
|
||||
|
||||
public function createAlbum(string $title, $images)
|
||||
{
|
||||
// Ваш код
|
||||
|
||||
Reference in New Issue
Block a user