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