mirror of
https://github.com/filegator/filegator.git
synced 2025-08-01 23:20:35 +02:00
New config param: overwrite_on_upload
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## Upcoming...
|
## Upcoming...
|
||||||
|
|
||||||
|
* New config param: overwrite files on upload
|
||||||
|
|
||||||
## 7.3.3 - 2020-03-08
|
## 7.3.3 - 2020-03-08
|
||||||
|
|
||||||
* Download filename bugfix
|
* Download filename bugfix
|
||||||
|
@@ -182,8 +182,8 @@ class FileController
|
|||||||
fwrite($stream, $content);
|
fwrite($stream, $content);
|
||||||
rewind($stream);
|
rewind($stream);
|
||||||
|
|
||||||
$res = $this->storage->deleteFile($path.$this->separator.$name);
|
$this->storage->deleteFile($path.$this->separator.$name);
|
||||||
$res = $this->storage->store($path, $name, $stream);
|
$this->storage->store($path, $name, $stream);
|
||||||
|
|
||||||
if (is_resource($stream)) {
|
if (is_resource($stream)) {
|
||||||
fclose($stream);
|
fclose($stream);
|
||||||
|
@@ -65,6 +65,8 @@ class UploadController
|
|||||||
|
|
||||||
$file = $request->files->get('file');
|
$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')) {
|
if (! $file || ! $file->isValid() || $file->getSize() > $this->config->get('frontend_config.upload_max_size')) {
|
||||||
return $response->json('Bad file', 422);
|
return $response->json('Bad file', 422);
|
||||||
}
|
}
|
||||||
@@ -103,7 +105,7 @@ class UploadController
|
|||||||
}
|
}
|
||||||
|
|
||||||
$final = $this->tmpfs->readStream($file_name);
|
$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
|
// cleanup
|
||||||
$this->tmpfs->remove($file_name);
|
$this->tmpfs->remove($file_name);
|
||||||
|
@@ -161,12 +161,16 @@ class Filesystem implements Service
|
|||||||
return $this->storage->rename($from, $to);
|
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);
|
$destination = $this->joinPaths($this->applyPathPrefix($path), $name);
|
||||||
|
|
||||||
while ($this->storage->has($destination)) {
|
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);
|
return $this->storage->putStream($destination, $resource);
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
return [
|
return [
|
||||||
'public_path' => APP_PUBLIC_PATH,
|
'public_path' => APP_PUBLIC_PATH,
|
||||||
'public_dir' => APP_PUBLIC_DIR,
|
'public_dir' => APP_PUBLIC_DIR,
|
||||||
|
'overwrite_on_upload' => false,
|
||||||
|
|
||||||
'frontend_config' => [
|
'frontend_config' => [
|
||||||
'app_name' => 'FileGator',
|
'app_name' => 'FileGator',
|
||||||
|
@@ -10,10 +10,10 @@
|
|||||||
|
|
||||||
namespace Tests\Unit;
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Filegator\Services\Storage\Filesystem;
|
use Filegator\Services\Storage\Filesystem;
|
||||||
use League\Flysystem\Adapter\Local;
|
use League\Flysystem\Adapter\Local;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@@ -265,6 +265,33 @@ class FilesystemTest extends TestCase
|
|||||||
$this->assertEquals('croissant', stream_get_contents($ret['stream']));
|
$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()
|
public function testCreatingFileWithTheSameNameUpcountsFilenameRecursively()
|
||||||
{
|
{
|
||||||
$this->storage->createFile('/', 'test.txt');
|
$this->storage->createFile('/', 'test.txt');
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
return [
|
return [
|
||||||
'public_path' => '',
|
'public_path' => '',
|
||||||
'public_dir' => __DIR__.'/../../dist',
|
'public_dir' => __DIR__.'/../../dist',
|
||||||
|
'overwrite_on_upload' => false,
|
||||||
|
|
||||||
'frontend_config' => [
|
'frontend_config' => [
|
||||||
'app_name' => 'FileGator',
|
'app_name' => 'FileGator',
|
||||||
|
Reference in New Issue
Block a user