Moved video files to public/videos directory, small refactoring.

This commit is contained in:
Krzysztof Płaczek
2023-03-30 12:46:30 +02:00
parent 5902bf7c2b
commit 5784cf8ed3
10 changed files with 42 additions and 41 deletions

View File

@@ -3,12 +3,11 @@
namespace App\Http\Controllers;
use App\Http\SymfonyCastDl\SymfonyCastDlService;
use App\Models\Chapter;
use App\Models\Course;
class ChapterController extends Controller
{
public function index(Course $course, int $chapter, SymfonyCastDlService $symfonyCastDlService)
public function __invoke(Course $course, int $chapter, SymfonyCastDlService $symfonyCastDlService)
{
$chapter = $course->chapters->firstWhere('order', $chapter);

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Http\Controllers\Course;
use App\Http\Controllers\Controller;
use App\Jobs\DownloadVideoFile;
use App\Models\Course;
use Illuminate\Http\RedirectResponse;
class Sync extends Controller
{
public function __invoke(Course $course): RedirectResponse
{
$course->chapters->each->update(['sync_offline' => 1]);
$course->chapters->each(fn($chapter) => DownloadVideoFile::dispatch($chapter->id));
return redirect()->back();
}
}

View File

@@ -11,16 +11,8 @@ use Illuminate\View\View;
class CourseController extends Controller
{
public function index(Course $course): View
public function __invoke(Course $course): View
{
return view('course.index', compact('course'));
}
public function sync(Course $course): \Illuminate\Http\RedirectResponse
{
$course->chapters->each->update(['sync_offline' => 1]);
$course->chapters->each(fn($chapter) => DownloadVideoFile::dispatch($chapter->id));
return redirect()->back();
}
}

View File

@@ -5,14 +5,19 @@ namespace App\Http\Controllers;
use App\Http\SymfonyCastDl\HtmlParser;
use App\Http\SymfonyCastDl\SymfonyCastDlService;
use App\Models\Course;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class Index extends Controller
{
public function index(string $track = null)
{
$courses = Course::where('tracks', 'like', '%'.$track.'%')->with('chapters')->withCount('chapters')->get();
$courses = Course::where('tracks', 'like', '%' . $track . '%')->with('chapters')->withCount([
'chapters',
'chapters as chapters_to_sync' => function (Builder $query) {
$query->where('sync_offline', 1);
},
])->get();
return view('index', compact(['courses']));
}

View File

@@ -18,11 +18,8 @@ class SymfonyCastDlService
'base_uri' => config('symfonycast.base_url'),
'cookies' => true
]);
$response = $this->client->get('login');
$token = $htmlParser->getCsrfToken($response);
$this->client->post('login', [
'form_params' => [
'email' => config('symfonycast.login'),
@@ -38,7 +35,6 @@ class SymfonyCastDlService
public function getInfo(): void
{
$coursePage = $this->client->get('courses/filtering');
$courses = $this->htmlParser->getCourses($coursePage);
$courses->each->save();
/** @var Course $course */
@@ -68,7 +64,7 @@ class SymfonyCastDlService
return;
}
if (!is_dir($chapter->directory_path)) {
mkdir($chapter->directory_path);
mkdir(directory: $chapter->directory_path, recursive: true);
}
if (!$chapter->is_video_file) {
$this->client->request(

View File

@@ -46,7 +46,9 @@ class Chapter extends Model
protected function videoUrl(): Attribute
{
return Attribute::make(
get: fn() => url($this->course_id . '/' . $this->order . '.mp4')
get: fn() => url(
'videos' . DIRECTORY_SEPARATOR . $this->course_id . DIRECTORY_SEPARATOR . $this->order . '.mp4'
)
);
}
@@ -60,7 +62,7 @@ class Chapter extends Model
protected function directoryPath(): Attribute
{
return Attribute::make(
get: fn() => public_path($this->course_id)
get: fn() => public_path('videos' . DIRECTORY_SEPARATOR . $this->course_id)
);
}

View File

@@ -55,9 +55,7 @@ class Course extends Model
public function totalSizeHuman(): Attribute
{
return Attribute::make(
get: function () {
return formatFileSize($this->total_size);
},
get: fn() => formatFileSize($this->total_size),
);
}
}