-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathGaufretteFile.php
More file actions
79 lines (65 loc) · 2.03 KB
/
Copy pathGaufretteFile.php
File metadata and controls
79 lines (65 loc) · 2.03 KB
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
<?php
namespace Craue\FormFlowBundle\Storage;
use Craue\FormFlowBundle\Exception\InvalidTypeException;
use Craue\FormFlowBundle\Util\TempFileUtil;
use Gaufrette\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Representation of a file handled with Gaufrette. Only supports <code>UploadedFile</code> currently.
*
* @author Kevin Cerro <kevincerro1997@gmail.com>
* @copyright 2020 Kevin Cerro
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class GaufretteFile
{
/**
* @var string Name of the file provided by Gaufrette on upload
*/
private $fileName;
private $clientMimeType;
/**
* @param string $filename
* @param $originalFile
*/
public function __construct(string $filename, $originalFile)
{
if (!self::isSupported($originalFile)) {
throw new InvalidTypeException($originalFile, UploadedFile::class);
}
//Filename of uploaded file with Gaufrette
$this->fileName = $filename;
//Keep client original mime type
$this->clientMimeType = $originalFile->getClientMimeType();
}
/**
* @param File $file
* @return mixed The file retrieved from Gaufrette converted to UploadedFile
*/
public function getAsUploadedFile(File $file)
{
$tempDir = sys_get_temp_dir();
// create a temporary file with its original content
$tempFile = tempnam($tempDir, 'craue_form_flow_serialized_file');
file_put_contents($tempFile, $file->getContent());
TempFileUtil::addTempFile($tempFile);
// avoid a deprecation notice regarding "passing a size as 4th argument to the constructor"
// TODO remove as soon as Symfony >= 4.1 is required
if (property_exists(UploadedFile::class, 'size')) {
return new UploadedFile($tempFile, $this->fileName, $this->clientMimeType, null, null, true);
}
return new UploadedFile($tempFile, $this->fileName, $this->clientMimeType, null, true);
}
public function getFileName()
{
return $this->fileName;
}
/**
* @param mixed $file
* @return bool
*/
public static function isSupported($file)
{
return $file instanceof UploadedFile;
}
}