Downloading video to correct directory and displaying it on a chapter page.

This commit is contained in:
Krzysztof Płaczek
2022-11-17 08:24:17 +01:00
parent 21f61b0911
commit 2c62880282
4 changed files with 42 additions and 13 deletions

View File

@@ -11,8 +11,6 @@ class ChapterController extends Controller
{ {
$symfonyCastDlService->videoSize($chapter); $symfonyCastDlService->videoSize($chapter);
$symfonyCastDlService->downloadFile($chapter); $symfonyCastDlService->downloadFile($chapter);
// $symfonyCastDlService->videoSize($chapter); return view('chapter.index', compact('chapter'));
// file_put_contents('movie.avi', file_get_contents($chapter->video_link));
dd($chapter->toArray());
} }
} }

View File

@@ -7,6 +7,7 @@ use App\Models\Chapter;
use App\Models\Course; use App\Models\Course;
use GuzzleHttp\TransferStats; use GuzzleHttp\TransferStats;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Support\Str;
class SymfonyCastDlService class SymfonyCastDlService
{ {
@@ -54,7 +55,6 @@ class SymfonyCastDlService
{ {
try { try {
if (!$chapter->video_size) { if (!$chapter->video_size) {
echo 1;
$response = $this->client->head($chapter->video_link); $response = $this->client->head($chapter->video_link);
if ($response->hasHeader('Content-Length')) { if ($response->hasHeader('Content-Length')) {
$chapter->video_size = $response->getHeader('Content-Length')[0]; $chapter->video_size = $response->getHeader('Content-Length')[0];
@@ -66,16 +66,17 @@ class SymfonyCastDlService
return $chapter; return $chapter;
} }
public function downloadFile(Chapter $chapter): bool public function downloadFile(Chapter $chapter): void
{ {
if (!is_dir($chapter->course_id . '/')) { if (!is_dir($chapter->directory_path)) {
mkdir($chapter->course_id); 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;
} }
} }

View File

@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -15,6 +16,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property string $duration * @property string $duration
* @property integer $course_id * @property integer $course_id
* @property bool $sync_offline * @property bool $sync_offline
* @property string $video_path
* @property string $directory_path
*/ */
class Chapter extends Model class Chapter extends Model
{ {
@@ -35,4 +38,18 @@ class Chapter extends Model
{ {
return $this->belongsTo(Course::class); 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
);
}
} }

View File

@@ -0,0 +1,13 @@
<x-layout>
<h1>
{{ $chapter->course->name }}
<small class="text-muted">{{ $chapter->title }}</small>
</h1>
<div>
<div class="mx-auto" style="width: 500px;">
<video class="mx-auto" width="500" controls>
<source src="/{{ $chapter->video_path }}" type="video/mp4"/>
</video>
</div>
</div>
</x-layout>