Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/Storage/Device/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,34 @@ public function uploadData(string $data, string $path, string $contentType, int
private function joinChunks(string $path, int $chunks)
{
$tmp = \dirname($path).DIRECTORY_SEPARATOR.'tmp_'.\basename($path).DIRECTORY_SEPARATOR.\basename($path).'_chunks.log';

$dest = \fopen($path, 'wb');
if ($dest === false) {
throw new Exception('Failed to open destination file '.$path);
}

for ($i = 1; $i <= $chunks; $i++) {
$part = dirname($tmp).DIRECTORY_SEPARATOR.pathinfo($path, PATHINFO_FILENAME).'.part.'.$i;
$data = file_get_contents($part);
if (! $data) {
$src = \fopen($part, 'rb');
if ($src === false) {
\fclose($dest);
throw new Exception('Failed to read chunk '.$part);
}

if (! file_put_contents($path, $data, FILE_APPEND)) {
if (\stream_copy_to_stream($src, $dest) === false) {
\fclose($src);
\fclose($dest);
throw new Exception('Failed to append chunk '.$part);
}
\fclose($src);
\unlink($part);
}

\fclose($dest);
\unlink($tmp);
\rmdir(dirname($tmp));
if (! \rmdir(\dirname($tmp))) {
\trigger_error('Failed to remove temporary chunk directory '.dirname($tmp).' (chunk log: '.$tmp.')', E_USER_WARNING);
}
}

/**
Expand Down