]> BookStack Code Mirror - bookstack/blob - app/Uploads/Attachment.php
Merge branch 'codemirror6' into codemirror6_take2
[bookstack] / app / Uploads / Attachment.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\Auth\Permissions\JointPermission;
6 use BookStack\Auth\Permissions\PermissionApplicator;
7 use BookStack\Auth\User;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Model;
11 use BookStack\Traits\HasCreatorAndUpdater;
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      * @return mixed|string
45      */
46     public function getFileName()
47     {
48         if (strpos($this->name, '.') !== false) {
49             return $this->name;
50         }
51
52         return $this->name . '.' . $this->extension;
53     }
54
55     /**
56      * Get the page this file was uploaded to.
57      */
58     public function page(): BelongsTo
59     {
60         return $this->belongsTo(Page::class, 'uploaded_to');
61     }
62
63     public function jointPermissions(): HasMany
64     {
65         return $this->hasMany(JointPermission::class, 'entity_id', 'uploaded_to')
66             ->where('joint_permissions.entity_type', '=', 'page');
67     }
68
69     /**
70      * Get the url of this file.
71      */
72     public function getUrl($openInline = false): string
73     {
74         if ($this->external && strpos($this->path, 'http') !== 0) {
75             return $this->path;
76         }
77
78         return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
79     }
80
81     /**
82      * Generate a HTML link to this attachment.
83      */
84     public function htmlLink(): string
85     {
86         return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
87     }
88
89     /**
90      * Generate a markdown link to this attachment.
91      */
92     public function markdownLink(): string
93     {
94         return '[' . $this->name . '](' . $this->getUrl() . ')';
95     }
96
97     /**
98      * Scope the query to those attachments that are visible based upon related page permissions.
99      */
100     public function scopeVisible(): Builder
101     {
102         $permissions = app()->make(PermissionApplicator::class);
103
104         return $permissions->restrictPageRelationQuery(
105             self::query(),
106             'attachments',
107             'uploaded_to'
108         );
109     }
110 }