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

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