← All changes
|
includes/Core/Integration/ZohoMail/FilesApiHelper.php
+25
-4
2.0
→
3.3.1
View file →
| @@ -6,26 +6,31 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoMail; |
| 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 $_payloadBoundary; |
| 18 | 20 | private $_basepath; |
| 19 | 21 | |
| 20 | - public function __construct($tokenDetails, $formID, $entryID) { | |
| 22 | + public function __construct($tokenDetails, $formID, $entryID) | |
| 23 | + { | |
| 21 | 24 | $this->_payloadBoundary = wp_generate_password(24); |
| 22 | 25 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 23 | 26 | $this->_defaultHeader['Content-Type'] = 'application/octet-stream; boundary=' . $this->_payloadBoundary; |
| 24 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 27 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 25 | 28 | } |
| 26 | 29 | |
| 27 | - public function uploadFiles($files, $accountId, $dataCenter) { | |
| 30 | + public function uploadFiles($files, $accountId, $dataCenter) | |
| 31 | + { | |
| 32 | + $files = self::normalizeFileNames($files); | |
| 28 | 33 | $uploadFileEndpoint = "https://mail.zoho.{$dataCenter}/api/accounts/{$accountId}/messages/attachments"; |
| 29 | 34 | |
| 30 | 35 | $payload = ''; |
| 31 | 36 | |
| @@ -43,6 +48,22 @@ | ||
| 43 | 48 | $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 44 | 49 | |
| 45 | 50 | return $uploadResponse; |
| 46 | 51 | } |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), | |
| 56 | + * so reduce each to the stored file name before resolving against the entry directory. | |
| 57 | + * | |
| 58 | + * @param mixed $files file name(s) or URL(s) | |
| 59 | + * | |
| 60 | + * @return mixed | |
| 61 | + */ | |
| 62 | + private static function normalizeFileNames($files) | |
| 63 | + { | |
| 64 | + if (is_array($files)) { | |
| 65 | + return array_map([__CLASS__, 'normalizeFileNames'], $files); | |
| 66 | + } | |
| 67 | + return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; | |
| 47 | 68 | } |
| 48 | 69 | } |