]> BookStack Code Mirror - bookstack/blob - app/Uploads/Attachment.php
Increased attachment link limit from 192 to 2k
[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     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      * Generate a HTML link to this attachment.
81      */
82     public function htmlLink(): string
83     {
84         return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
85     }
86
87     /**
88      * Generate a markdown link to this attachment.
89      */
90     public function markdownLink(): string
91     {
92         return '[' . $this->name . '](' . $this->getUrl() . ')';
93     }
94
95     /**
96      * Scope the query to those attachments that are visible based upon related page permissions.
97      */
98     public function scopeVisible(): Builder
99     {
100         $permissions = app()->make(PermissionApplicator::class);
101
102         return $permissions->restrictPageRelationQuery(
103             self::query(),
104             'attachments',
105             'uploaded_to'
106         );
107     }
108 }