3 namespace BookStack\Uploads;
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Permissions\Models\JointPermission;
9 use BookStack\Permissions\PermissionApplicator;
10 use BookStack\Users\Models\HasCreatorAndUpdater;
11 use BookStack\Users\Models\OwnableInterface;
12 use BookStack\Users\Models\User;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Database\Eloquent\Factories\HasFactory;
15 use Illuminate\Database\Eloquent\Relations\BelongsTo;
16 use Illuminate\Database\Eloquent\Relations\HasMany;
20 * @property string $name
21 * @property string $path
22 * @property string $extension
23 * @property ?Page $page
24 * @property bool $external
25 * @property int $uploaded_to
26 * @property User $updatedBy
27 * @property User $createdBy
29 * @method static Entity|Builder visible()
31 class Attachment extends Model implements OwnableInterface
33 use HasCreatorAndUpdater;
36 protected $fillable = ['name', 'order'];
37 protected $hidden = ['path', 'page'];
43 * Get the downloadable file name for this upload.
45 public function getFileName(): string
47 if (str_contains($this->name, '.')) {
51 return $this->name . '.' . $this->extension;
55 * Get the page this file was uploaded to.
57 public function page(): BelongsTo
59 return $this->belongsTo(Page::class, 'uploaded_to');
62 public function jointPermissions(): HasMany
64 return $this->hasMany(JointPermission::class, 'entity_id', 'uploaded_to')
65 ->where('joint_permissions.entity_type', '=', 'page');
69 * Get the url of this file.
71 public function getUrl($openInline = false): string
73 if ($this->external && !str_starts_with($this->path, 'http')) {
77 return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
81 * Get the representation of this attachment in a format suitable for the page editors.
82 * Detects and adapts video content to use an inline video embed.
84 public function editorContent(): array
86 $videoExtensions = ['mp4', 'webm', 'mkv', 'ogg', 'avi'];
87 if (in_array(strtolower($this->extension), $videoExtensions)) {
88 $html = '<video src="' . e($this->getUrl(true)) . '" controls width="480" height="270"></video>';
89 return ['text/html' => $html, 'text/plain' => $html];
92 return ['text/html' => $this->htmlLink(), 'text/plain' => $this->markdownLink()];
96 * Generate the HTML link to this attachment.
98 public function htmlLink(): string
100 return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
104 * Generate a MarkDown link to this attachment.
106 public function markdownLink(): string
108 return '[' . $this->name . '](' . $this->getUrl() . ')';
112 * Scope the query to those attachments that are visible based upon related page permissions.
114 public function scopeVisible(): Builder
116 $permissions = app()->make(PermissionApplicator::class);
118 return $permissions->restrictPageRelationQuery(