3 namespace BookStack\Uploads;
5 use BookStack\Entities\Models\Page;
7 use BookStack\Traits\HasCreatorAndUpdater;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
12 * @property string name
13 * @property string path
14 * @property string extension
15 * @property ?Page page
16 * @property bool external
18 class Attachment extends Model
20 use HasCreatorAndUpdater;
22 protected $fillable = ['name', 'order'];
25 * Get the downloadable file name for this upload.
27 * @return mixed|string
29 public function getFileName()
31 if (strpos($this->name, '.') !== false) {
35 return $this->name . '.' . $this->extension;
39 * Get the page this file was uploaded to.
41 public function page(): BelongsTo
43 return $this->belongsTo(Page::class, 'uploaded_to');
47 * Get the url of this file.
49 public function getUrl($openInline = false): string
51 if ($this->external && strpos($this->path, 'http') !== 0) {
55 return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
59 * Generate a HTML link to this attachment.
61 public function htmlLink(): string
63 return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
67 * Generate a markdown link to this attachment.
69 public function markdownLink(): string
71 return '[' . $this->name . '](' . $this->getUrl() . ')';