Skip to content

Commit 99dfe15

Browse files
committed
Handle windows file names in the S3RenameUpload. Fixes #16
1 parent 009bbd1 commit 99dfe15

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/Aws/Filter/File/S3RenameUpload.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ protected function getFinalTarget($uploadData)
9090
throw new MissingBucketException('No bucket was set when trying to upload a file to S3');
9191
}
9292

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

tests/Aws/Tests/Filter/File/S3RenameUploadTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,30 @@ public function testThrowExceptionIfNoBucketIsSet()
6060
$this->filter->filter(array('tmp_name' => 'foo'));
6161
}
6262

63-
public function testAssertS3UriIsGenerated()
63+
/**
64+
* @dataProvider tmpNameProvider
65+
*/
66+
public function testAssertS3UriIsGenerated($tmpName, $expectedKey)
6467
{
6568
$reflMethod = new ReflectionMethod($this->filter, 'getFinalTarget');
6669
$reflMethod->setAccessible(true);
6770

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

7073
$result = $reflMethod->invoke($this->filter, array(
71-
'tmp_name' => 'temp/phptmpname'
74+
'tmp_name' => $tmpName
7275
));
7376

74-
$this->assertEquals('s3://my-bucket/temp/phptmpname', $result);
77+
$this->assertEquals("s3://my-bucket/{$expectedKey}", $result);
78+
}
79+
80+
public function tmpNameProvider()
81+
{
82+
return array(
83+
array('temp/phptmpname', 'temp/phptmpname'),
84+
array('temp/phptmpname/', 'temp/phptmpname'),
85+
array('temp\\phptmpname', 'temp/phptmpname'),
86+
array('temp\\phptmpname\\', 'temp/phptmpname'),
87+
);
7588
}
7689
}

0 commit comments

Comments
 (0)