Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INNOVERY FIX - Update the V8 Api to allow for upload of DocumentRevisions of documents similar to notes #457

Open
wants to merge 2 commits into
base: hotfix
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions public/legacy/Api/V8/Service/ModuleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions public/legacy/modules/DocumentRevisions/vardefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down