From 2c62880282c43c3389dede1b4c9d6458d31b0ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Thu, 17 Nov 2022 08:24:17 +0100 Subject: [PATCH] Downloading video to correct directory and displaying it on a chapter page. --- app/Http/Controllers/ChapterController.php | 4 +--- .../SymfonyCastDl/SymfonyCastDlService.php | 21 ++++++++++--------- app/Models/Chapter.php | 17 +++++++++++++++ resources/views/chapter/index.blade.php | 13 ++++++++++++ 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 resources/views/chapter/index.blade.php diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index abae6e5..a779207 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -11,8 +11,6 @@ class ChapterController extends Controller { $symfonyCastDlService->videoSize($chapter); $symfonyCastDlService->downloadFile($chapter); -// $symfonyCastDlService->videoSize($chapter); -// file_put_contents('movie.avi', file_get_contents($chapter->video_link)); - dd($chapter->toArray()); + return view('chapter.index', compact('chapter')); } } diff --git a/app/Http/SymfonyCastDl/SymfonyCastDlService.php b/app/Http/SymfonyCastDl/SymfonyCastDlService.php index 9716cb2..84a596d 100644 --- a/app/Http/SymfonyCastDl/SymfonyCastDlService.php +++ b/app/Http/SymfonyCastDl/SymfonyCastDlService.php @@ -7,6 +7,7 @@ use App\Models\Chapter; use App\Models\Course; use GuzzleHttp\TransferStats; use GuzzleHttp\Client; +use Illuminate\Support\Str; class SymfonyCastDlService { @@ -54,7 +55,6 @@ class SymfonyCastDlService { try { if (!$chapter->video_size) { - echo 1; $response = $this->client->head($chapter->video_link); if ($response->hasHeader('Content-Length')) { $chapter->video_size = $response->getHeader('Content-Length')[0]; @@ -66,16 +66,17 @@ class SymfonyCastDlService return $chapter; } - public function downloadFile(Chapter $chapter): bool + public function downloadFile(Chapter $chapter): void { - if (!is_dir($chapter->course_id . '/')) { - mkdir($chapter->course_id); + if (!is_dir($chapter->directory_path)) { + mkdir($chapter->directory_path); + } + if (!is_file($chapter->video_path)) { + $this->client->request( + 'GET', + $chapter->video_link, + ['sink' => $chapter->video_path], + ); } - $this->client->request( - 'GET', - $chapter->video_link, - ['sink' => $chapter->course_id . '/' . $chapter->id . '.mp4'] - ); - return true; } } diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 29e8d5d..59670ac 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -15,6 +16,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property string $duration * @property integer $course_id * @property bool $sync_offline + * @property string $video_path + * @property string $directory_path */ class Chapter extends Model { @@ -35,4 +38,18 @@ class Chapter extends Model { return $this->belongsTo(Course::class); } + + protected function videoPath(): Attribute + { + return Attribute::make( + get: fn() => $this->directory_path . '/' . $this->id . '.' . $this->link . '.mp4' + ); + } + + protected function directoryPath(): Attribute + { + return Attribute::make( + get: fn() => $this->course_id . '.' . $this->course->link + ); + } } diff --git a/resources/views/chapter/index.blade.php b/resources/views/chapter/index.blade.php new file mode 100644 index 0000000..c9f06b4 --- /dev/null +++ b/resources/views/chapter/index.blade.php @@ -0,0 +1,13 @@ + +

+ {{ $chapter->course->name }} + {{ $chapter->title }} +

+
+
+ +
+
+