Downloading files, calculating video size.

This commit is contained in:
Krzysztof Płaczek
2022-11-17 11:38:20 +01:00
parent 2c62880282
commit 55c104629f
9 changed files with 77 additions and 23 deletions

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Console\Commands;
use App\Jobs\DownloadVideoFile;
use App\Models\Chapter;
use Illuminate\Console\Command;
class SyncFiles extends Command
{
protected $signature = 'symfonycast:sync';
protected $description = 'Downloads files locally for chapters marked as sync offline';
public function handle()
{
$chapters = Chapter::where('sync_offline', 1)->get();
$chapters->each(fn($chapter) => DownloadVideoFile::dispatch($chapter->id));
return self::SUCCESS;
}
}

View File

@@ -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'));
}
}

View File

@@ -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,

View File

@@ -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));
}
}

View File

@@ -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;

View File

@@ -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)
);
}
}

View File

@@ -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;
},
);
}
}