Skip to content

Commit

Permalink
Merge pull request #8 from mostafaznv/dev
Browse files Browse the repository at this point in the history
fix: make directory based on output prefix before optimizing files #6
  • Loading branch information
mostafaznv authored Jul 20, 2024
2 parents e286bed + 3e40a25 commit f7db1bd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
17 changes: 14 additions & 3 deletions src/Actions/OptimizePdfAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,21 @@ private function io(string $input, string $output): array
if ($this->file) {
$input = $this->file->getLocalPath();
}

if ($this->outputDisk?->getAdapter()) {
$output = $this->outputDisk->isLocalDisk()
? $this->outputDisk->getAdapter()->path($output)
: $this->outputDisk->getTemporaryDisk()->path($output);
$output = ltrim($output, '/');
$dirname = pathinfo($output, PATHINFO_DIRNAME);

$disk = $this->outputDisk->isLocalDisk()
? $this->outputDisk->getAdapter()
: $this->outputDisk->getTemporaryDisk();


if ($dirname != '.' and !$disk->directoryExists($dirname)) {
$disk->makeDirectory($dirname);
}

$output = $disk->path($output);
}

$this->logger->info("Input: $input", [
Expand Down
28 changes: 17 additions & 11 deletions tests/Unit/Laravel/Concerns/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,26 @@
});

it('will delete temporary directory on __destruct', function () {
$disk = 's3';
$path = 'path/to/file.pdf';
$localPath = '';

Storage::fake($disk);
Storage::disk($disk)->put(
$path,
UploadedFile::fake()->create('file.pdf', 1000, 'application/pdf')
);
function t(): void
{
$disk = 's3';
$path = 'path/to/file.pdf';

$file = File::make($path, $disk);
$localPath = $file->getLocalPath();
Storage::fake($disk);
Storage::disk($disk)->put(
$path,
UploadedFile::fake()->create('file.pdf', 1000, 'application/pdf')
);

expect(file_exists($localPath))->toBeTrue();
$file = File::make($path, $disk);
$localPath = $file->getLocalPath();

expect(file_exists($localPath))->toBeTrue();
}

t();

unset($file);
expect(file_exists($localPath))->toBeFalse();
});
21 changes: 14 additions & 7 deletions tests/Unit/OptimizePdfActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,33 @@
->not->toContain($this->input);
});

it('will store files to remote disks', function () {
it('will store files to remote disks', function (string $prefix) {
$disk = Disk::make('s3');
$output = $prefix . $this->output;

expect($disk->getAdapter()->allFiles())->toHaveCount(0);

$result = OptimizePdfAction::init(outputDisk: $disk)
->logger($this->logger)
->execute(
$this->command, pdf(), $this->output
$this->command, pdf(), $output
);

$diskFiles = $disk->getAdapter()->allFiles();
$output = ltrim($output, '/');

expect($result->status)
->toBeTrue()
->and($diskFiles)
->toHaveCount(1)
->toContain($this->output);
});
->toContain($output);
})->with([
'', 'prefix/', '/prefix/', 'prefix1/prefix2/'
]);

it('will cleanup temp files after finishing process', function () {
it('will cleanup temp files after finishing process', function (string $prefix) {
$disk = 's3';
$output = $prefix . $this->output;
Storage::disk($disk)->put($this->input, file_get_contents(pdf()));

$file = File::make($this->input, $disk);
Expand All @@ -205,7 +210,7 @@
$result = OptimizePdfAction::init($file, $outputDisk)
->logger($this->logger)
->execute(
$this->command, pdf(), $this->output
$this->command, pdf(), $output
);

$allFiles = Storage::disk($disk)->allFiles();
Expand All @@ -221,7 +226,9 @@
->toHaveCount(0)
->and($outputDiskAllTempFiles)
->toHaveCount(0);
});
})->with([
'', 'prefix/', '/prefix/', 'prefix1/prefix2/'
]);

it('will log output for successful actions', function () {
$result = $this->action->execute(
Expand Down

0 comments on commit f7db1bd

Please sign in to comment.