3 namespace BookStack\Uploads;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\Page;
10 use BookStack\Traits\HasCreatorAndUpdater;
11 use Illuminate\Database\Eloquent\Builder;
12 use Illuminate\Database\Eloquent\Relations\BelongsTo;
16 * @property string $name
17 * @property string $path
18 * @property string $extension
19 * @property ?Page $page
20 * @property bool $external
21 * @property int $uploaded_to
22 * @property User $updatedBy
23 * @property User $createdBy
25 * @method static Entity|Builder visible()
27 class Attachment extends Model
29 use HasCreatorAndUpdater;
31 protected $fillable = ['name', 'order'];
32 protected $hidden = ['path', 'page'];
38 * Get the downloadable file name for this upload.
40 * @return mixed|string
42 public function getFileName()
44 if (strpos($this->name, '.') !== false) {
48 return $this->name . '.' . $this->extension;
52 * Get the page this file was uploaded to.
54 public function page(): BelongsTo
56 return $this->belongsTo(Page::class, 'uploaded_to');
60 * Get the url of this file.
62 public function getUrl($openInline = false): string
64 if ($this->external && strpos($this->path, 'http') !== 0) {
68 return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
72 * Generate a HTML link to this attachment.
74 public function htmlLink(): string
76 return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
80 * Generate a markdown link to this attachment.
82 public function markdownLink(): string
84 return '[' . $this->name . '](' . $this->getUrl() . ')';
88 * Scope the query to those attachments that are visible based upon related page permissions.
90 public function scopeVisible(): Builder
92 $permissionService = app()->make(PermissionService::class);
94 return $permissionService->filterRelatedEntity(