Skip to content

Commit

Permalink
Merge pull request #24 from lildude/handle-non-image-raw-uploads
Browse files Browse the repository at this point in the history
Handle non image raw uploads and bump version to 2.0.1
  • Loading branch information
lildude committed Oct 19, 2017
2 parents 9d8236d + 2b5b366 commit ab2f05e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
As of 2.0.0, this project adheres to [Semantic Versioning](http://semver.org/) and the format is based on the suggestions a <http://keepachangelog.com/>.

## [2.0.1] - 2017-10-19
### Fixed
- Uploading non-image files to the raw endpoint failed with a `400 Bad Request` error. (Ticket #23)

## [2.0.0] - 2016-10-16
### Added
- A new example showing how to create a gallery and upload an image to it.
Expand Down
5 changes: 2 additions & 3 deletions lib/phpZenfolio/Client.php
Expand Up @@ -14,7 +14,7 @@ class Client
/**
* A few default variables.
*/
const VERSION = '2.0.0';
const VERSION = '2.0.1';
public $AppName = 'Unknown Application';
protected $authToken;
private $keyring;
Expand Down Expand Up @@ -220,7 +220,6 @@ public function login($username, $password, $plaintext = false)
public function upload($photoSet, $file, $args = array())
{
if (is_file($file)) {
$fileinfo = getimagesize($file); // We need this to get the content type.
$fp = fopen($file, 'rb');
$data = fread($fp, filesize($file));
fclose($fp);
Expand All @@ -244,7 +243,7 @@ public function upload($photoSet, $file, $args = array())
$this->client = self::getHttpClient();

// Required headers
$this->request_options['headers']['Content-Type'] = $fileinfo['mime'];
$this->request_options['headers']['Content-Type'] = mime_content_type($file);
$this->request_options['headers']['Content-Length'] = filesize($file);

if (!is_null($this->authToken)) {
Expand Down
14 changes: 13 additions & 1 deletion test/phpZenfolio/Tests/ClientTest.php
Expand Up @@ -348,6 +348,7 @@ public function shouldUploadToPhotoSet()
new Response(200, []), // Upload using photoset object
new Response(200, [], $this->fauxPhotoSetObjectResponse), // LoadPhotoSet() called when using photoset ID for upload
new Response(200, []), // Upload using photoset ID
new Response(200, []), // Upload raw for non-image type using photoset object
new Response(200, []), // Upload using upload URL
]);

Expand Down Expand Up @@ -379,6 +380,13 @@ public function shouldUploadToPhotoSet()
$this->assertArrayHasKey('modified', $request_options['query']);
$this->assertEquals($mod_date, $request_options['query']['modified']);

// Upload raw for non-image type using photoset object
$client->upload(json_decode($this->fauxPhotoSetObjectResponse)->result, './README.md', ['type' => 'raw']);
// Confirm the content type
$request_options = $client->getRequestOptions();
$this->assertArrayHasKey('Content-Type', $request_options['headers']);
$this->assertEquals('text/plain', $request_options['headers']['Content-Type']);

// Upload by photoset URL
$client->upload(json_decode($this->fauxPhotoSetObjectResponse)->result->UploadUrl, './examples/phpZenfolio-logo.png');
$request_options = $client->getRequestOptions();
Expand All @@ -401,8 +409,12 @@ public function shouldUploadToPhotoSet()
$this->assertEquals(json_decode($this->fauxPhotoSetObjectResponse)->result->RawUploadUrl, $url->getScheme().'://'.$url->getHost().$url->getPath());
$this->assertEquals('filename=newfilename.png&modified='.rawurlencode($mod_date), $query);
break;

case 3:
// Raw upload URL for non-photo file
$this->assertEquals(json_decode($this->fauxPhotoSetObjectResponse)->result->RawUploadUrl, $url->getScheme().'://'.$url->getHost().$url->getPath());
$this->assertEquals('filename=README.md', $query);
break;
case 4:
// Upload url
$this->assertEquals(json_decode($this->fauxPhotoSetObjectResponse)->result->UploadUrl, $url->getScheme().'://'.$url->getHost().$url->getPath());
$this->assertEquals('filename=phpZenfolio-logo.png', $query);
Expand Down
5 changes: 4 additions & 1 deletion test/phpZenfolio/Tests/ClientZenfolioTest.php
Expand Up @@ -80,10 +80,13 @@ public function shouldCreateNewPhotoSet($id)
* @test
* @depends shouldCreateNewPhotoSet
*
* Tests adding a photo to a photoset
* Tests adding a photo & non-photo file to a photoset
*/
public function shouldUploadToPhotoSet($photoSetObject)
{
$response = $this->client->upload($photoSetObject, 'README.md', ['type' => 'raw']);
$this->assertTrue(empty($response));

$response = $this->client->upload($photoSetObject, 'examples/phpZenfolio-logo.png');
$this->assertTrue(is_int($response));

Expand Down

0 comments on commit ab2f05e

Please sign in to comment.