mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Implement new attachment property in its getters/setters.
This commit is contained in:
parent
ba4bf4b613
commit
97cb91a3b2
2 changed files with 49 additions and 4 deletions
|
@ -48,7 +48,6 @@ use Doctrine\ORM\Mapping as ORM;
|
||||||
*/
|
*/
|
||||||
abstract class Attachment extends NamedDBElement
|
abstract class Attachment extends NamedDBElement
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of file extensions, that browsers can show directly as image.
|
* A list of file extensions, that browsers can show directly as image.
|
||||||
* Based on: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
|
* Based on: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
|
||||||
|
@ -57,6 +56,11 @@ abstract class Attachment extends NamedDBElement
|
||||||
const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
|
const PICTURE_EXTS = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png',
|
||||||
'svg', 'webp'];
|
'svg', 'webp'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When the path begins with one of this placeholders
|
||||||
|
*/
|
||||||
|
const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%', '%FOOTPRINTS%', '%FOOTPRINTS3D%'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
* @ORM\Column(type="boolean")
|
* @ORM\Column(type="boolean")
|
||||||
|
@ -117,9 +121,14 @@ abstract class Attachment extends NamedDBElement
|
||||||
*/
|
*/
|
||||||
public function isExternal() : bool
|
public function isExternal() : bool
|
||||||
{
|
{
|
||||||
//Treat all pathes without a filepath as external
|
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placholder via explode
|
||||||
return (strpos($this->getPath(), '%MEDIA%') !== 0)
|
$tmp = explode("/", $this->path);
|
||||||
&& (strpos($this->getPath(), '%BASE%') !== 0);
|
|
||||||
|
if (empty($tmp)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !in_array($tmp[0], static::INTERNAL_PLACEHOLDER, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
|
@ -139,6 +148,11 @@ abstract class Attachment extends NamedDBElement
|
||||||
if ($this->isExternal()) {
|
if ($this->isExternal()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($this->original_filename)) {
|
||||||
|
return strtolower(pathinfo($this->original_filename, PATHINFO_EXTENSION));
|
||||||
|
}
|
||||||
|
|
||||||
return strtolower(pathinfo($this->getPath(), PATHINFO_EXTENSION));
|
return strtolower(pathinfo($this->getPath(), PATHINFO_EXTENSION));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,9 +220,25 @@ abstract class Attachment extends NamedDBElement
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//If we have a stored original filename, then use it
|
||||||
|
if (!empty($this->original_filename)) {
|
||||||
|
return $this->original_filename;
|
||||||
|
}
|
||||||
|
|
||||||
return pathinfo($this->getPath(), PATHINFO_BASENAME);
|
return pathinfo($this->getPath(), PATHINFO_BASENAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the filename that is shown for this attachment. Useful when the internal path is some generated value.
|
||||||
|
* @param string|null $new_filename The filename that should be shown.
|
||||||
|
* Set to null to generate the filename from path.
|
||||||
|
* @return Attachment
|
||||||
|
*/
|
||||||
|
public function setFilename(?string $new_filename): Attachment
|
||||||
|
{
|
||||||
|
$this->original_filename = $new_filename;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the show_in_table attribute.
|
* Get the show_in_table attribute.
|
||||||
*
|
*
|
||||||
|
|
|
@ -48,6 +48,12 @@ class AttachmentTest extends TestCase
|
||||||
$this->setProtectedProperty($attachment, 'path', '%BASE%/foo/bar.jpg');
|
$this->setProtectedProperty($attachment, 'path', '%BASE%/foo/bar.jpg');
|
||||||
$this->assertFalse($attachment->isExternal());
|
$this->assertFalse($attachment->isExternal());
|
||||||
|
|
||||||
|
$this->setProtectedProperty($attachment, 'path', '%FOOTPRINTS%/foo/bar.jpg');
|
||||||
|
$this->assertFalse($attachment->isExternal());
|
||||||
|
|
||||||
|
$this->setProtectedProperty($attachment, 'path', '%FOOTPRINTS3D%/foo/bar.jpg');
|
||||||
|
$this->assertFalse($attachment->isExternal());
|
||||||
|
|
||||||
//Every other string is not a external attachment
|
//Every other string is not a external attachment
|
||||||
$this->setProtectedProperty($attachment, 'path', '%test%/foo/bar.ghp');
|
$this->setProtectedProperty($attachment, 'path', '%test%/foo/bar.ghp');
|
||||||
$this->assertTrue($attachment->isExternal());
|
$this->assertTrue($attachment->isExternal());
|
||||||
|
@ -68,6 +74,11 @@ class AttachmentTest extends TestCase
|
||||||
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg');
|
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg');
|
||||||
$this->assertEquals('jpeg', $attachment->getExtension());
|
$this->assertEquals('jpeg', $attachment->getExtension());
|
||||||
|
|
||||||
|
//Test if we can override the filename
|
||||||
|
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg');
|
||||||
|
$this->setProtectedProperty($attachment, 'original_filename', 'test.txt');
|
||||||
|
$this->assertEquals('txt', $attachment->getExtension());
|
||||||
|
|
||||||
$this->setProtectedProperty($attachment, 'path', 'https://foo.bar');
|
$this->setProtectedProperty($attachment, 'path', 'https://foo.bar');
|
||||||
$this->assertNull( $attachment->getExtension());
|
$this->assertNull( $attachment->getExtension());
|
||||||
|
|
||||||
|
@ -110,6 +121,10 @@ class AttachmentTest extends TestCase
|
||||||
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt');
|
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt');
|
||||||
$this->assertEquals('bar.txt', $attachment->getFilename());
|
$this->assertEquals('bar.txt', $attachment->getFilename());
|
||||||
|
|
||||||
|
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg');
|
||||||
|
$this->setProtectedProperty($attachment, 'original_filename', 'test.txt');
|
||||||
|
$this->assertEquals('test.txt', $attachment->getFilename());
|
||||||
|
|
||||||
$this->setProtectedProperty($attachment, 'path', 'https://www.google.de/test.txt');
|
$this->setProtectedProperty($attachment, 'path', 'https://www.google.de/test.txt');
|
||||||
$this->assertNull($attachment->getFilename());
|
$this->assertNull($attachment->getFilename());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue