3 namespace BookStack\Exports;
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;
11 use Illuminate\Database\Eloquent\Factories\HasFactory;
12 use Illuminate\Database\Eloquent\Model;
13 use Illuminate\Database\Eloquent\Relations\BelongsTo;
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
27 class Import extends Model implements Loggable
31 public function getSizeString(): string
33 $mb = round($this->size / 1000000, 2);
38 * Get the URL to view/continue this import.
40 public function getUrl(string $path = ''): string
42 $path = ltrim($path, '/');
43 return url("/import/{$this->id}" . ($path ? '/' . $path : ''));
46 public function logDescriptor(): string
48 return "({$this->id}) {$this->name}";
51 public function createdBy(): BelongsTo
53 return $this->belongsTo(User::class, 'created_by');
56 public function decodeMetadata(): ZipExportBook|ZipExportChapter|ZipExportPage|null
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),