Commit ca895f18 authored by Dmitry Nikulin's avatar Dmitry Nikulin

Return download link instead of hash

parent 04f76205
...@@ -10,6 +10,11 @@ class AttachmentsClient { ...@@ -10,6 +10,11 @@ class AttachmentsClient {
private $baseUrl; private $baseUrl;
private $client; private $client;
/**
* Creates a new client instance.
*
* @param string $baseUrl Base URL for attachment server API
*/
function __construct ($baseUrl) { function __construct ($baseUrl) {
$this->baseUrl = $this->ensureNotEndsWithSlash($baseUrl); $this->baseUrl = $this->ensureNotEndsWithSlash($baseUrl);
$this->client = new Client(); $this->client = new Client();
...@@ -22,7 +27,7 @@ class AttachmentsClient { ...@@ -22,7 +27,7 @@ class AttachmentsClient {
* *
* @param string $filename Path to file to be uploaded * @param string $filename Path to file to be uploaded
* *
* @return string Hash of uploaded file * @return string Download link for the file
*/ */
public function uploadFromFile ($filename) { public function uploadFromFile ($filename) {
$data = file_get_contents($filename); $data = file_get_contents($filename);
...@@ -35,14 +40,14 @@ class AttachmentsClient { ...@@ -35,14 +40,14 @@ class AttachmentsClient {
* @param string $file File data to be uploaded, stored in memory * @param string $file File data to be uploaded, stored in memory
* @param string $name (optional) Name of file to be passed to server * @param string $name (optional) Name of file to be passed to server
* *
* @return string Hash of uploaded file * @return string Download link for the file
*/ */
public function uploadFromMemory ($data, $name = 'file') { public function uploadFromMemory ($data, $name = 'file') {
$url = $this->getFullMethodUrl('/ul'); $url = $this->getFullMethodUrl('/ul');
$result = $this->sendPostRequestWithFile ($url, $data, $name); $result = $this->sendPostRequestWithFile ($url, $data, $name);
$json = json_decode($result, true); $json = json_decode($result, true);
return $json['id']; return $this->getDownloadUrl($json['id'], $json['filename']);
} }
// *** End of public API methods *** // *** End of public API methods ***
...@@ -51,6 +56,10 @@ class AttachmentsClient { ...@@ -51,6 +56,10 @@ class AttachmentsClient {
return rtrim($url, '/'); return rtrim($url, '/');
} }
private function getDownloadUrl ($hash, $name) {
return $this->baseUrl . '/dl/' . $hash . '/' . $name;
}
private function getFullMethodUrl ($method) { private function getFullMethodUrl ($method) {
return $this->baseUrl . $method; return $this->baseUrl . $method;
} }
...@@ -66,8 +75,6 @@ class AttachmentsClient { ...@@ -66,8 +75,6 @@ class AttachmentsClient {
] ]
]); ]);
echo $response->getBody();
if ($response->getStatusCode() !== 200) { if ($response->getStatusCode() !== 200) {
$message = $message =
'File uploading failed: server returned ' . 'File uploading failed: server returned ' .
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment