diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index c1df102..3deaae3 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -10,9 +10,13 @@ class ChapterController extends Controller public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService) { $symfonyCastDlService->videoSize($chapter); - if($chapter->sync_offline){ + if ($chapter->sync_offline) { $symfonyCastDlService->downloadFile($chapter); } - return view('chapter.index', compact('chapter')); + $chapters = $chapter->course->chapters; + + $next = $chapters->where('order', '>', $chapter->order)->first(); + $prev = $chapters->where('order', '<', $chapter->order)->sortByDesc('order')->first(); + return view('chapter.index', compact('chapter', 'chapters', 'prev', 'next')); } } diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 341b9ff..031e218 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -36,13 +36,20 @@ class Chapter extends Model 'sync_offline', ]; - protected $appends = ['video_path', 'directory_path']; + protected $appends = ['video_path', 'directory_path', 'video_url']; public function course(): BelongsTo { return $this->belongsTo(Course::class); } + protected function videoUrl(): Attribute + { + return Attribute::make( + get: fn() => url($this->course_id . '/' . $this->order . '.mp4') + ); + } + protected function videoPath(): Attribute { return Attribute::make( @@ -68,9 +75,7 @@ class Chapter extends Model { return Attribute::make( get: function () { - $base = log($this->video_size) / log(1024); - $suffix = ["", "k", "M", "G", "T"][floor($base)]; - return round(pow(1024, $base - floor($base)), 2) . $suffix; + return formatFileSize($this->video_size); }, ); } diff --git a/app/Models/Course.php b/app/Models/Course.php index 9feae52..bd4f0cd 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -33,6 +33,7 @@ class Course extends Model 'published_at', ]; + protected $appends = ['total_size', 'total_size_human']; protected $dates = [ 'published_at' ]; @@ -53,9 +54,7 @@ class Course extends Model { return Attribute::make( get: function () { - $base = log($this->total_size) / log(1024); - $suffix = ["", "k", "M", "G", "T"][floor($base)]; - return round(pow(1024, $base - floor($base)), 2) . $suffix; + return formatFileSize($this->total_size); }, ); } diff --git a/app/helpers.php b/app/helpers.php new file mode 100644 index 0000000..fd45259 --- /dev/null +++ b/app/helpers.php @@ -0,0 +1,22 @@ +