Downloading files, calculating video size.
This commit is contained in:
21
app/Console/Commands/SyncFiles.php
Normal file
21
app/Console/Commands/SyncFiles.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,9 @@ class ChapterController extends Controller
|
|||||||
public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService)
|
public function index(Chapter $chapter, SymfonyCastDlService $symfonyCastDlService)
|
||||||
{
|
{
|
||||||
$symfonyCastDlService->videoSize($chapter);
|
$symfonyCastDlService->videoSize($chapter);
|
||||||
$symfonyCastDlService->downloadFile($chapter);
|
if($chapter->sync_offline){
|
||||||
|
$symfonyCastDlService->downloadFile($chapter);
|
||||||
|
}
|
||||||
return view('chapter.index', compact('chapter'));
|
return view('chapter.index', compact('chapter'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ use App\Models\Chapter;
|
|||||||
use App\Models\Course;
|
use App\Models\Course;
|
||||||
use GuzzleHttp\TransferStats;
|
use GuzzleHttp\TransferStats;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Illuminate\Support\Str;
|
|
||||||
|
|
||||||
class SymfonyCastDlService
|
class SymfonyCastDlService
|
||||||
{
|
{
|
||||||
@@ -53,25 +52,22 @@ class SymfonyCastDlService
|
|||||||
|
|
||||||
public function videoSize(Chapter $chapter): Chapter
|
public function videoSize(Chapter $chapter): Chapter
|
||||||
{
|
{
|
||||||
try {
|
if (!$chapter->video_size) {
|
||||||
if (!$chapter->video_size) {
|
$response = $this->client->head($chapter->video_link);
|
||||||
$response = $this->client->head($chapter->video_link);
|
if ($response->hasHeader('Content-Length')) {
|
||||||
if ($response->hasHeader('Content-Length')) {
|
$chapter->video_size = (int)$response->getHeader('Content-Length')[0];
|
||||||
$chapter->video_size = $response->getHeader('Content-Length')[0];
|
|
||||||
}
|
|
||||||
$chapter->save();
|
$chapter->save();
|
||||||
}
|
}
|
||||||
} catch (\Exception $exception) {
|
|
||||||
}
|
}
|
||||||
return $chapter;
|
return $chapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function downloadFile(Chapter $chapter): void
|
public function downloadFile(Chapter $chapter): void
|
||||||
{
|
{
|
||||||
if (!is_dir($chapter->directory_path)) {
|
if (!is_dir(public_path($chapter->directory_path))) {
|
||||||
mkdir($chapter->directory_path);
|
mkdir(public_path($chapter->directory_path));
|
||||||
}
|
}
|
||||||
if (!is_file($chapter->video_path)) {
|
if (!$chapter->is_video_file) {
|
||||||
$this->client->request(
|
$this->client->request(
|
||||||
'GET',
|
'GET',
|
||||||
$chapter->video_link,
|
$chapter->video_link,
|
||||||
|
|||||||
@@ -17,11 +17,10 @@ class DownloadVideoFile implements ShouldQueue
|
|||||||
|
|
||||||
public function __construct(private int $chapterId)
|
public function __construct(private int $chapterId)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(SymfonyCastDlService $symfonyCastDlService)
|
public function handle(SymfonyCastDlService $symfonyCastDlService)
|
||||||
{
|
{
|
||||||
$symfonyCastDlService->videoSize(Chapter::find($this->chapterId));
|
$symfonyCastDlService->downloadFile(Chapter::find($this->chapterId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Jobs;
|
namespace App\Jobs;
|
||||||
|
|
||||||
use App\Http\SymfonyCastDl\HtmlParser;
|
|
||||||
use App\Http\SymfonyCastDl\SymfonyCastDlService;
|
use App\Http\SymfonyCastDl\SymfonyCastDlService;
|
||||||
use App\Models\Chapter;
|
use App\Models\Chapter;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @property int $id
|
||||||
* @property int $order
|
* @property int $order
|
||||||
* @property string $link
|
* @property string $link
|
||||||
* @property string $video_link
|
* @property string $video_link
|
||||||
@@ -18,6 +19,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
* @property bool $sync_offline
|
* @property bool $sync_offline
|
||||||
* @property string $video_path
|
* @property string $video_path
|
||||||
* @property string $directory_path
|
* @property string $directory_path
|
||||||
|
* @property Course $course
|
||||||
*/
|
*/
|
||||||
class Chapter extends Model
|
class Chapter extends Model
|
||||||
{
|
{
|
||||||
@@ -34,6 +36,8 @@ class Chapter extends Model
|
|||||||
'sync_offline',
|
'sync_offline',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $appends = ['video_path', 'directory_path'];
|
||||||
|
|
||||||
public function course(): BelongsTo
|
public function course(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Course::class);
|
return $this->belongsTo(Course::class);
|
||||||
@@ -52,4 +56,11 @@ class Chapter extends Model
|
|||||||
get: fn() => $this->course_id . '.' . $this->course->link
|
get: fn() => $this->course_id . '.' . $this->course->link
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function isVideoFile(): Attribute
|
||||||
|
{
|
||||||
|
return Attribute::make(
|
||||||
|
get: fn() => is_file($this->video_path)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Carbon\Traits\Date;
|
use Carbon\Traits\Date;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@@ -16,6 +17,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
|||||||
* @property integer $numberofchapters
|
* @property integer $numberofchapters
|
||||||
* @property integer $timeswatched
|
* @property integer $timeswatched
|
||||||
* @property Date $published_at
|
* @property Date $published_at
|
||||||
|
* @property Chapter[] $chapters
|
||||||
*/
|
*/
|
||||||
class Course extends Model
|
class Course extends Model
|
||||||
{
|
{
|
||||||
@@ -39,4 +41,22 @@ class Course extends Model
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Chapter::class);
|
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;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
<x-layout>
|
<x-layout>
|
||||||
<h1>
|
<h1>
|
||||||
{{ $chapter->course->name }}
|
{{ $chapter->course->name }}
|
||||||
|
<br>
|
||||||
<small class="text-muted">{{ $chapter->title }}</small>
|
<small class="text-muted">{{ $chapter->title }}</small>
|
||||||
</h1>
|
</h1>
|
||||||
<div>
|
<div>
|
||||||
<div class="mx-auto" style="width: 500px;">
|
<div class="mx-auto" style="width: 500px;">
|
||||||
<video class="mx-auto" width="500" controls>
|
@if($chapter->is_video_file)
|
||||||
<source src="/{{ $chapter->video_path }}" type="video/mp4"/>
|
<video class="mx-auto" width="500" controls>
|
||||||
</video>
|
<source src="/{{ $chapter->video_path }}" type="video/mp4"/>
|
||||||
|
</video>
|
||||||
|
@endif
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</x-layout>
|
</x-layout>
|
||||||
|
|||||||
@@ -15,13 +15,16 @@
|
|||||||
@foreach($course->chapters()->get() as $chapter)
|
@foreach($course->chapters()->get() as $chapter)
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href="{{ route('course.chapter', ['chapter' => $chapter->id]) }}">{{ $chapter->title }}</a></td>
|
<td><a href="{{ route('course.chapter', ['chapter' => $chapter->id]) }}">{{ $chapter->title }}</a></td>
|
||||||
<td>{{ $chapter->sync_offline?'Yes':'No' }}</td>
|
|
||||||
<td>{{ $chapter->duration }}</td>
|
<td>{{ $chapter->duration }}</td>
|
||||||
|
<td>{{ $chapter->sync_offline?'Yes':'no' }}</td>
|
||||||
|
<td>{{ $chapter->is_video_file?'Yes':'-' }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
@endforeach
|
@endforeach
|
||||||
<tr>
|
<tr>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td>Total: {{ $course->course_duration }}</td>
|
<td>Total: {{ $course->course_duration }}</td>
|
||||||
</tr>
|
<td></td>
|
||||||
|
<td>Size: {{ $course->total_size_human }}</td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</x-layout>
|
</x-layout>
|
||||||
|
|||||||
Reference in New Issue
Block a user