New config param: overwrite_on_upload

This commit is contained in:
Milos Stojanovic 2020-03-23 13:37:54 +01:00
parent 0765958729
commit 8330fa86bc
7 changed files with 43 additions and 6 deletions

View File

@ -2,6 +2,8 @@
## Upcoming...
* New config param: overwrite files on upload
## 7.3.3 - 2020-03-08
* Download filename bugfix

View File

@ -182,8 +182,8 @@ class FileController
fwrite($stream, $content);
rewind($stream);
$res = $this->storage->deleteFile($path.$this->separator.$name);
$res = $this->storage->store($path, $name, $stream);
$this->storage->deleteFile($path.$this->separator.$name);
$this->storage->store($path, $name, $stream);
if (is_resource($stream)) {
fclose($stream);

View File

@ -65,6 +65,8 @@ class UploadController
$file = $request->files->get('file');
$overwrite_on_upload = (bool) $this->config->get('overwrite_on_upload', false);
if (! $file || ! $file->isValid() || $file->getSize() > $this->config->get('frontend_config.upload_max_size')) {
return $response->json('Bad file', 422);
}
@ -103,7 +105,7 @@ class UploadController
}
$final = $this->tmpfs->readStream($file_name);
$res = $this->storage->store($destination, $final['filename'], $final['stream']);
$res = $this->storage->store($destination, $final['filename'], $final['stream'], $overwrite_on_upload);
// cleanup
$this->tmpfs->remove($file_name);

View File

@ -161,12 +161,16 @@ class Filesystem implements Service
return $this->storage->rename($from, $to);
}
public function store(string $path, string $name, $resource): bool
public function store(string $path, string $name, $resource, bool $overwrite = false): bool
{
$destination = $this->joinPaths($this->applyPathPrefix($path), $name);
while ($this->storage->has($destination)) {
$destination = $this->upcountName($destination);
if ($overwrite) {
$this->deleteFile($destination);
} else {
$destination = $this->upcountName($destination);
}
}
return $this->storage->putStream($destination, $resource);

View File

@ -3,6 +3,7 @@
return [
'public_path' => APP_PUBLIC_PATH,
'public_dir' => APP_PUBLIC_DIR,
'overwrite_on_upload' => false,
'frontend_config' => [
'app_name' => 'FileGator',

View File

@ -10,10 +10,10 @@
namespace Tests\Unit;
use Exception;
use Filegator\Services\Storage\Filesystem;
use League\Flysystem\Adapter\Local;
use Tests\TestCase;
use Exception;
/**
* @internal
@ -265,6 +265,33 @@ class FilesystemTest extends TestCase
$this->assertEquals('croissant', stream_get_contents($ret['stream']));
}
public function testStoringFileWithTheSameNameOverwritesOriginalFile()
{
// create dummy file
$string = 'lorem ipsum';
$resource = fopen('data://text/plain;base64,'.base64_encode($string), 'r');
// and store it
$this->storage->store('/', 'singletone.txt', $resource);
fclose($resource);
// first file contains lorem ipsum
$ret = $this->storage->readStream('singletone.txt');
$this->assertEquals('lorem ipsum', stream_get_contents($ret['stream']));
// create another dummy file
$string = 'croissant';
$resource = fopen('data://text/plain;base64,'.base64_encode($string), 'r');
// and store it with the same name
$this->storage->store('/', 'singletone.txt', $resource, true);
fclose($resource);
// first file is overwritten
$ret = $this->storage->readStream('singletone.txt');
$this->assertEquals('croissant', stream_get_contents($ret['stream']));
}
public function testCreatingFileWithTheSameNameUpcountsFilenameRecursively()
{
$this->storage->createFile('/', 'test.txt');

View File

@ -3,6 +3,7 @@
return [
'public_path' => '',
'public_dir' => __DIR__.'/../../dist',
'overwrite_on_upload' => false,
'frontend_config' => [
'app_name' => 'FileGator',