sintonia_webapp/app/Http/Controllers/PodcastEpisodeController.php

139 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Podcast\CeleryTask;
use App\Models\Podcast\Podcast;
use App\Models\Podcast\PodcastEpisode;
use App\Models\Podcast\ThirdPartyTrackReference;
use Celery\Celery;
use DateTime;
use DateTimeZone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class PodcastEpisodeController extends Controller
{
private static $_CELERY_MESSAGE_TIMEOUT = 900000; // 15 minutes
public function index(Request $request) {
$user = Auth::user();
try {
if (!$user->id) {
throw new \Exception('You must be logged in');
}
return PodcastEpisode::where('podcast_id','=',$request->podcast_id)->get()->toJson();
} catch (\Exception $exception) {
return response($exception->getMessage(), 500);
}
}
public function store(Request $request) {
$user = Auth::user();
try {
if (!$user->id) {
throw new \Exception('You must be logged in');
}
$podcastEpisode = new PodcastEpisode();
$podcastEpisode->fill([
'podcast_id' => $request->podcast_id,
'publication_date' =>$request->episode['pubDate'],
'download_url' => $request->episode['link'],
'episode_guid' => $request->episode['guid'],
'episode_title' => $request->episode['title'],
'episode_description' => htmlentities($request->episode['description']),
])->save();
$brokerTask = $this->downloadPodcastEpisode($request, $podcastEpisode);
$ref = new ThirdPartyTrackReference();
$ref->fill([
'service' => 'podcast',
'foreign_id' => $podcastEpisode->id,
'file_id' => null
])->save();
$task = new CeleryTask();
$timezone = new DateTimeZone(getenv('APP_TIMEZONE'));
$task->fill([
'task_id' => $brokerTask->task_id,
'track_reference' => $ref->id,
'name' => 'podcast-download',
'dispatch_time' => new DateTime('now', $timezone),
'status' => 'PENDING'
])->save();
return $podcastEpisode->toJson();
} catch (\Exception $exception) {
return response($exception->getMessage(), 500);
}
}
public function checkPodcastEpisodeDownload($id) {
$episode = PodcastEpisode::where('id',$id)->firstOrFail();
return $episode->third_party_track_reference->celery_task->status;
}
public function savePodcastEpisode(Request $request) {
if(isset($request->file)) {
try {
$file = (new FileController())->store($request);
} catch (\Exception $e) {
return response($e->getMessage(), 500);
}
if ($file) {
return $file;
}
return json_encode([
'id' => 0
]);
}
}
private function downloadPodcastEpisode(Request $request, $podcastEpisode) {
$request->validate([
'podcast_id' => 'required',
'episode_url' => 'required',
'episode_title' => 'required',
]);
try {
$podcast = Podcast::findOrFail($request->podcast_id);
$conn = new Celery(
config('rabbitmq.host'),
config('rabbitmq.user'),
config('rabbitmq.password'),
config('rabbitmq.vhost'),
'podcast',
'podcast',
config('rabbitmq.port'),
false,
self::$_CELERY_MESSAGE_TIMEOUT
);
$data = [
'episode_id' => $podcastEpisode->id,
'episode_url' => $request->episode_url,
'episode_title' => $podcastEpisode->episode_title,
'podcast_name' => $podcast->title,
'override_album' => 'false' //ToDo connect $album_override from imported_podcast,
];
$taskId = $conn->PostTask('podcast-download', $data, true, 'podcast');
return $taskId;
} catch (\Exception $exception) {
Log::error($exception->getMessage());
die($exception->getMessage());
}
}
}