Skip to content

Commit

Permalink
Merge pull request #3133 from ushahidi/1618-ush-023
Browse files Browse the repository at this point in the history
Fix server error while uploading photos
  • Loading branch information
rjmackay authored Jul 5, 2018
2 parents 75aed3f + ffcb9ce commit 56cc531
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Core/Exception/ValidatorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ValidatorException extends \InvalidArgumentException
{
private $errors;

public function __construct($message, array $errors, Exception $previous = null)
public function __construct($message, array $errors, \Exception $previous = null)
{
$flatErrors = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($errors)), false);

Expand Down
28 changes: 22 additions & 6 deletions src/Core/Tool/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public function upload(UploadData $file, $filename = null)
$filename = uniqid() . '-' . $file->name;
}

// Avoid any possible issues with case sensitivity by forcing all files
// to be made lowercase.
$filename = strtolower($filename);
// Avoid possible issues with case sensitivity by forcing all files
// to be made lowercase. Also replace possibly invalid characters in filename
$filename = strtolower(preg_replace('/[^\pL\pN\-\_\s\.]+/u', '', $filename));


// Add the first and second letters of filename to the directory path
// to help segment the files, producing a more reasonable amount of
Expand All @@ -66,9 +67,24 @@ public function upload(UploadData $file, $filename = null)
$extension = pathinfo($filepath, PATHINFO_EXTENSION);
$mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain';
$config = ['mimetype' => $mimeType];
$this->fs->putStream($filepath, $stream, $config);
if (is_resource($stream)) {
fclose($stream);

try {
$this->fs->putStream($filepath, $stream, $config);
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
// Flysystem and FlysystemRackspace are very leaky abstractions
// so we have to manually catch guzzle errors here
$response = $e->getResponse();

throw new \InvalidArgumentException('Could not upload file: '. $response->getBody(true));
} catch (\Guzzle\Http\Exception\BadResponseException $e) {
// Flysystem and FlysystemRackspace are very leaky abstractions
// so we have to manually catch guzzle errors here

throw new \InvalidArgumentException('Could not upload file');
} finally {
if (is_resource($stream)) {
fclose($stream);
}
}

// Get meta information about the file.
Expand Down
16 changes: 11 additions & 5 deletions src/Core/Usecase/Media/CreateMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Ushahidi\Core\Tool\Uploader;
use Ushahidi\Core\Tool\UploadData;
use Ushahidi\Core\Usecase\CreateUsecase;
use Ushahidi\Exception\ValidatorException;
use Ushahidi\Core\Exception\ValidatorException;

class CreateMedia extends CreateUsecase
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public function interact()
} catch (ValidatorException $e) {
// If a file was uploaded, it must be purged after a failed upload.
// Otherwise storage will be filled with junk files.
if ($this->fs->has($this->upload->file)) {
if ($this->upload && $this->fs->has($this->upload->file)) {
$this->fs->delete($this->upload->file);
}

Expand All @@ -79,9 +79,15 @@ public function interact()
protected function getEntity()
{
// Upload the file and get the file reference
$this->upload = $this->uploader->upload(
new UploadData($this->getPayload('file'))
);
try {
$this->upload = $this->uploader->upload(
new UploadData($this->getPayload('file'))
);
} catch (\InvalidArgumentException $e) {
throw new ValidatorException($e->getMessage(), [
'file' => $e->getMessage()
], $e);
}

$payload = [
'caption' => $this->getPayload('caption', false) ?: null,
Expand Down

0 comments on commit 56cc531

Please sign in to comment.