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