1 <?php namespace BookStack\Uploads;
3 use BookStack\Entities\Models\Page;
5 use BookStack\Traits\HasCreatorAndUpdater;
9 * @property string name
10 * @property string path
11 * @property string extension
12 * @property bool external
14 class Attachment extends Model
16 use HasCreatorAndUpdater;
18 protected $fillable = ['name', 'order'];
21 * Get the downloadable file name for this upload.
22 * @return mixed|string
24 public function getFileName()
26 if (strpos($this->name, '.') !== false) {
29 return $this->name . '.' . $this->extension;
33 * Get the page this file was uploaded to.
34 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
36 public function page()
38 return $this->belongsTo(Page::class, 'uploaded_to');
42 * Get the url of this file.
44 public function getUrl(): string
46 if ($this->external && strpos($this->path, 'http') !== 0) {
49 return url('/attachments/' . $this->id);
53 * Generate a HTML link to this attachment.
55 public function htmlLink(): string
57 return '<a target="_blank" href="'.e($this->getUrl()).'">'.e($this->name).'</a>';
61 * Generate a markdown link to this attachment.
63 public function markdownLink(): string
65 return '['. $this->name .']('. $this->getUrl() .')';