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

Add drawio support #158

Merged
merged 2 commits into from
Jun 20, 2022
Merged
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
85 changes: 80 additions & 5 deletions lib/Service/FilesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,18 @@ private function parseMimeTypeText(string $mimeType, string $extension, string &
throw new KnownFileMimeTypeException();
}

// 20220219 Parse XML files as TEXT files
if (substr($mimeType, 0, 15) === 'application/xml') {
$parsed = self::MIMETYPE_TEXT;
throw new KnownFileMimeTypeException();
}

// 20220219 Parse .drawio file
if ($extension === 'drawio') {
$parsed = self::MIMETYPE_TEXT;
throw new KnownFileMimeTypeException();
}

$textMimes = [
'application/epub+zip'
];
Expand Down Expand Up @@ -1039,14 +1051,77 @@ private function extractContentFromFilePDF(FilesDocument $document, File $file)
return;
}

try {
$document->setContent(
base64_encode($file->getContent()), IIndexDocument::ENCODED_BASE64
);
} catch (NotPermittedException | LockedException $e) {
// 20220219 Inflate drawio file
if ( $file->getExtension() === 'drawio') {
$content = $file->getContent();

try {
$xml = simplexml_load_string($content);

// Initialize $content
$content = '';

foreach ($xml->diagram as $child) {
$deflated_content = (string)$child;
$base64decoded = base64_decode($deflated_content);
$urlencoded_content = gzinflate($base64decoded);
$urldecoded_content = urldecode($urlencoded_content);

// Remove image tag
$diagram_str = preg_replace('/style=\"shape=image[^"]*\"/', '', $urldecoded_content);

// Construct XML
$diagram_xml = simplexml_load_string($diagram_str);
$content = $content . ' ' . $this->readDrawioXmlValue($diagram_xml);
}

} catch (\Throwable $t) {
}

try {
$document->setContent(
// 20220219 Pass content of inflated drawio graph xml
base64_encode($content), IIndexDocument::ENCODED_BASE64
);
} catch (NotPermittedException | LockedException $e) {
}
} else {
try {
$document->setContent(
base64_encode($file->getContent()), IIndexDocument::ENCODED_BASE64
);
} catch (NotPermittedException | LockedException $e) {
}
}
}

// 20220220 Read Draw.io XML elements and return a space separated
// strings, stripped of HTML tags, to be indexed.
/**
* @param SimpleXMLElement $element
*
* @return string
*/
private function readDrawioXmlValue(\SimpleXMLElement $element) {
$str = '';
if( $element['value'] != null && trim(strval($element['value'])) !== '') {
$str = $str . " " . trim(strval($element['value']));
}
if( $element != null && trim(strval($element)) !== '') {
$str = $str . " " . trim(strval($element));
}

try {
foreach ($element->children() as $child) {
$str = $str . " " . $this->readDrawioXmlValue($child);
}
} finally {
}

// Strip HTML tags
$str_without_tags = preg_replace('/<[^>]*>/', ' ', $str);
return $str_without_tags;
}

/**
* @param FilesDocument $document
Expand Down
1 change: 1 addition & 0 deletions test.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="embed.diagrams.net" modified="2022-05-14T01:26:13.931Z" agent="5.0 (X11)" etag="mcFH4BbWC4NME3q3iiyV" version="18.0.3" type="embed" pages="2"><diagram id="bHMW1FkPiVIQ63f7flve" name="Page-1">jZLBToQwEIafhqMJ0Mh6FtfdiydMPDftSJsUStphAZ/eItMFJCaemPnm73T6Dwkrm/HieKferAST5KkcE/aS5HlWFHn4zGRayImdFlA7LUm0gkp/AcGUaK8l+J0QrTWouz0Utm1B4I5x5+ywl31as7+14zUcQCW4OdIPLVEt9OkxXfkVdK3izVlKlYZHMQGvuLTDBrFzwkpnLS5RM5ZgZvOiL8u51z+q98EctPifA7SIGzc9ve0dPD5UvRDgPbXzOMWXO9u3EubDacKeB6URqo6LuTqEXQemsDEhy0J4HIYa3sAhjBtEw13ANoBuChKqsoKMmn7lw+p7Fs1UG8+jjtOq63vr1Y0QkCExXY3/qW1+X3b+Bg==</diagram><diagram id="nxyU9POgWWsITp6xB2Gz" name="Page-2">jZJNb4MwDIZ/DcdKQDS6XUdpd1h3odLOEfFIpkBQMAX66xeGU0DVpJ1wHn/y2gFLq+FkeSPPRoAO4lAMATsEcRwlSew+Exlnsmf7GZRWCQpaQK5uQDAk2ikB7SYQjdGomi0sTF1DgRvGrTX9NuzL6G3XhpfwAPKC60f6qQTKmT4/hQt/A1VK3zkKyVNxH0yglVyYfoVYFrDUGoOzVQ0p6Ek8r8ucd/zDex/MQo3/SagP7IKnl/fzR6ay/Ijft+S6oypXrjv64Qu0uMu7ooC2pd21OHo5rOlqAVPFMGCvvVQIecOLydu7A3BMYqXdK3Lm44S+HViEYYVo4hOYCtCOLoS8LCH16HziPb37ZRmRV1iuFuHzOO2/vJdeJHIGqeSfyzZ+faubZtkP</diagram></mxfile>
4 changes: 4 additions & 0 deletions test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<test>
<test-key>Test-key-success</test-key>
<test-value>Test-value-success</test-value>
</test>