2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Uploads;
|
2018-09-25 19:30:50 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\App\Model;
|
2021-10-19 00:46:55 +08:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2020-11-22 08:17:45 +08:00
|
|
|
use BookStack\Entities\Models\Page;
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Permissions\Models\JointPermission;
|
|
|
|
use BookStack\Permissions\PermissionApplicator;
|
|
|
|
use BookStack\Users\Models\HasCreatorAndUpdater;
|
|
|
|
use BookStack\Users\Models\User;
|
2021-10-19 00:46:55 +08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-02-08 22:39:13 +08:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-06-13 21:16:09 +08:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2023-01-25 03:04:32 +08:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2016-10-10 01:58:22 +08:00
|
|
|
|
2020-09-14 01:29:48 +08:00
|
|
|
/**
|
2021-10-20 17:49:45 +08:00
|
|
|
* @property int $id
|
2021-10-19 00:46:55 +08:00
|
|
|
* @property string $name
|
|
|
|
* @property string $path
|
|
|
|
* @property string $extension
|
2021-10-20 17:49:45 +08:00
|
|
|
* @property ?Page $page
|
|
|
|
* @property bool $external
|
|
|
|
* @property int $uploaded_to
|
|
|
|
* @property User $updatedBy
|
|
|
|
* @property User $createdBy
|
2021-10-19 00:46:55 +08:00
|
|
|
*
|
|
|
|
* @method static Entity|Builder visible()
|
2020-09-14 01:29:48 +08:00
|
|
|
*/
|
2020-12-31 02:25:35 +08:00
|
|
|
class Attachment extends Model
|
2016-10-10 01:58:22 +08:00
|
|
|
{
|
2020-12-31 02:25:35 +08:00
|
|
|
use HasCreatorAndUpdater;
|
2023-02-08 22:39:13 +08:00
|
|
|
use HasFactory;
|
2020-12-31 02:25:35 +08:00
|
|
|
|
2016-10-10 01:58:22 +08:00
|
|
|
protected $fillable = ['name', 'order'];
|
2021-10-20 17:43:03 +08:00
|
|
|
protected $hidden = ['path', 'page'];
|
2021-10-20 07:58:56 +08:00
|
|
|
protected $casts = [
|
|
|
|
'external' => 'bool',
|
|
|
|
];
|
2016-10-10 01:58:22 +08:00
|
|
|
|
2016-10-23 20:36:45 +08:00
|
|
|
/**
|
|
|
|
* Get the downloadable file name for this upload.
|
|
|
|
*/
|
2023-02-20 21:05:23 +08:00
|
|
|
public function getFileName(): string
|
2016-10-23 20:36:45 +08:00
|
|
|
{
|
2023-02-20 21:05:23 +08:00
|
|
|
if (str_contains($this->name, '.')) {
|
2018-01-29 00:58:52 +08:00
|
|
|
return $this->name;
|
|
|
|
}
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2016-10-23 20:36:45 +08:00
|
|
|
return $this->name . '.' . $this->extension;
|
|
|
|
}
|
|
|
|
|
2016-10-10 01:58:22 +08:00
|
|
|
/**
|
|
|
|
* Get the page this file was uploaded to.
|
|
|
|
*/
|
2021-06-13 21:16:09 +08:00
|
|
|
public function page(): BelongsTo
|
2016-10-10 01:58:22 +08:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Page::class, 'uploaded_to');
|
|
|
|
}
|
|
|
|
|
2023-01-25 03:04:32 +08:00
|
|
|
public function jointPermissions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(JointPermission::class, 'entity_id', 'uploaded_to')
|
|
|
|
->where('joint_permissions.entity_type', '=', 'page');
|
|
|
|
}
|
|
|
|
|
2016-10-11 03:30:27 +08:00
|
|
|
/**
|
|
|
|
* Get the url of this file.
|
|
|
|
*/
|
2021-06-06 20:55:56 +08:00
|
|
|
public function getUrl($openInline = false): string
|
2016-10-11 03:30:27 +08:00
|
|
|
{
|
2023-02-20 21:05:23 +08:00
|
|
|
if ($this->external && !str_starts_with($this->path, 'http')) {
|
2018-05-20 18:06:10 +08:00
|
|
|
return $this->path;
|
|
|
|
}
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2021-06-06 20:55:56 +08:00
|
|
|
return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
|
2016-10-11 03:30:27 +08:00
|
|
|
}
|
2020-07-28 17:45:28 +08:00
|
|
|
|
2020-09-14 01:29:48 +08:00
|
|
|
/**
|
2024-01-15 19:50:05 +08:00
|
|
|
* Get the representation of this attachment in a format suitable for the page editors.
|
|
|
|
* Detects and adapts video content to use an inline video embed.
|
|
|
|
*/
|
|
|
|
public function editorContent(): array
|
|
|
|
{
|
|
|
|
$videoExtensions = ['mp4', 'webm', 'mkv', 'ogg', 'avi'];
|
|
|
|
if (in_array(strtolower($this->extension), $videoExtensions)) {
|
|
|
|
$html = '<video src="' . e($this->getUrl(true)) . '" controls width="480" height="270"></video>';
|
|
|
|
return ['text/html' => $html, 'text/plain' => $html];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['text/html' => $this->htmlLink(), 'text/plain' => $this->markdownLink()];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the HTML link to this attachment.
|
2020-09-14 01:29:48 +08:00
|
|
|
*/
|
2020-07-28 17:45:28 +08:00
|
|
|
public function htmlLink(): string
|
|
|
|
{
|
2021-06-26 23:23:15 +08:00
|
|
|
return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
|
2020-07-28 17:45:28 +08:00
|
|
|
}
|
|
|
|
|
2020-09-14 01:29:48 +08:00
|
|
|
/**
|
2024-01-15 19:50:05 +08:00
|
|
|
* Generate a MarkDown link to this attachment.
|
2020-09-14 01:29:48 +08:00
|
|
|
*/
|
2020-07-28 17:45:28 +08:00
|
|
|
public function markdownLink(): string
|
|
|
|
{
|
2021-06-26 23:23:15 +08:00
|
|
|
return '[' . $this->name . '](' . $this->getUrl() . ')';
|
2020-07-28 17:45:28 +08:00
|
|
|
}
|
2021-10-19 00:46:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope the query to those attachments that are visible based upon related page permissions.
|
|
|
|
*/
|
2021-10-20 07:58:56 +08:00
|
|
|
public function scopeVisible(): Builder
|
2021-10-19 00:46:55 +08:00
|
|
|
{
|
2022-07-13 03:15:41 +08:00
|
|
|
$permissions = app()->make(PermissionApplicator::class);
|
2021-10-20 17:49:45 +08:00
|
|
|
|
2022-07-17 02:54:25 +08:00
|
|
|
return $permissions->restrictPageRelationQuery(
|
2021-10-27 05:04:18 +08:00
|
|
|
self::query(),
|
2021-10-19 00:46:55 +08:00
|
|
|
'attachments',
|
|
|
|
'uploaded_to'
|
|
|
|
);
|
|
|
|
}
|
2016-10-10 01:58:22 +08:00
|
|
|
}
|