1 <?php namespace BookStack\Uploads;
3 use BookStack\Entities\Models\Page;
5 use BookStack\Traits\HasCreatorAndUpdater;
6 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 * @property string name
11 * @property string path
12 * @property string extension
13 * @property ?Page page
14 * @property bool external
16 class Attachment extends Model
18 use HasCreatorAndUpdater;
20 protected $fillable = ['name', 'order'];
23 * Get the downloadable file name for this upload.
24 * @return mixed|string
26 public function getFileName()
28 if (strpos($this->name, '.') !== false) {
31 return $this->name . '.' . $this->extension;
35 * Get the page this file was uploaded to.
37 public function page(): BelongsTo
39 return $this->belongsTo(Page::class, 'uploaded_to');
43 * Get the url of this file.
45 public function getUrl($openInline = false): string
47 if ($this->external && strpos($this->path, 'http') !== 0) {
50 return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
54 * Generate a HTML link to this attachment.
56 public function htmlLink(): string
58 return '<a target="_blank" href="'.e($this->getUrl()).'">'.e($this->name).'</a>';
62 * Generate a markdown link to this attachment.
64 public function markdownLink(): string
66 return '['. $this->name .']('. $this->getUrl() .')';