Added helper file, replaced size file for human version as a helper method, next prev buttons for chapters.

This commit is contained in:
Krzysztof Płaczek
2022-12-07 11:37:50 +01:00
parent b992d05312
commit 25e28f8193
7 changed files with 82 additions and 16 deletions

View File

@@ -10,9 +10,13 @@ class ChapterController extends Controller
public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService) public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService)
{ {
$symfonyCastDlService->videoSize($chapter); $symfonyCastDlService->videoSize($chapter);
if($chapter->sync_offline){ if ($chapter->sync_offline) {
$symfonyCastDlService->downloadFile($chapter); $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'));
} }
} }

View File

@@ -36,13 +36,20 @@ class Chapter extends Model
'sync_offline', 'sync_offline',
]; ];
protected $appends = ['video_path', 'directory_path']; protected $appends = ['video_path', 'directory_path', 'video_url'];
public function course(): BelongsTo public function course(): BelongsTo
{ {
return $this->belongsTo(Course::class); 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 protected function videoPath(): Attribute
{ {
return Attribute::make( return Attribute::make(
@@ -68,9 +75,7 @@ class Chapter extends Model
{ {
return Attribute::make( return Attribute::make(
get: function () { get: function () {
$base = log($this->video_size) / log(1024); return formatFileSize($this->video_size);
$suffix = ["", "k", "M", "G", "T"][floor($base)];
return round(pow(1024, $base - floor($base)), 2) . $suffix;
}, },
); );
} }

View File

@@ -33,6 +33,7 @@ class Course extends Model
'published_at', 'published_at',
]; ];
protected $appends = ['total_size', 'total_size_human'];
protected $dates = [ protected $dates = [
'published_at' 'published_at'
]; ];
@@ -53,9 +54,7 @@ class Course extends Model
{ {
return Attribute::make( return Attribute::make(
get: function () { get: function () {
$base = log($this->total_size) / log(1024); return formatFileSize($this->total_size);
$suffix = ["", "k", "M", "G", "T"][floor($base)];
return round(pow(1024, $base - floor($base)), 2) . $suffix;
}, },
); );
} }

22
app/helpers.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
if (!function_exists('formatFileSize')) {
function formatFileSize($fileSizeInBytes): string
{
$base = log($fileSizeInBytes) / log(1024);
$suffix = ["", "k", "M", "G", "T"][floor($base)];
$size = round(pow(1024, $base - floor($base)), 2);
return !is_nan($size) ? $size . $suffix : '0B';
}
}
if (!function_exists('folderSize')) {
function folderSize($dir): int
{
$size = 0;
foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : folderSize($each);
}
return $size;
}
}

View File

@@ -2,7 +2,10 @@
"name": "laravel/laravel", "name": "laravel/laravel",
"type": "project", "type": "project",
"description": "The Laravel Framework.", "description": "The Laravel Framework.",
"keywords": ["framework", "laravel"], "keywords": [
"framework",
"laravel"
],
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "^8.0.2", "php": "^8.0.2",
@@ -27,7 +30,10 @@
"App\\": "app/", "App\\": "app/",
"Database\\Factories\\": "database/factories/", "Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/" "Database\\Seeders\\": "database/seeders/"
} },
"files": [
"app/helpers.php"
]
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {

View File

@@ -1,14 +1,26 @@
<x-layout> <x-layout>
<h1> <h1>
{{ $chapter->course->name }} <a href="/">List</a> &raquo; <a
href="{{ route('course.index', ['course' => $chapter->course_id]) }}">{{ $chapter->course->name }}</a>
<br> <br>
<small class="text-muted">{{ $chapter->title }}</small> <small class="text-muted">{{ $chapter->title }}</small>
</h1> </h1>
<div> <div>
<div class="float-none">
@if($prev)
<div class="float-start m-2"><a href="{{ route('course.chapter', ['chapter' => $prev->id]) }}">Prev</a>
</div>
@endif
@if($next)
<div class="float-end m-2"><a href="{{ route('course.chapter', ['chapter' => $next->id]) }}">Next</a>
</div>
@endif
</div>
<div class="mx-auto" style="width: 500px;"> <div class="mx-auto" style="width: 500px;">
@if($chapter->is_video_file) @if($chapter->is_video_file)
<video class="mx-auto" width="500" controls> <video class="mx-auto" width="500" controls>
<source src="/{{ $chapter->video_path }}" type="video/mp4"/> <source src="{{ $chapter->video_url }}" type="video/mp4"/>
</video> </video>
@endif @endif
</div> </div>

View File

@@ -9,17 +9,35 @@
<th>status</th> <th>status</th>
<th>Chapters</th> <th>Chapters</th>
<th>Published at</th> <th>Published at</th>
<th>Estimate file size</th>
<th>Actual size</th>
</tr> </tr>
</thead> </thead>
@foreach($courses as $course) @foreach($courses as $course)
<tr> <tr>
<td class="align-middle">{{ $course->id }}</td> <td class="align-middle">{{ $course->id }}</td>
<td><img src="{{ $course->thumbnail }}" alt="{{ $course->name }}" class="img-thumbnail" style="width: 70px;"></td> <td><img src="{{ $course->thumbnail }}" alt="{{ $course->name }}" class="img-thumbnail"
<td class="align-middle"><a href="{{ route('course.index', ['course' => $course->id]) }}">{{ $course->name }}</a></td> style="width: 70px;"></td>
<td class="align-middle"><a
href="{{ route('course.index', ['course' => $course->id]) }}">{{ $course->name }}</a></td>
<td class="align-middle">{{ $course->status }}</td> <td class="align-middle">{{ $course->status }}</td>
<td class="align-middle">{{ $course->chapters_count }} / {{ $course->numberofchapters }}</td> <td class="align-middle">{{ $course->chapters_count }} / {{ $course->numberofchapters }}</td>
<td class="align-middle"><abbr title="{{ $course->published_at?->format('Y-m-d') }}">{{ $course->published_at?->diffForHumans() }}</abbr></td> <td class="align-middle"><abbr
title="{{ $course->published_at?->format('Y-m-d') }}">{{ $course->published_at?->diffForHumans() }}</abbr>
</td>
<td class="align-middle text-end pe-4">{{ $course->total_size_human }}</td>
<td class="align-middle text-end pe-4">{{formatFileSize(folderSize(public_path($course->id)))}}</td>
</tr> </tr>
@endforeach @endforeach
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="align-middle text-end pe-4">{{ formatFileSize($courses->sum('total_size')) }}</td>
<td class="align-middle text-end pe-4">{{formatFileSize(folderSize(public_path()))}}</td>
</tr>
</table> </table>
</x-layout> </x-layout>