Skip to content

Commit

Permalink
Return image link with extension (#128)
Browse files Browse the repository at this point in the history
* Return image link with extension

return image by `/<id>` and `/<id>.jpg` links

* update readme
  • Loading branch information
talyguryn authored Jul 25, 2018
1 parent d74eca8 commit 6e4c4a3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ Each response will have at least `success` and `message` fields.
"success": true,
"message": "Image uploaded",
"id": "69256e83-66e1-449a-b0c2-5414d332e3a6",
"url": "https://capella.pics/69256e83-66e1-449a-b0c2-5414d332e3a6",
"url": "https:\/\/capella.pics\/69256e83-66e1-449a-b0c2-5414d332e3a6.jpg",
"mime": "image\/jpg",
"width": 1080,
"height": 700,
"color": "#9d908d",
Expand Down Expand Up @@ -124,7 +125,7 @@ Each response will have at least `success` and `message` fields.
| `Source is damaged` | Source has no data, size or mime-type |
| `Can't get headers for this URL` | Wrong url was passed |

### Example
### Examples

#### CURL

Expand Down Expand Up @@ -176,9 +177,9 @@ print(response)

## Get image

You can get each uploaded image by the following URL scheme.
You can get each uploaded image by the following URL scheme with or without extension.

`https://capella.pics/<image_id>`
`https://capella.pics/<image_id>` or `https://capella.pics/<image_id>.jpg`

[![](https://capella.pics/69256e83-66e1-449a-b0c2-5414d332e3a6)](https://capella.pics/69256e83-66e1-449a-b0c2-5414d332e3a6)

Expand Down
2 changes: 1 addition & 1 deletion capella/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codex.capella",
"version": "2.0.1",
"version": "2.1.0",
"description": "Cloud service for image storage and delivery. Upload files and accept image-filters on the fly with the simple API.",
"scripts": {
"build": "webpack --mode development --progress --display-error-details --display-entrypoints --display-reasons --watch",
Expand Down
5 changes: 5 additions & 0 deletions capella/src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function __construct($uri, $filterList)
$this->pathParts = explode('/', $this->path);
$this->id = $this->pathParts[0];

/**
* Find image id if image name was passed
*/
$this->id = \Methods::imageNameToId($this->id);

/** Slices filters and additional parameters list from pathParts */
$this->rawFilters = array_slice($this->pathParts, 1);

Expand Down
25 changes: 24 additions & 1 deletion capella/src/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,30 @@ public static function getImageUri($id)
{
$domain = self::getDomainAndProtocol();

return $domain . "/" . $id;
return $domain . "/" . $id . "." . Uploader::TARGET_EXT;
}

/**
* Get image's id from name
*
* 52df7fbf-ff1d-44e7-803a-e9f04d03d542.jpg
* -> 52df7fbf-ff1d-44e7-803a-e9f04d03d542
*
* @param string $name
* @return string
*/
public static function imageNameToId($name)
{
$defaultExtension = Uploader::TARGET_EXT;

/**
* Allow getting images with extension at the end of uri
*/
if (preg_match('/(?P<id>[\w-]+)\.'.$defaultExtension.'$/', $name, $matches)) {
return $matches['id'];
}

return $name;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions capella/src/controller/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use API;
use HTTP;
use Uploader;

/**
* Class for processing uploading form or AJAX upload
Expand Down Expand Up @@ -94,15 +95,18 @@ protected function uploadLink()
/**
* Return success result with image link
*
* @param string $imageData
* @param array $imageData
*/
protected function returnImageData($imageData)
{
HTTP\Response::OK();

API\Response::success([
'message' => 'Image uploaded',
'id' => basename($imageData['link']),
/**
* Get ID as name without extension
*/
'id' => basename($imageData['link'], '.' . Uploader::TARGET_EXT),
'url' => $imageData['link'],
'mime' => $imageData['mime'],
'width' => $imageData['width'],
Expand Down
2 changes: 2 additions & 0 deletions capella/src/controller/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Controller;

use HTTP;
use Methods;

/**
* Show Capella page with uploaded image
Expand All @@ -15,6 +16,7 @@ public function __construct($requestUri)

/** Get the image id from request URI */
$imageId = explode('/', $requestUri)[2];
$imageId = Methods::imageNameToId($imageId);

/** Check if image exist */
\Methods::getPathToImageSource($imageId);
Expand Down
1 change: 0 additions & 1 deletion capella/src/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
new \Controller\Form();
break;


/**
* Process uri
*/
Expand Down

0 comments on commit 6e4c4a3

Please sign in to comment.