← All changes
|
includes/Core/Integration/ZohoProjects/FilesApiHelper.php
+20
-2
2.10.0
→
3.3.1
View file →
| @@ -6,8 +6,9 @@ | ||
| 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 |
| @@ -30,9 +31,9 @@ | ||
| 30 | 31 | $this->_payloadBoundary = wp_generate_password(24); |
| 31 | 32 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 32 | 33 | $this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; |
| 33 | 34 | $this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 34 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 35 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 35 | 36 | } |
| 36 | 37 | |
| 37 | 38 | /** |
| 38 | 39 | * Helps to execute upload files api |
| @@ -45,9 +46,10 @@ | ||
| 45 | 46 | * @return mixed $uploadedFiles ID's of uploaded file in Zoho Projects |
| 46 | 47 | */ |
| 47 | 48 | public function uploadFiles($files, $portalId, $projectId, $event, $eventId, $dataCenter) |
| 48 | 49 | { |
| 49 | - $uploadFileEndpoint = "https://projectsapi.zoho.{$dataCenter}/restapi/portal/{$portalId}/projects/{$projectId}/" . ('task' === $event || 'subtask' === $event ? 'tasks' : 'bugs') . "/${eventId}/attachments/"; | |
| 50 | + $files = self::normalizeFileNames($files); | |
| 51 | + $uploadFileEndpoint = "https://projectsapi.zoho.{$dataCenter}/restapi/portal/{$portalId}/projects/{$projectId}/" . ('task' === $event || 'subtask' === $event ? 'tasks' : 'bugs') . "/{$eventId}/attachments/"; | |
| 50 | 52 | |
| 51 | 53 | $payload = ''; |
| 52 | 54 | if (is_array($files)) { |
| 53 | 55 | foreach ($files as $fileIndex => $fileName) { |
| @@ -77,6 +79,22 @@ | ||
| 77 | 79 | $payload .= '--' . $this->_payloadBoundary . '--'; |
| 78 | 80 | $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 79 | 81 | |
| 80 | 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; | |
| 81 | 99 | } |
| 82 | 100 | } |