Skip to content

Commit

Permalink
Optionally keep original filename (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrydallacroce authored Aug 18, 2020
1 parent 9e79d55 commit fceb4f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/FileFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function __construct(string $identifier, $storage, array $config = nul
'total_bytes' => 0,
'total_bytes_copied' => 0,
'temporary' => false,
'keep_original_filename' => $config['keep_original_filename'] ?? false,
'destination' => $config['filePath'],
'temporary_directory' => $config['temporaryDirectory'],
];
Expand Down Expand Up @@ -86,7 +87,7 @@ private static function getDefaultProcessors()
$processors = [];
$processors[Local::class] = new Local();
$processors[Remote::class] = new Remote();
$processors[LastResort::class] = new LastResort();
$processors[LastResort::class] = new LastResort();
return $processors;
}

Expand Down
42 changes: 27 additions & 15 deletions src/TemporaryFilePathFromUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,40 @@

trait TemporaryFilePathFromUrl
{
private function getTemporaryFilePath(array $state)

/**
* Get temporary file path, depending on flag keep_original_filename value.
*
* @param array $state
* State.
*
* @return string
* Temporary file path.
*/
private function getTemporaryFilePath(array $state): string
{
if ($state['keep_original_filename']) {
return $this->getTemporaryFileOriginalName($state);
} else {
return $this->getTemporaryFile($state);
}
}

private function getTemporaryFileOriginalName(array $state): string
{
$file_name = basename($state['source']);
return "{$state['temporary_directory']}/{$file_name}";
}

private function getTemporaryFile(array $state): string
{
$info = parse_url($state['source']);
$file_name = "";
if (isset($info["host"])) {
$file_name .= str_replace(".", "_", $info["host"]);
}
$file_name .= str_replace("/", "_", $info['path']);
return $this->getTemporaryFile($file_name, $state);
}

/**
* Generate a tmp filepath for a given $uuid.
*
* @param string $uuid
* UUID.
*
* @return string
*/
private function getTemporaryFile(string $filename, array $state): string
{
return $state['temporary_directory'] . '/' . $this->sanitizeString($filename);
return $state['temporary_directory'] . '/' . $this->sanitizeString($file_name);
}

private function sanitizeString(string $string): string
Expand Down
23 changes: 23 additions & 0 deletions test/FileFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use FileFetcher\FileFetcher;
use FileFetcher\Processor\LastResort;
use FileFetcher\Processor\Local;
use FileFetcher\Processor\Remote;
use PHPUnit\Framework\TestCase;
use Procrastinator\Result;

Expand Down Expand Up @@ -36,6 +37,26 @@ public function testRemote()
$this->assertTrue($data->temporary);
}

public function testKeepOriginalFilename()
{
$fetcher = FileFetcher::get(
"2",
new Memory(),
[
"filePath" => "http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv",
"processors" => [Remote::class],
"keep_original_filename" => true,
]
);

$result = $fetcher->run();

$data = json_decode($result->getData());
$filepath = "/tmp/Sacramentorealestatetransactions.csv";
$this->assertEquals($filepath, $data->destination);
$this->assertTrue($data->temporary);
}

public function testLocal()
{
$local_file = __DIR__ . "/files/tiny.csv";
Expand All @@ -55,6 +76,7 @@ public function testLocal()
$result = $fetcher->run();
$data = json_decode($result->getData());
$this->assertEquals($local_file, $data->destination);
$this->assertFalse($fetcher->getStateProperty('keep_original_filename'));
$this->assertFalse($data->temporary);
}

Expand Down Expand Up @@ -140,6 +162,7 @@ public function tearDown(): void
parent::tearDown();
$files = [
"/tmp/samplecsvs_s3_amazonaws_com_sacramentorealestatetransactions.csv",
"/tmp/Sacramentorealestatetransactions.csv",
"/tmp/dkan_default_content_files_s3_amazonaws_com_{$this->sampleCsvSize}_mb_sample.csv",
"/tmp/data_medicare_gov_api_views_42wc_33ci_rows.csv",
];
Expand Down
2 changes: 1 addition & 1 deletion test/files/tiny.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"hello", "friend"
"good bye", "enemy"
"goodbye", "enemy"

0 comments on commit fceb4f6

Please sign in to comment.