Skip to content

Commit

Permalink
Handle windows file names in the S3RenameUpload. Fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremeamia committed Jan 8, 2014
1 parent 009bbd1 commit 99dfe15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Aws/Filter/File/S3RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ protected function getFinalTarget($uploadData)
throw new MissingBucketException('No bucket was set when trying to upload a file to S3');
}

$target = parent::getFinalTarget($uploadData);
return sprintf('s3://%s/%s', $this->options['bucket'], trim($target, '/'));
// Get the tmp file name and convert it to an S3 key
$key = trim(str_replace('\\', '/', parent::getFinalTarget($uploadData)), '/');
if (strpos($key, './') === 0) {
$key = substr($key, 2);
}

return "s3://{$this->options['bucket']}/{$key}";
}
}
19 changes: 16 additions & 3 deletions tests/Aws/Tests/Filter/File/S3RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,30 @@ public function testThrowExceptionIfNoBucketIsSet()
$this->filter->filter(array('tmp_name' => 'foo'));
}

public function testAssertS3UriIsGenerated()
/**
* @dataProvider tmpNameProvider
*/
public function testAssertS3UriIsGenerated($tmpName, $expectedKey)
{
$reflMethod = new ReflectionMethod($this->filter, 'getFinalTarget');
$reflMethod->setAccessible(true);

$this->filter->setBucket('my-bucket');

$result = $reflMethod->invoke($this->filter, array(
'tmp_name' => 'temp/phptmpname'
'tmp_name' => $tmpName
));

$this->assertEquals('s3://my-bucket/temp/phptmpname', $result);
$this->assertEquals("s3://my-bucket/{$expectedKey}", $result);
}

public function tmpNameProvider()
{
return array(
array('temp/phptmpname', 'temp/phptmpname'),
array('temp/phptmpname/', 'temp/phptmpname'),
array('temp\\phptmpname', 'temp/phptmpname'),
array('temp\\phptmpname\\', 'temp/phptmpname'),
);
}
}

0 comments on commit 99dfe15

Please sign in to comment.