new: [event report] fetch from url now detects other formats

- pdf, xlsx, pptx, ods, odt, docx extension documents are now imported via the given module
pull/9370/head
iglocska 2023-11-02 14:41:06 +01:00
parent 38004f65b8
commit 9c5919f96e
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
3 changed files with 48 additions and 20 deletions

View File

@ -305,13 +305,23 @@ class EventReportsController extends AppController
throw new MethodNotAllowedException(__('An URL must be provided'));
}
$url = $this->data['EventReport']['url'];
$markdown = $this->EventReport->downloadMarkdownFromURL($event_id, $url);
$format = 'html';
$parsed_formats = ['pdf', 'xlsx', 'pptx', 'ods', 'odt', 'docx'];
foreach ($parsed_formats as $parsed_format) {
if (substr($url, -(1 + strlen($parsed_format))) === '.' . $parsed_format) {
$format = $parsed_format;
}
}
$content = $this->EventReport->downloadMarkdownFromURL($event_id, $url, $format);
$errors = [];
if (!empty($markdown)) {
if (!empty($content)) {
$report = [
'name' => __('Report from - %s (%s)', $url, time()),
'distribution' => 5,
'content' => $markdown
'content' => $content
];
$errors = $this->EventReport->addReport($this->Auth->user(), $report, $event_id);
} else {
@ -551,11 +561,4 @@ class EventReportsController extends AppController
}
return $savedReport;
}
public function test()
{
$report = $this->EventReport->find('first', ['conditions' => ['EventReport.id' => 25]]);
$errors = [];
$this->EventReport->sendToLLM($report, $this->Auth->user(), $errors);
}
}

View File

@ -85,7 +85,7 @@ class AppModel extends Model
93 => false, 94 => false, 95 => true, 96 => false, 97 => true, 98 => false,
99 => false, 100 => false, 101 => false, 102 => false, 103 => false, 104 => false,
105 => false, 106 => false, 107 => false, 108 => false, 109 => false, 110 => false,
111 => false, 112 => false, 113 => true, 114 => false, 115 => false
111 => false, 112 => false, 113 => true, 114 => false, 115 => false, 116 => false
);
const ADVANCED_UPDATES_DESCRIPTION = array(
@ -1977,6 +1977,9 @@ class AppModel extends Model
$sqlArray[] = "ALTER TABLE `users` ADD COLUMN `last_pw_change` BIGINT(20) NULL DEFAULT NULL;";
$sqlArray[] = "UPDATE `users` SET last_pw_change=date_modified WHERE last_pw_change IS NULL";
break;
case 116:
$sqlArray[] = "ALTER TABLE `event_reports` modify `content` mediumtext";
break;
case 'fixNonEmptySharingGroupID':
$sqlArray[] = 'UPDATE `events` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';
$sqlArray[] = 'UPDATE `attributes` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';

View File

@ -897,31 +897,53 @@ class EventReport extends AppModel
return $toReturn;
}
public function downloadMarkdownFromURL($event_id, $url)
public function downloadMarkdownFromURL($event_id, $url, $format = 'html')
{
$this->Module = ClassRegistry::init('Module');
$module = $this->isFetchURLModuleEnabled();
$formatMapping = [
'html' => 'html_to_markdown',
'pdf' => 'pdf_enrich',
'pptx' => 'pptx_enrich',
'xlsx' => 'xlsx_enrich',
'ods' => 'ods_enrich',
'odt' => 'odt_enrich',
'docx' => 'docx_enrich'
];
$module = $this->isFetchURLModuleEnabled($formatMapping[$format]);
if (!is_array($module)) {
return false;
}
$modulePayload = [
'module' => $module['name'],
'event_id' => $event_id,
'url' => $url
'event_id' => $event_id
];
if ($format === 'html') {
$modulePayload['url'] = $url;
} else {
$url = filter_var($url, FILTER_SANITIZE_URL);
$modulePayload['attachment'] = 'temp.foo';
$modulePayload['data'] = base64_encode(file_get_contents($url));
}
if (!empty($module)) {
$result = $this->Module->queryModuleServer($modulePayload, false, 'Enrichment', false, []);
if (empty($result['results'][0]['values'][0])) {
return '';
if ($format === 'html') {
if (empty($result['results'][0]['values'][0])) {
return '';
}
return $result['results'][0]['values'][0];
} else {
if (empty($result['results'][0]['values'])) {
return '';
}
return $result['results'][0]['values'];
}
return $result['results'][0]['values'][0];
}
return false;
}
public function isFetchURLModuleEnabled() {
public function isFetchURLModuleEnabled($moduleName = 'html_to_markdown') {
$this->Module = ClassRegistry::init('Module');
$module = $this->Module->getEnabledModule('html_to_markdown', 'expansion');
$module = $this->Module->getEnabledModule($moduleName, 'expansion');
return !empty($module) ? $module : false;
}