Skip to content

Commit db2b64f

Browse files
Filestack PHP SDK Issues Fixed (#65)
* 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
1 parent 2d5df1d commit db2b64f

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

filestack/FilestackClient.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@ public function upload($filepath, $options = [])
685685
$location = $options['location'];
686686
}
687687

688+
$path = '';
689+
if (array_key_exists('path', $options)) {
690+
$path = $options['path'];
691+
}
692+
688693
$filename = basename($filepath);
689694
if (array_key_exists('filename', $options)) {
690695
$filename = $options['filename'];
@@ -701,6 +706,7 @@ public function upload($filepath, $options = [])
701706
'filesize' => filesize($filepath),
702707
'mimetype' => $mimetype,
703708
'location' => $location,
709+
'path' => $path,
704710
];
705711

706712
// register job
@@ -782,6 +788,90 @@ public function uploadUrl($resource, $options = [])
782788
return $filelink;
783789
}
784790

791+
/**
792+
* Get Doc Detection is used for get coords or process image filelink
793+
*
794+
* @param string $resource URL or Handle or Storage path
795+
* pass into this function
796+
* @param array $options extra optional params. e.g.
797+
* coords (bool, true|false),
798+
* preprocess (bool, true|false)
799+
*
800+
* @throws FilestackException if API call fails
801+
*
802+
* @return Filestack\Filelink
803+
*/
804+
public function getDocDetection($resource, $options = [])
805+
{
806+
// generate url
807+
$url = $this->getDocDetectionUrl($resource, $options);
808+
809+
// send get request
810+
$response = $this->sendRequest('POST', $url);
811+
$status_code = $response->getStatusCode();
812+
813+
if ($status_code !== 200) {
814+
throw new FilestackException($response->getBody(), $status_code);
815+
}
816+
817+
$json_response = json_decode($response->getBody(), true);
818+
819+
if (array_key_exists('coords', $json_response)) {
820+
return [
821+
'data' => $json_response
822+
];
823+
} else {
824+
return [
825+
'url' => $json_response['url'],
826+
'mimetype' => $json_response['type'],
827+
'size' => $json_response['size']
828+
];
829+
}
830+
}
831+
832+
/**
833+
* Get Doc Detection Url is used for generate full request url
834+
*
835+
* @param string $resource URL or Handle or Storage
836+
* pass into this function
837+
* @param array $options extra optional params. e.g.
838+
* coords (bool, true|false),
839+
* preprocess (bool, true|false)
840+
*
841+
* @return string it will return generated url
842+
*/
843+
public function getDocDetectionUrl($sources, $options = [])
844+
{
845+
if (!array_key_exists('coords', $options)) {
846+
$options['coords'] = 'false';
847+
} else {
848+
$options['coords'] = filter_var($options['coords'], FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
849+
}
850+
851+
if (!array_key_exists('preprocess', $options)) {
852+
$options['preprocess'] = 'true';
853+
} else {
854+
$options['preprocess'] = filter_var($options['preprocess'], FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
855+
}
856+
857+
$url = sprintf('%s/API_KEY/security=p:%s,s:%s/doc_detection=coords:%s,preprocess:%s/%s',
858+
$this->getCustomUrl(FilestackConfig::CDN_URL),
859+
$this->security->policy,
860+
$this->security->signature,
861+
$options['coords'],
862+
$options['preprocess'],
863+
$sources
864+
);
865+
866+
if ($this->hasHttpOrHttps($sources)) {
867+
$url = str_replace("/API_KEY", "/{$this->api_key}", $url);
868+
} else {
869+
$url = str_replace("/API_KEY", "", $url);
870+
}
871+
872+
return $url;
873+
}
874+
785875
/**
786876
* Bundle an array of files into a zip file. This task takes the file or files
787877
* that are passed in the array and compresses them into a zip file. Sources can

filestack/UploadProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function registerUploadTask($api_key, $metadata)
8181
$this->appendData($data, 'mimetype', $metadata['mimetype']);
8282
$this->appendData($data, 'size', $metadata['filesize']);
8383
$this->appendData($data, 'store_location', $metadata['location']);
84+
$this->appendData($data, 'store_path', $metadata['path']);
8485
$this->appendData($data, 'multipart', true);
8586

8687
array_push($data, ['name' => 'files',

filestack/mixins/CommonMixin.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ protected function getCustomUrl($url) {
4343
return $url;
4444
}
4545

46+
/**
47+
* Has Http Or Https is used for validate external url
48+
*
49+
* @param string $url pass string value
50+
*
51+
* @return boolean
52+
*/
53+
public function hasHttpOrHttps($url = '') {
54+
// Parse the URL
55+
$urlParts = parse_url($url);
56+
57+
// Check if the URL has a scheme (protocol)
58+
if (isset($urlParts['scheme'])) {
59+
// Check if the scheme is either http or https
60+
if ($urlParts['scheme'] === 'http' || $urlParts['scheme'] === 'https' || $urlParts['scheme'] === 'src') {
61+
return true; // URL has http:// or https:// protocol
62+
}
63+
}
64+
65+
return false; // URL doesn't have http:// or https:// protocol
66+
}
67+
4668
/**
4769
* Check if a string is a valid url.
4870
*

filestack/mixins/TransformationMixin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public function sendTransform($resource, $transform_tasks, $security = null)
132132

133133
// call CommonMixin function
134134
$response = $this->sendRequest('GET', $transform_url);
135+
136+
if ($json_response = json_decode($response->getBody(), true)) {
137+
return $json_response;
138+
}
139+
135140
$filelink = $this->handleResponseCreateFilelink($response);
136141

137142
return $filelink;

0 commit comments

Comments
 (0)