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