mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-03 05:55:16 +01:00
Support external hosted attachments via link.
This commit is contained in:
@@ -128,6 +128,34 @@ abstract class Attachment extends NamedDBElement
|
||||
return file_exists($this->getPath()) || static::isURL($this->getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* The URL to the external file.
|
||||
* Returns null, if the file is not external.
|
||||
* @return string|null
|
||||
*/
|
||||
public function getURL(): ?string
|
||||
{
|
||||
if (!$this->isExternal()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hostname where the external file is stored.
|
||||
* Returns null, if the file is not external.
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHost(): ?string
|
||||
{
|
||||
if (!$this->isExternal()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parse_url($this->getURL(), PHP_URL_HOST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filepath, relative to %BASE%.
|
||||
*
|
||||
@@ -138,6 +166,8 @@ abstract class Attachment extends NamedDBElement
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the filename of the attachment.
|
||||
* For a path like %BASE/path/foo.bar, foo.bar will be returned.
|
||||
|
||||
@@ -95,6 +95,9 @@ class EntityURLGenerator
|
||||
public function viewURL($entity) : string
|
||||
{
|
||||
if ($entity instanceof Attachment) {
|
||||
if ($entity->isExternal()) { //For external attachments, return the link to external path
|
||||
return $entity->getURL();
|
||||
}
|
||||
return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]);
|
||||
}
|
||||
|
||||
@@ -105,6 +108,9 @@ class EntityURLGenerator
|
||||
public function downloadURL($entity) : string
|
||||
{
|
||||
if ($entity instanceof Attachment) {
|
||||
if ($entity->isExternal()) { //For external attachments, return the link to external path
|
||||
return $entity->getURL();
|
||||
}
|
||||
return $this->urlGenerator->generate('attachment_download', ['id' => $entity->getID()]);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,9 +22,17 @@
|
||||
</td>
|
||||
<td class="align-middle">{{ attachment.name }}</td>
|
||||
<td class="align-middle">{{ attachment.type.fullPath }}</td>
|
||||
<td class="align-middle">{{ attachment.filename }}</td>
|
||||
<td class="align-middle">
|
||||
{% if attachment_helper.fileExisting(attachment) %}
|
||||
{% if attachment.external %}
|
||||
<a href="{{ attachment.uRL }}" target="_blank" class="link-external">{{ attachment.host }}</a>
|
||||
{% else %}
|
||||
{{ attachment.filename }}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td class="align-middle">
|
||||
{% if attachment.external %}
|
||||
<i>{% trans %}attachment.external_file{% endtrans %}</i>
|
||||
{% elseif attachment_helper.fileExisting(attachment) %}
|
||||
{{ attachment_helper.humanFileSize(attachment) }}
|
||||
{% else %}
|
||||
<b class="text-danger">{% trans %}attachment.file_not_found{% endtrans %}</b>
|
||||
|
||||
Reference in New Issue
Block a user