diff --git a/app/Console/Commands/SyncFiles.php b/app/Console/Commands/SyncFiles.php new file mode 100644 index 0000000..3f69101 --- /dev/null +++ b/app/Console/Commands/SyncFiles.php @@ -0,0 +1,21 @@ +get(); + $chapters->each(fn($chapter) => DownloadVideoFile::dispatch($chapter->id)); + return self::SUCCESS; + } +} diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index a779207..c1df102 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -10,7 +10,9 @@ class ChapterController extends Controller public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService) { $symfonyCastDlService->videoSize($chapter); - $symfonyCastDlService->downloadFile($chapter); + if($chapter->sync_offline){ + $symfonyCastDlService->downloadFile($chapter); + } return view('chapter.index', compact('chapter')); } } diff --git a/app/Http/SymfonyCastDl/SymfonyCastDlService.php b/app/Http/SymfonyCastDl/SymfonyCastDlService.php index 84a596d..34ccaec 100644 --- a/app/Http/SymfonyCastDl/SymfonyCastDlService.php +++ b/app/Http/SymfonyCastDl/SymfonyCastDlService.php @@ -7,7 +7,6 @@ use App\Models\Chapter; use App\Models\Course; use GuzzleHttp\TransferStats; use GuzzleHttp\Client; -use Illuminate\Support\Str; class SymfonyCastDlService { @@ -53,25 +52,22 @@ class SymfonyCastDlService public function videoSize(Chapter $chapter): Chapter { - try { - if (!$chapter->video_size) { - $response = $this->client->head($chapter->video_link); - if ($response->hasHeader('Content-Length')) { - $chapter->video_size = $response->getHeader('Content-Length')[0]; - } + if (!$chapter->video_size) { + $response = $this->client->head($chapter->video_link); + if ($response->hasHeader('Content-Length')) { + $chapter->video_size = (int)$response->getHeader('Content-Length')[0]; $chapter->save(); } - } catch (\Exception $exception) { } return $chapter; } public function downloadFile(Chapter $chapter): void { - if (!is_dir($chapter->directory_path)) { - mkdir($chapter->directory_path); + if (!is_dir(public_path($chapter->directory_path))) { + mkdir(public_path($chapter->directory_path)); } - if (!is_file($chapter->video_path)) { + if (!$chapter->is_video_file) { $this->client->request( 'GET', $chapter->video_link, diff --git a/app/Jobs/DownloadVideoFile.php b/app/Jobs/DownloadVideoFile.php index 19f6667..17371f0 100644 --- a/app/Jobs/DownloadVideoFile.php +++ b/app/Jobs/DownloadVideoFile.php @@ -17,11 +17,10 @@ class DownloadVideoFile implements ShouldQueue public function __construct(private int $chapterId) { - // } public function handle(SymfonyCastDlService $symfonyCastDlService) { - $symfonyCastDlService->videoSize(Chapter::find($this->chapterId)); + $symfonyCastDlService->downloadFile(Chapter::find($this->chapterId)); } } diff --git a/app/Jobs/GetVideoFileSize.php b/app/Jobs/GetVideoFileSize.php index b5a658d..ec37c97 100644 --- a/app/Jobs/GetVideoFileSize.php +++ b/app/Jobs/GetVideoFileSize.php @@ -2,7 +2,6 @@ namespace App\Jobs; -use App\Http\SymfonyCastDl\HtmlParser; use App\Http\SymfonyCastDl\SymfonyCastDlService; use App\Models\Chapter; use Illuminate\Bus\Queueable; diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 59670ac..7434594 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** + * @property int $id * @property int $order * @property string $link * @property string $video_link @@ -18,6 +19,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property bool $sync_offline * @property string $video_path * @property string $directory_path + * @property Course $course */ class Chapter extends Model { @@ -34,6 +36,8 @@ class Chapter extends Model 'sync_offline', ]; + protected $appends = ['video_path', 'directory_path']; + public function course(): BelongsTo { return $this->belongsTo(Course::class); @@ -52,4 +56,11 @@ class Chapter extends Model get: fn() => $this->course_id . '.' . $this->course->link ); } + + protected function isVideoFile(): Attribute + { + return Attribute::make( + get: fn() => is_file($this->video_path) + ); + } } diff --git a/app/Models/Course.php b/app/Models/Course.php index 5a9b779..9feae52 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Carbon\Traits\Date; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -16,6 +17,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @property integer $numberofchapters * @property integer $timeswatched * @property Date $published_at + * @property Chapter[] $chapters */ class Course extends Model { @@ -39,4 +41,22 @@ class Course extends Model { return $this->hasMany(Chapter::class); } + + public function totalSize(): Attribute + { + return Attribute::make( + get: fn() => $this->chapters->sum('video_size'), + ); + } + + public function totalSizeHuman(): Attribute + { + 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; + }, + ); + } } diff --git a/resources/views/chapter/index.blade.php b/resources/views/chapter/index.blade.php index c9f06b4..4f4fc5e 100644 --- a/resources/views/chapter/index.blade.php +++ b/resources/views/chapter/index.blade.php @@ -1,13 +1,16 @@

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

- + @if($chapter->is_video_file) + + @endif
diff --git a/resources/views/course/index.blade.php b/resources/views/course/index.blade.php index 11f3c89..f104013 100644 --- a/resources/views/course/index.blade.php +++ b/resources/views/course/index.blade.php @@ -15,13 +15,16 @@ @foreach($course->chapters()->get() as $chapter) {{ $chapter->title }} - {{ $chapter->sync_offline?'Yes':'No' }} {{ $chapter->duration }} + {{ $chapter->sync_offline?'Yes':'no' }} + {{ $chapter->is_video_file?'Yes':'-' }} @endforeach - - - Total: {{ $course->course_duration }} - + + + Total: {{ $course->course_duration }} + + Size: {{ $course->total_size_human }} +