]> BookStack Code Mirror - bookstack/blob - app/Exports/Import.php
ZIP Imports: Fleshed out continue page, Added testing
[bookstack] / app / Exports / Import.php
1 <?php
2
3 namespace BookStack\Exports;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Users\Models\User;
7 use Carbon\Carbon;
8 use Illuminate\Database\Eloquent\Factories\HasFactory;
9 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11
12 /**
13  * @property int $id
14  * @property string $path
15  * @property string $name
16  * @property int $size - ZIP size in bytes
17  * @property int $book_count
18  * @property int $chapter_count
19  * @property int $page_count
20  * @property int $created_by
21  * @property Carbon $created_at
22  * @property Carbon $updated_at
23  * @property User $createdBy
24  */
25 class Import extends Model implements Loggable
26 {
27     use HasFactory;
28
29     public const TYPE_BOOK = 'book';
30     public const TYPE_CHAPTER = 'chapter';
31     public const TYPE_PAGE = 'page';
32
33     /**
34      * Get the type (model) that this import is intended to be.
35      */
36     public function getType(): string
37     {
38         if ($this->book_count === 1) {
39             return self::TYPE_BOOK;
40         } elseif ($this->chapter_count === 1) {
41             return self::TYPE_CHAPTER;
42         }
43
44         return self::TYPE_PAGE;
45     }
46
47     public function getSizeString(): string
48     {
49         $mb = round($this->size / 1000000, 2);
50         return "{$mb} MB";
51     }
52
53     /**
54      * Get the URL to view/continue this import.
55      */
56     public function getUrl(string $path = ''): string
57     {
58         $path = ltrim($path, '/');
59         return url("/import/{$this->id}" . ($path ? '/' . $path : ''));
60     }
61
62     public function logDescriptor(): string
63     {
64         return "({$this->id}) {$this->name}";
65     }
66
67     public function createdBy(): BelongsTo
68     {
69         return $this->belongsTo(User::class, 'created_by');
70     }
71 }