2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Uploads;
|
2018-09-25 13:30:50 +02:00
|
|
|
|
2021-10-18 18:46:55 +02:00
|
|
|
use BookStack\Auth\Permissions\PermissionService;
|
2021-10-20 01:58:56 +02:00
|
|
|
use BookStack\Auth\User;
|
2021-10-18 18:46:55 +02:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2020-11-22 01:17:45 +01:00
|
|
|
use BookStack\Entities\Models\Page;
|
2020-12-30 19:25:35 +01:00
|
|
|
use BookStack\Model;
|
|
|
|
use BookStack\Traits\HasCreatorAndUpdater;
|
2021-10-18 18:46:55 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-06-13 15:16:09 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2016-10-09 19:58:22 +02:00
|
|
|
|
2020-09-13 19:29:48 +02:00
|
|
|
/**
|
2021-10-20 11:49:45 +02:00
|
|
|
* @property int $id
|
2021-10-18 18:46:55 +02:00
|
|
|
* @property string $name
|
|
|
|
* @property string $path
|
|
|
|
* @property string $extension
|
2021-10-20 11:49:45 +02:00
|
|
|
* @property ?Page $page
|
|
|
|
* @property bool $external
|
|
|
|
* @property int $uploaded_to
|
|
|
|
* @property User $updatedBy
|
|
|
|
* @property User $createdBy
|
2021-10-18 18:46:55 +02:00
|
|
|
*
|
|
|
|
* @method static Entity|Builder visible()
|
2020-09-13 19:29:48 +02:00
|
|
|
*/
|
2020-12-30 19:25:35 +01:00
|
|
|
class Attachment extends Model
|
2016-10-09 19:58:22 +02:00
|
|
|
{
|
2020-12-30 19:25:35 +01:00
|
|
|
use HasCreatorAndUpdater;
|
|
|
|
|
2016-10-09 19:58:22 +02:00
|
|
|
protected $fillable = ['name', 'order'];
|
2021-10-20 11:43:03 +02:00
|
|
|
protected $hidden = ['path', 'page'];
|
2021-10-20 01:58:56 +02:00
|
|
|
protected $casts = [
|
|
|
|
'external' => 'bool',
|
|
|
|
];
|
2016-10-09 19:58:22 +02:00
|
|
|
|
2016-10-23 14:36:45 +02:00
|
|
|
/**
|
|
|
|
* Get the downloadable file name for this upload.
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2016-10-23 14:36:45 +02:00
|
|
|
* @return mixed|string
|
|
|
|
*/
|
|
|
|
public function getFileName()
|
|
|
|
{
|
2019-09-14 00:58:40 +02:00
|
|
|
if (strpos($this->name, '.') !== false) {
|
2018-01-28 17:58:52 +01:00
|
|
|
return $this->name;
|
|
|
|
}
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2016-10-23 14:36:45 +02:00
|
|
|
return $this->name . '.' . $this->extension;
|
|
|
|
}
|
|
|
|
|
2016-10-09 19:58:22 +02:00
|
|
|
/**
|
|
|
|
* Get the page this file was uploaded to.
|
|
|
|
*/
|
2021-06-13 15:16:09 +02:00
|
|
|
public function page(): BelongsTo
|
2016-10-09 19:58:22 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Page::class, 'uploaded_to');
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:30:27 +02:00
|
|
|
/**
|
|
|
|
* Get the url of this file.
|
|
|
|
*/
|
2021-06-06 14:55:56 +02:00
|
|
|
public function getUrl($openInline = false): string
|
2016-10-10 21:30:27 +02:00
|
|
|
{
|
2018-05-20 12:06:10 +02:00
|
|
|
if ($this->external && strpos($this->path, 'http') !== 0) {
|
|
|
|
return $this->path;
|
|
|
|
}
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2021-06-06 14:55:56 +02:00
|
|
|
return url('/attachments/' . $this->id . ($openInline ? '?open=true' : ''));
|
2016-10-10 21:30:27 +02:00
|
|
|
}
|
2020-07-28 11:45:28 +02:00
|
|
|
|
2020-09-13 19:29:48 +02:00
|
|
|
/**
|
|
|
|
* Generate a HTML link to this attachment.
|
|
|
|
*/
|
2020-07-28 11:45:28 +02:00
|
|
|
public function htmlLink(): string
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
return '<a target="_blank" href="' . e($this->getUrl()) . '">' . e($this->name) . '</a>';
|
2020-07-28 11:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 19:29:48 +02:00
|
|
|
/**
|
|
|
|
* Generate a markdown link to this attachment.
|
|
|
|
*/
|
2020-07-28 11:45:28 +02:00
|
|
|
public function markdownLink(): string
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
return '[' . $this->name . '](' . $this->getUrl() . ')';
|
2020-07-28 11:45:28 +02:00
|
|
|
}
|
2021-10-18 18:46:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope the query to those attachments that are visible based upon related page permissions.
|
|
|
|
*/
|
2021-10-20 01:58:56 +02:00
|
|
|
public function scopeVisible(): Builder
|
2021-10-18 18:46:55 +02:00
|
|
|
{
|
|
|
|
$permissionService = app()->make(PermissionService::class);
|
2021-10-20 11:49:45 +02:00
|
|
|
|
2021-10-18 18:46:55 +02:00
|
|
|
return $permissionService->filterRelatedEntity(
|
|
|
|
Page::class,
|
2021-10-26 23:04:18 +02:00
|
|
|
self::query(),
|
2021-10-18 18:46:55 +02:00
|
|
|
'attachments',
|
|
|
|
'uploaded_to'
|
|
|
|
);
|
|
|
|
}
|
2016-10-09 19:58:22 +02:00
|
|
|
}
|