← All changes
|
includes/Core/Integration/ZohoProjects/FilesApiHelper.php
+34
-13
2.0
→
3.3.1
View file →
| @@ -6,14 +6,16 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoProjects; |
| 9 | 9 | |
| 10 | +use BitCode\BitForm\Core\Util\FileHandler; | |
| 10 | 11 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Provide functionality for Upload files |
| 14 | 15 | */ |
| 15 | -final class FilesApiHelper { | |
| 16 | +final class FilesApiHelper | |
| 17 | +{ | |
| 16 | 18 | private $_defaultHeader; |
| 17 | 19 | private $_apiDomain; |
| 18 | 20 | private $_payloadBoundary; |
| 19 | 21 | private $_basepath; |
| @@ -19,32 +21,35 @@ | ||
| 19 | 21 | private $_basepath; |
| 20 | 22 | |
| 21 | 23 | /** |
| 22 | 24 | * |
| 23 | - * @param Object $tokenDetails Api token details | |
| 24 | - * @param Integer $formID ID of the form, for which integration is executing | |
| 25 | - * @param Integer $entryID Current submittion ID | |
| 25 | + * @param object $tokenDetails Api token details | |
| 26 | + * @param integer $formID ID of the form, for which integration is executing | |
| 27 | + * @param integer $entryID Current submittion ID | |
| 26 | 28 | */ |
| 27 | - public function __construct($tokenDetails, $formID, $entryID) { | |
| 29 | + public function __construct($tokenDetails, $formID, $entryID) | |
| 30 | + { | |
| 28 | 31 | $this->_payloadBoundary = wp_generate_password(24); |
| 29 | 32 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 30 | 33 | $this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; |
| 31 | 34 | $this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 32 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 35 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 33 | 36 | } |
| 34 | 37 | |
| 35 | 38 | /** |
| 36 | 39 | * Helps to execute upload files api |
| 37 | 40 | * |
| 38 | - * @param Mixed $files Files path | |
| 39 | - * @param Bool $isAttachment Check upload type | |
| 40 | - * @param Mixed $module Attachment Module name | |
| 41 | - * @param Mixed $recordID Record id | |
| 41 | + * @param mixed $files Files path | |
| 42 | + * @param bool $isAttachment Check upload type | |
| 43 | + * @param mixed $module Attachment Module name | |
| 44 | + * @param mixed $recordID Record id | |
| 42 | 45 | * |
| 43 | - * @return Array $uploadedFiles ID's of uploaded file in Zoho Projects | |
| 46 | + * @return mixed $uploadedFiles ID's of uploaded file in Zoho Projects | |
| 44 | 47 | */ |
| 45 | - public function uploadFiles($files, $portalId, $projectId, $event, $eventId, $dataCenter) { | |
| 46 | - $uploadFileEndpoint = "https://projectsapi.zoho.{$dataCenter}/restapi/portal/{$portalId}/projects/{$projectId}/" . ('task' === $event || 'subtask' === $event ? 'tasks' : 'bugs') . "/${eventId}/attachments/"; | |
| 48 | + public function uploadFiles($files, $portalId, $projectId, $event, $eventId, $dataCenter) | |
| 49 | + { | |
| 50 | + $files = self::normalizeFileNames($files); | |
| 51 | + $uploadFileEndpoint = "https://projectsapi.zoho.{$dataCenter}/restapi/portal/{$portalId}/projects/{$projectId}/" . ('task' === $event || 'subtask' === $event ? 'tasks' : 'bugs') . "/{$eventId}/attachments/"; | |
| 47 | 52 | |
| 48 | 53 | $payload = ''; |
| 49 | 54 | if (is_array($files)) { |
| 50 | 55 | foreach ($files as $fileIndex => $fileName) { |
| @@ -74,6 +79,22 @@ | ||
| 74 | 79 | $payload .= '--' . $this->_payloadBoundary . '--'; |
| 75 | 80 | $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 76 | 81 | |
| 77 | 82 | return $uploadResponse; |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), | |
| 87 | + * so reduce each to the stored file name before resolving against the entry directory. | |
| 88 | + * | |
| 89 | + * @param mixed $files file name(s) or URL(s) | |
| 90 | + * | |
| 91 | + * @return mixed | |
| 92 | + */ | |
| 93 | + private static function normalizeFileNames($files) | |
| 94 | + { | |
| 95 | + if (is_array($files)) { | |
| 96 | + return array_map([__CLASS__, 'normalizeFileNames'], $files); | |
| 97 | + } | |
| 98 | + return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; | |
| 78 | 99 | } |
| 79 | 100 | } |