]> BookStack Code Mirror - bookstack/blob - app/Uploads/Attachment.php
Allow to use DB tables prefix
[bookstack] / app / Uploads / Attachment.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Model;
7 use BookStack\Traits\HasCreatorAndUpdater;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
10 /**
11  * @property int id
12  * @property string name
13  * @property string path
14  * @property string extension
15  * @property ?Page page
16  * @property bool external
17  */
18 class Attachment extends Model
19 {
20     use HasCreatorAndUpdater;
21
22     protected $fillable = ['name', 'order'];
23
24     /**
25      * Get the downloadable file name for this upload.
26      *
27      * @return mixed|string
28      */
29     public function getFileName()
30     {
31         if (strpos($this->name, '.') !== false) {
32             return $this->name;
33         }
34
35         return $this->name . '.' . $this->extension;
36     }
37
38     /**
39      * Get the page this file was uploaded to.
40      */
41     public function page(): BelongsTo
42     {
43         return $this->belongsTo(Page::class, 'uploaded_to');
44     }
45
46     /**
47      * Get the url of this file.
48      */
49     public function getUrl($openInline = false): string
50     {
51         if ($this->external && strpos($this->path, 'http') !== 0) {
52             return $this->path;
53         }
54
55         return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
56     }
57
58     /**
59      * Generate a HTML link to this attachment.
60      */
61     public function htmlLink(): string
62     {
63         return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
64     }
65
66     /**
67      * Generate a markdown link to this attachment.
68      */
69     public function markdownLink(): string
70     {
71         return '[' . $this->name . '](' . $this->getUrl() . ')';
72     }
73 }