-
Notifications
You must be signed in to change notification settings - Fork 177
/
GaufretteFile.php
105 lines (91 loc) · 3.42 KB
/
GaufretteFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Oneup\UploaderBundle\Uploader\File;
use Gaufrette\Adapter\AwsS3;
use Gaufrette\Adapter\StreamFactory;
use Gaufrette\File;
use Gaufrette\FilesystemInterface;
class GaufretteFile extends File implements FileInterface
{
/**
* @var string
*/
protected $mimeType;
public function __construct(File $file, FilesystemInterface $filesystem, protected ?string $streamWrapperPrefix = null)
{
parent::__construct($file->getKey(), $filesystem);
}
/**
* Returns the size of the file.
*
* !! WARNING !!
* Calling this loads the entire file into memory,
* unless it is on a stream-capable filesystem.
* In case of bigger files this could throw exceptions,
* and will have heavy performance footprint.
* !! ------- !!
*/
public function getSize(): int|false
{
// This can only work on streamable files, so basically local files,
// still only perform it once even on local files to avoid bothering the filesystem.php g
if ($this->filesystem->getAdapter() instanceof StreamFactory && !$this->size) {
if ($this->streamWrapperPrefix) {
try {
$this->setSize((int) filesize($this->streamWrapperPrefix . $this->getKey()));
} catch (\Exception $e) {
// Fail gracefully if there was a problem with opening the file and
// let gaufrette load the file into memory allowing it to throw exceptions
// if that doesn't work either.
// Not empty to make the scrutiziner happy.
return parent::getSize();
}
}
}
return parent::getSize();
}
public function getPathname(): string
{
return $this->getKey();
}
public function getPath(): string
{
return pathinfo($this->getKey(), \PATHINFO_DIRNAME);
}
public function getBasename(): string
{
return pathinfo($this->getKey(), \PATHINFO_BASENAME);
}
public function getMimeType(): string
{
// This can only work on streamable files, so basically local files,
// still only perform it once even on local files to avoid bothering the filesystem.
if ($this->filesystem->getAdapter() instanceof StreamFactory && !$this->mimeType) {
if ($this->streamWrapperPrefix) {
/** @var resource $finfo */
$finfo = finfo_open(\FILEINFO_MIME_TYPE);
$this->mimeType = (string) finfo_file($finfo, $this->streamWrapperPrefix . $this->getKey());
finfo_close($finfo);
}
} elseif ($this->filesystem->getAdapter() instanceof AwsS3 && !$this->mimeType) {
$metadata = $this->filesystem->getAdapter()->getMetadata($this->getBasename());
if (isset($metadata['ContentType'])) {
$this->mimeType = $metadata['ContentType'];
}
}
return $this->mimeType;
}
/**
* Now that we may be able to get the mime-type the extension
* COULD be guessed based on that, but it would be even less
* accurate as mime-types can have multiple extensions.
*/
public function getExtension(): string
{
return pathinfo($this->getKey(), \PATHINFO_EXTENSION);
}
public function getFilesystem(): FilesystemInterface
{
return $this->filesystem;
}
}