Downloading files, calculating video size.

This commit is contained in:
Krzysztof Płaczek
2022-11-17 11:38:20 +01:00
parent 2c62880282
commit 55c104629f
9 changed files with 77 additions and 23 deletions

View File

@@ -10,7 +10,9 @@ class ChapterController extends Controller
public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService)
{
$symfonyCastDlService->videoSize($chapter);
$symfonyCastDlService->downloadFile($chapter);
if($chapter->sync_offline){
$symfonyCastDlService->downloadFile($chapter);
}
return view('chapter.index', compact('chapter'));
}
}

View File

@@ -7,7 +7,6 @@ use App\Models\Chapter;
use App\Models\Course;
use GuzzleHttp\TransferStats;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
class SymfonyCastDlService
{
@@ -53,25 +52,22 @@ class SymfonyCastDlService
public function videoSize(Chapter $chapter): Chapter
{
try {
if (!$chapter->video_size) {
$response = $this->client->head($chapter->video_link);
if ($response->hasHeader('Content-Length')) {
$chapter->video_size = $response->getHeader('Content-Length')[0];
}
if (!$chapter->video_size) {
$response = $this->client->head($chapter->video_link);
if ($response->hasHeader('Content-Length')) {
$chapter->video_size = (int)$response->getHeader('Content-Length')[0];
$chapter->save();
}
} catch (\Exception $exception) {
}
return $chapter;
}
public function downloadFile(Chapter $chapter): void
{
if (!is_dir($chapter->directory_path)) {
mkdir($chapter->directory_path);
if (!is_dir(public_path($chapter->directory_path))) {
mkdir(public_path($chapter->directory_path));
}
if (!is_file($chapter->video_path)) {
if (!$chapter->is_video_file) {
$this->client->request(
'GET',
$chapter->video_link,