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