Skip to content

Commit

Permalink
Filestack PHP SDK Issues Fixed (#65)
Browse files Browse the repository at this point in the history
* Update CommonMixin.php

* Update Filelink.php

* Update FilestackClient.php

* Update UploadProcessor.php

* Update composer.json

* Update FilestackClient.php

* Update UploadProcessor.php

* Update CommonMixin.php

* Update TransformationMixin.php

* Update FilestackClient.php
  • Loading branch information
sathish-filestack authored May 15, 2024
1 parent 2d5df1d commit db2b64f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
90 changes: 90 additions & 0 deletions filestack/FilestackClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,11 @@ public function upload($filepath, $options = [])
$location = $options['location'];
}

$path = '';
if (array_key_exists('path', $options)) {
$path = $options['path'];
}

$filename = basename($filepath);
if (array_key_exists('filename', $options)) {
$filename = $options['filename'];
Expand All @@ -701,6 +706,7 @@ public function upload($filepath, $options = [])
'filesize' => filesize($filepath),
'mimetype' => $mimetype,
'location' => $location,
'path' => $path,
];

// register job
Expand Down Expand Up @@ -782,6 +788,90 @@ public function uploadUrl($resource, $options = [])
return $filelink;
}

/**
* Get Doc Detection is used for get coords or process image filelink
*
* @param string $resource URL or Handle or Storage path
* pass into this function
* @param array $options extra optional params. e.g.
* coords (bool, true|false),
* preprocess (bool, true|false)
*
* @throws FilestackException if API call fails
*
* @return Filestack\Filelink
*/
public function getDocDetection($resource, $options = [])
{
// generate url
$url = $this->getDocDetectionUrl($resource, $options);

// send get request
$response = $this->sendRequest('POST', $url);
$status_code = $response->getStatusCode();

if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}

$json_response = json_decode($response->getBody(), true);

if (array_key_exists('coords', $json_response)) {
return [
'data' => $json_response
];
} else {
return [
'url' => $json_response['url'],
'mimetype' => $json_response['type'],
'size' => $json_response['size']
];
}
}

/**
* Get Doc Detection Url is used for generate full request url
*
* @param string $resource URL or Handle or Storage
* pass into this function
* @param array $options extra optional params. e.g.
* coords (bool, true|false),
* preprocess (bool, true|false)
*
* @return string it will return generated url
*/
public function getDocDetectionUrl($sources, $options = [])
{
if (!array_key_exists('coords', $options)) {
$options['coords'] = 'false';
} else {
$options['coords'] = filter_var($options['coords'], FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
}

if (!array_key_exists('preprocess', $options)) {
$options['preprocess'] = 'true';
} else {
$options['preprocess'] = filter_var($options['preprocess'], FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
}

$url = sprintf('%s/API_KEY/security=p:%s,s:%s/doc_detection=coords:%s,preprocess:%s/%s',
$this->getCustomUrl(FilestackConfig::CDN_URL),
$this->security->policy,
$this->security->signature,
$options['coords'],
$options['preprocess'],
$sources
);

if ($this->hasHttpOrHttps($sources)) {
$url = str_replace("/API_KEY", "/{$this->api_key}", $url);
} else {
$url = str_replace("/API_KEY", "", $url);
}

return $url;
}

/**
* Bundle an array of files into a zip file. This task takes the file or files
* that are passed in the array and compresses them into a zip file. Sources can
Expand Down
1 change: 1 addition & 0 deletions filestack/UploadProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function registerUploadTask($api_key, $metadata)
$this->appendData($data, 'mimetype', $metadata['mimetype']);
$this->appendData($data, 'size', $metadata['filesize']);
$this->appendData($data, 'store_location', $metadata['location']);
$this->appendData($data, 'store_path', $metadata['path']);
$this->appendData($data, 'multipart', true);

array_push($data, ['name' => 'files',
Expand Down
22 changes: 22 additions & 0 deletions filestack/mixins/CommonMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ protected function getCustomUrl($url) {
return $url;
}

/**
* Has Http Or Https is used for validate external url
*
* @param string $url pass string value
*
* @return boolean
*/
public function hasHttpOrHttps($url = '') {
// Parse the URL
$urlParts = parse_url($url);

// Check if the URL has a scheme (protocol)
if (isset($urlParts['scheme'])) {
// Check if the scheme is either http or https
if ($urlParts['scheme'] === 'http' || $urlParts['scheme'] === 'https' || $urlParts['scheme'] === 'src') {
return true; // URL has http:// or https:// protocol
}
}

return false; // URL doesn't have http:// or https:// protocol
}

/**
* Check if a string is a valid url.
*
Expand Down
5 changes: 5 additions & 0 deletions filestack/mixins/TransformationMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public function sendTransform($resource, $transform_tasks, $security = null)

// call CommonMixin function
$response = $this->sendRequest('GET', $transform_url);

if ($json_response = json_decode($response->getBody(), true)) {
return $json_response;
}

$filelink = $this->handleResponseCreateFilelink($response);

return $filelink;
Expand Down

0 comments on commit db2b64f

Please sign in to comment.