From e02199d5e6105bfba7c213d378c05bc67b5ca465 Mon Sep 17 00:00:00 2001 From: Vincenzo Rosciano <86845172+Vercin1001@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:07:48 +0200 Subject: [PATCH 1/2] Update ModuleService.php for documentRevisions --- .../legacy/Api/V8/Service/ModuleService.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/public/legacy/Api/V8/Service/ModuleService.php b/public/legacy/Api/V8/Service/ModuleService.php index 8ec5612ab2..aab05b42d1 100644 --- a/public/legacy/Api/V8/Service/ModuleService.php +++ b/public/legacy/Api/V8/Service/ModuleService.php @@ -277,6 +277,9 @@ public function createRecord(CreateModuleParams $params, Request $request) if ($fileUpload && $bean->module_dir === 'Notes') { $this->addFileToNote($bean->id, $attributes); } + if ($fileUpload && $bean->module_dir === 'DocumentRevisions') { + $this->addFileToDocumentRevision($bean->id, $attributes); + } if ($fileUpload && $bean->module_dir === 'Documents') { $this->addFileToDocument($bean, $attributes); } @@ -367,6 +370,52 @@ private function addFileToDocument(SugarBean $bean, array $attributes) * @param $attributes * @throws Exception */ + protected function addFileToDocumentRevision($beanId, $attributes) + { + global $sugar_config, $log; + + $module = 'DocumentRevision'; + if (!empty($attributes['moduleName'])) { + $module = $attributes['moduleName']; + unset($attributes['moduleName']); + } + BeanFactory::unregisterBean($module, $beanId); + $bean = $this->beanManager->getBeanSafe($module, $beanId); + + // Write file to upload dir + try { + // Checking file extension + $extPos = strrpos((string) $attributes['filename'], '.'); + $fileExtension = substr((string) $attributes['filename'], $extPos + 1); + + if ($extPos === false || empty($fileExtension) || in_array( + $fileExtension, + $sugar_config['upload_badext'], + true + )) { + throw new Exception('File upload failed: File extension is not included or is not valid.'); + } + + $fileName = $bean->id; + $fileContents = $attributes['filecontents']; + $targetPath = 'upload/' . $fileName; + $content = base64_decode($fileContents); + + $file = fopen($targetPath, 'wb'); + fwrite($file, $content); + fclose($file); + } catch (Exception $e) { + $log->error('addFileToNote: ' . $e->getMessage()); + throw new Exception($e->getMessage()); + } + + // Fill in file details for use with upload checks + $mimeType = mime_content_type($targetPath); + $bean->filename = $attributes['filename']; + $bean->file_mime_type = $mimeType; + $bean->save(); + } + protected function addFileToNote($beanId, $attributes) { global $sugar_config, $log; From 8c5c421128e713f0328ccb191a7250ccffd3e4d0 Mon Sep 17 00:00:00 2001 From: Vincenzo Rosciano <86845172+Vercin1001@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:12:05 +0200 Subject: [PATCH 2/2] Update vardefs.php This update it will let you post a file via api V8 --- public/legacy/modules/DocumentRevisions/vardefs.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/public/legacy/modules/DocumentRevisions/vardefs.php b/public/legacy/modules/DocumentRevisions/vardefs.php index b8eabfb8e9..c315bf4215 100755 --- a/public/legacy/modules/DocumentRevisions/vardefs.php +++ b/public/legacy/modules/DocumentRevisions/vardefs.php @@ -221,6 +221,13 @@ 'len' => '255', 'source' => 'non-db', ), + 'filecontents' => + array( + 'name' => 'filecontents', + 'vname' => 'LBL_FILE_CONTENTS', + 'type' => 'varchar', + 'source' => 'non-db', + ), 'latest_revision' => array( 'name' => 'latest_revision',