mirror of
https://github.com/filegator/filegator.git
synced 2025-05-31 11:10:20 +02:00
New config param: overwrite_on_upload
This commit is contained in:
parent
0765958729
commit
8330fa86bc
@ -2,6 +2,8 @@
|
||||
|
||||
## Upcoming...
|
||||
|
||||
* New config param: overwrite files on upload
|
||||
|
||||
## 7.3.3 - 2020-03-08
|
||||
|
||||
* Download filename bugfix
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -3,6 +3,7 @@
|
||||
return [
|
||||
'public_path' => APP_PUBLIC_PATH,
|
||||
'public_dir' => APP_PUBLIC_DIR,
|
||||
'overwrite_on_upload' => false,
|
||||
|
||||
'frontend_config' => [
|
||||
'app_name' => 'FileGator',
|
||||
|
@ -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');
|
||||
|
@ -3,6 +3,7 @@
|
||||
return [
|
||||
'public_path' => '',
|
||||
'public_dir' => __DIR__.'/../../dist',
|
||||
'overwrite_on_upload' => false,
|
||||
|
||||
'frontend_config' => [
|
||||
'app_name' => 'FileGator',
|
||||
|
Loading…
x
Reference in New Issue
Block a user