Skip to content

Commit

Permalink
Merge pull request #73 from pelmered/main
Browse files Browse the repository at this point in the history
Add option to ignore storage folder structrue in added files
  • Loading branch information
freekmurze committed Jul 6, 2022
2 parents 2a0c6ee + 5a1df56 commit 49db4b7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions config/personal-data-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/
'disk' => 'personal-data-exports',

/*
* If you want to keep the original directory structure for added files,
*/
'keep_directory_structure' => true,

/*
* The amount of days the exports will be available.
*/
Expand Down
17 changes: 13 additions & 4 deletions src/PersonalDataSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ protected function copyLocalFile(string $pathToFile, string $directory = null)
{
$directory = trim($directory, '/');

$fileName = (is_null($directory) ? '' : $directory . '/') . pathinfo($pathToFile, PATHINFO_BASENAME);

$destination = $this->temporaryDirectory->path($fileName);
$destination = $this->getDestinationFilePath(pathinfo($pathToFile, PATHINFO_BASENAME), $directory);

$this->ensureDoesNotOverwriteExistingFile($destination);

Expand All @@ -80,7 +78,7 @@ protected function copyFileFromDisk(string $pathOnDisk, string $diskName, string

$pathOnDirectory = (is_null($directory) ? '' : $directory . '/') . $pathOnDisk;

$pathInTemporaryDirectory = $this->temporaryDirectory->path($pathOnDirectory);
$pathInTemporaryDirectory = $this->getDestinationFilePath($pathOnDisk, $directory);

$this->ensureDoesNotOverwriteExistingFile($pathInTemporaryDirectory);

Expand All @@ -91,6 +89,17 @@ protected function copyFileFromDisk(string $pathOnDisk, string $diskName, string
return $this;
}

protected function getDestinationFilePath($pathOnDisk, $directory)
{
$directory = (is_null($directory) ? '' : $directory . '/');

return config('personal-data-export.keep_directory_structure')
? $this->temporaryDirectory->path($directory . $pathOnDisk)
: $this->temporaryDirectory->path(
$directory.array_slice(explode('/', $pathOnDisk), -1)[0]
);
}

protected function ensureDoesNotOverwriteExistingFile(string $path)
{
if (file_exists($path)) {
Expand Down
34 changes: 34 additions & 0 deletions tests/Tests/PersonalDataSelectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,38 @@ public function it_will_not_allow_an_entry_in_the_personal_data_to_be_overwritte

$this->personalDataSelection->add('test.txt', 'test content');
}


/** @test */
public function it_will_keep_directory_structure()
{
config(['personal-data-export.keep_directory_structure' => true]);

$directory = 'test-directory/';
$filePath = 'subdir/my-file.txt';

$disk = Storage::fake('test-disk');
$disk->put($filePath, 'my content');

$this->personalDataSelection->addFile($filePath, 'test-disk', $directory);
$this->assertFileContents($this->temporaryDirectory->path($directory . $filePath), 'my content');
}


/** @test */
public function it_will_not_keep_directory_structure()
{
config(['personal-data-export.keep_directory_structure' => false]);

$directory = 'test-directory/';
$filePath = 'subdir/my-file.txt';

$disk = Storage::fake('test-disk');
$disk->put($filePath, 'my content3');

$this->personalDataSelection->addFile($filePath, 'test-disk', $directory);
$this->assertFileContents($this->temporaryDirectory->path($directory . 'my-file.txt'), 'my content3');
}


}

0 comments on commit 49db4b7

Please sign in to comment.