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

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