]> BookStack Code Mirror - bookstack/blob - app/Uploads/Attachment.php
Vectors: Added command to regenerate for all
[bookstack] / app / Uploads / Attachment.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Permissions\Models\JointPermission;
9 use BookStack\Permissions\PermissionApplicator;
10 use BookStack\Users\Models\HasCreatorAndUpdater;
11 use BookStack\Users\Models\User;
12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Database\Eloquent\Factories\HasFactory;
14 use Illuminate\Database\Eloquent\Relations\BelongsTo;
15 use Illuminate\Database\Eloquent\Relations\HasMany;
16
17 /**
18  * @property int    $id
19  * @property string $name
20  * @property string $path
21  * @property string $extension
22  * @property ?Page  $page
23  * @property bool   $external
24  * @property int    $uploaded_to
25  * @property User   $updatedBy
26  * @property User   $createdBy
27  *
28  * @method static Entity|Builder visible()
29  */
30 class Attachment extends Model
31 {
32     use HasCreatorAndUpdater;
33     use HasFactory;
34
35     protected $fillable = ['name', 'order'];
36     protected $hidden = ['path', 'page'];
37     protected $casts = [
38         'external' => 'bool',
39     ];
40
41     /**
42      * Get the downloadable file name for this upload.
43      */
44     public function getFileName(): string
45     {
46         if (str_contains($this->name, '.')) {
47             return $this->name;
48         }
49
50         return $this->name . '.' . $this->extension;
51     }
52
53     /**
54      * Get the page this file was uploaded to.
55      */
56     public function page(): BelongsTo
57     {
58         return $this->belongsTo(Page::class, 'uploaded_to');
59     }
60
61     public function jointPermissions(): HasMany
62     {
63         return $this->hasMany(JointPermission::class, 'entity_id', 'uploaded_to')
64             ->where('joint_permissions.entity_type', '=', 'page');
65     }
66
67     /**
68      * Get the url of this file.
69      */
70     public function getUrl($openInline = false): string
71     {
72         if ($this->external && !str_starts_with($this->path, 'http')) {
73             return $this->path;
74         }
75
76         return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
77     }
78
79     /**
80      * Get the representation of this attachment in a format suitable for the page editors.
81      * Detects and adapts video content to use an inline video embed.
82      */
83     public function editorContent(): array
84     {
85         $videoExtensions = ['mp4', 'webm', 'mkv', 'ogg', 'avi'];
86         if (in_array(strtolower($this->extension), $videoExtensions)) {
87             $html = '<video src="' . e($this->getUrl(true)) . '" controls width="480" height="270"></video>';
88             return ['text/html' => $html, 'text/plain' => $html];
89         }
90
91         return ['text/html' => $this->htmlLink(), 'text/plain' => $this->markdownLink()];
92     }
93
94     /**
95      * Generate the HTML link to this attachment.
96      */
97     public function htmlLink(): string
98     {
99         return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
100     }
101
102     /**
103      * Generate a MarkDown link to this attachment.
104      */
105     public function markdownLink(): string
106     {
107         return '[' . $this->name . '](' . $this->getUrl() . ')';
108     }
109
110     /**
111      * Scope the query to those attachments that are visible based upon related page permissions.
112      */
113     public function scopeVisible(): Builder
114     {
115         $permissions = app()->make(PermissionApplicator::class);
116
117         return $permissions->restrictPageRelationQuery(
118             self::query(),
119             'attachments',
120             'uploaded_to'
121         );
122     }
123 }