]> BookStack Code Mirror - bookstack/blob - app/Exports/Import.php
CommentDisplayTest correct namespace
[bookstack] / app / Exports / Import.php
1 <?php
2
3 namespace BookStack\Exports;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Exports\ZipExports\Models\ZipExportBook;
7 use BookStack\Exports\ZipExports\Models\ZipExportChapter;
8 use BookStack\Exports\ZipExports\Models\ZipExportPage;
9 use BookStack\Users\Models\User;
10 use Carbon\Carbon;
11 use Illuminate\Database\Eloquent\Factories\HasFactory;
12 use Illuminate\Database\Eloquent\Model;
13 use Illuminate\Database\Eloquent\Relations\BelongsTo;
14
15 /**
16  * @property int $id
17  * @property string $path
18  * @property string $name
19  * @property int $size - ZIP size in bytes
20  * @property string $type
21  * @property string $metadata
22  * @property int $created_by
23  * @property Carbon $created_at
24  * @property Carbon $updated_at
25  * @property User $createdBy
26  */
27 class Import extends Model implements Loggable
28 {
29     use HasFactory;
30
31     public function getSizeString(): string
32     {
33         $mb = round($this->size / 1000000, 2);
34         return "{$mb} MB";
35     }
36
37     /**
38      * Get the URL to view/continue this import.
39      */
40     public function getUrl(string $path = ''): string
41     {
42         $path = ltrim($path, '/');
43         return url("/import/{$this->id}" . ($path ? '/' . $path : ''));
44     }
45
46     public function logDescriptor(): string
47     {
48         return "({$this->id}) {$this->name}";
49     }
50
51     public function createdBy(): BelongsTo
52     {
53         return $this->belongsTo(User::class, 'created_by');
54     }
55
56     public function decodeMetadata(): ZipExportBook|ZipExportChapter|ZipExportPage|null
57     {
58         $metadataArray = json_decode($this->metadata, true);
59         return match ($this->type) {
60             'book' => ZipExportBook::fromArray($metadataArray),
61             'chapter' => ZipExportChapter::fromArray($metadataArray),
62             'page' => ZipExportPage::fromArray($metadataArray),
63             default => null,
64         };
65     }
66 }