Fix: merging chunks after upload

This commit is contained in:
Milos Stojanovic
2019-08-01 10:05:42 +02:00
parent a2517c6d9b
commit 49dc0b89a1
4 changed files with 27 additions and 4 deletions

View File

@@ -99,7 +99,7 @@ class UploadController
if ($chunks_size >= $total_size) { if ($chunks_size >= $total_size) {
for ($i = 1; $i <= $total_chunks; ++$i) { for ($i = 1; $i <= $total_chunks; ++$i) {
$part = $this->tmpfs->readStream($prefix.$file_name.'.part'.$i); $part = $this->tmpfs->readStream($prefix.$file_name.'.part'.$i);
$this->tmpfs->write($file_name, $part['stream']); $this->tmpfs->write($file_name, $part['stream'], true);
} }
$final = $this->tmpfs->readStream($file_name); $final = $this->tmpfs->readStream($file_name);

View File

@@ -30,11 +30,17 @@ class Tmpfs implements Service, TmpfsInterface
} }
} }
public function write(string $filename, $data) public function write(string $filename, $data, $append = false)
{ {
$filename = $this->sanitizeFilename($filename); $filename = $this->sanitizeFilename($filename);
file_put_contents($this->getPath().$filename, $data); $flags = 0;
if ($append) {
$flags = FILE_APPEND;
}
file_put_contents($this->getPath().$filename, $data, $flags);
} }
public function getFileLocation(string $filename): string public function getFileLocation(string $filename): string

View File

@@ -16,7 +16,7 @@ interface TmpfsInterface
public function findAll($pattern): array; public function findAll($pattern): array;
public function write(string $filename, $data); public function write(string $filename, $data, $append);
public function read(string $filename): string; public function read(string $filename): string;

View File

@@ -137,6 +137,23 @@ class UploadTest extends TestCase
$this->sendRequest('POST', '/upload', $data, $files); $this->sendRequest('POST', '/upload', $data, $files);
$this->assertOk(); $this->assertOk();
$this->sendRequest('POST', '/getdir', [
'dir' => '/',
]);
$this->assertResponseJsonHas([
'data' => [
'files' => [
0 => [
'type' => 'file',
'name' => 'sample.txt',
'path' => '/sample.txt',
'size' => 1572864,
],
],
],
]);
} }
public function testUploadFileBiggerThanAllowed() public function testUploadFileBiggerThanAllowed()