blob: a5afaac16b50d5b8b5cede1021dc88f415b5d4ea (
plain) (
blame)
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
|
<?php
namespace Drupal\file\Upload;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
use Symfony\Component\HttpFoundation\File\Exception\NoFileException;
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
/**
* Writes files from a input stream to a temporary file.
*/
class InputStreamFileWriter implements InputStreamFileWriterInterface {
/**
* Creates a new InputStreamFileUploader.
*/
public function __construct(
protected FileSystemInterface $fileSystem,
) {}
/**
* {@inheritdoc}
*/
public function writeStreamToFile(string $stream = self::DEFAULT_STREAM, int $bytesToRead = self::DEFAULT_BYTES_TO_READ): string {
// 'rb' is needed so reading works correctly on Windows environments too.
$fileData = fopen($stream, 'rb');
$tempFilePath = $this->fileSystem->tempnam('temporary://', 'file');
$tempFile = fopen($tempFilePath, 'wb');
if ($tempFile) {
while (!feof($fileData)) {
$read = fread($fileData, $bytesToRead);
if ($read === FALSE) {
// Close the file streams.
fclose($tempFile);
fclose($fileData);
throw new UploadException('Input file data could not be read');
}
if (fwrite($tempFile, $read) === FALSE) {
// Close the file streams.
fclose($tempFile);
fclose($fileData);
throw new CannotWriteFileException(sprintf('Temporary file data for "%s" could not be written', $tempFilePath));
}
}
// Close the temp file stream.
fclose($tempFile);
}
else {
// Close the input file stream since we can't proceed with the upload.
// Don't try to close $tempFile since it's FALSE at this point.
fclose($fileData);
throw new NoFileException(sprintf('Temporary file "%s" could not be opened for file upload', $tempFilePath));
}
// Close the input stream.
fclose($fileData);
return $tempFilePath;
}
}
|