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