Skip to content

Commit

Permalink
MIME type registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Rojek committed Mar 10, 2021
1 parent 0fdc792 commit 328938b
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 123 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@ Once installed, you will see an option to create a Draw.io diagram from the 'cre
- Requires [Nextcloud](https://nextcloud.com) >11.0.0
- Multi language support (l10n)
- Inspired by the old Draw.io Integration and OnlyOffice
- Tested with Chrome 58-86 and Firefox 53-77
- Tested with PHP 5.6/7.1/7.3
- Draw.io Integration v0.9.9 tested with NextCloud 11.0.3 / 12.0.2 / 13.0.6 / 14.0.4 / 15.0.0 / 16.0.0 / 17.0.0 / 18.0.0 / 19.0.0 / 20.0.0* / 21.0.0*
* see Known Issues

- Tested with Chrome 58-89 and Firefox 53-86
- Tested with PHP 5.6/7.1/7.3/8.0
- Draw.io Integration v1.0.0 tested with NextCloud 11.0.3 / 12.0.2 / 13.0.6 / 14.0.4 / 15.0.0 / 16.0.0 / 17.0.0 / 18.0.0 / 19.0.0 / 20.0.0 / 21.0.0


## Mimetype detection ##

Unfortunately, apps can't declare new mimetypes on the fly. To make
Draw.io work properly, you need to add a new mimetypes in the
`mimetypemapping.json` file (at Nextcloud level).
To make Draw.io work properly, you need to add a new mimetypes in the `mimetypemapping.json` file (at Nextcloud level).

To proceed, just copy `/resources/config/mimetypemapping.dist.json` to
`/config/mimetypemapping.json` (in the `config/` folder at Nextcloud’s
root directory; the file should be stored next to the `config.php`
file). Afterwards add the two following line just after the “_comment”
lines.
Go to `Admin settings > Additional settings` ( `/index.php/settings/admin/additional` ) and click the `Save` button to register MIME types.

Or you can do it manually:
- Download [mimetypemapping.json](https://github.com/pawelrojek/nextcloud-drawio/mimetypemapping.json) and save it in `config` folder
or
- Copy `/resources/config/mimetypemapping.dist.json` to `/config/mimetypemapping.json`
(in the `config/` folder at Nextcloud’s root directory; the file should be stored next to the `config.php` file).
Afterwards add the two following line just after the “_comment” lines.
"drawio": ["application/x-drawio"],

If all other mimetypes are not working properly, just run the
Expand All @@ -41,28 +40,29 @@ following command:
sudo -u www-data php occ files:scan --all

## Download ##
Current release: [zip](https://github.com/pawelrojek/nextcloud-drawio/releases/download/v.0.9.9/drawio-v0.9.9.zip) or [tar.gz](https://github.com/pawelrojek/nextcloud-drawio/releases/download/v.0.9.9/drawio-v0.9.9.tar.gz)

Current release: [zip](https://github.com/pawelrojek/nextcloud-drawio/releases/download/v.1.0.0/drawio-v1.0.0.zip) or [tar.gz](https://github.com/pawelrojek/nextcloud-drawio/releases/download/v.1.0.0/drawio-v1.0.0.tar.gz)



## Changelog ##

## 0.9.9
- NC21 compatibility
- public link support (#20)
## 1.0.0
- Auto MIME type registration for .drawio #122
- Public Link support fixed #133
- Client-side/JS bug fixes #138
- XML override deprecated (please rename your files to *.drawio)

[Complete changelog](https://github.com/pawelrojek/nextcloud-drawio/blob/master/drawio/CHANGELOG.md)


## Installation ##
1. Copy Nextcloud draw.io integration app ("drawio" directory) to your Nextcloud server into the /apps/ directory
2. Go to Apps -> "+ Apps" > "Not Enabled" and _Enable_ the **Draw.io** application
2. Go to "Apps" > "+ Apps" > "Not Enabled" and _Enable_ the **Draw.io** application
3. Go to "Admin settings > Additional settings" ( /index.php/settings/admin/additional ) and click the "Save" button to register MIME types.


## Known Issues ##
- If you're experiencing problems while updating your Nextcloud intance, try to disable/delete Draw.io integration app (/apps/drawio/) and then install/copy it again after the NC update is completed.
- Files won't open in Draw.io while using Dashboard / Recommended Files (NC20)


## Configuration ##
Expand Down
7 changes: 5 additions & 2 deletions drawio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Change Log

## 1.0.0
-
- Auto MIME type registration for .drawio #122
- Public Link support fixed #133
- Client-side/JS bug fixes #138
- XML override deprecated (please rename your files to *.drawio)

## 0.9.9
- NC21 compatibility
- public link support (#20)
- Public link support (#20)

## 0.9.8
- NC20 compatibility (#127)
Expand Down
13 changes: 12 additions & 1 deletion drawio/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>Draw.io</name>
<summary>Draw.io integration app</summary>
<description>Integrates Draw.io with Nextcloud</description>
<version>0.9.9</version>
<version>1.0.0</version>
<licence>agpl</licence>
<author>Paweł Rojek</author>
<namespace>Drawio</namespace>
Expand All @@ -25,4 +25,15 @@
<settings>
<admin>OCA\Drawio\AdminSettings</admin>
</settings>
<repair-steps>
<install>
<step>OCA\Drawio\Migration\RegisterMimeType</step>
</install>
<post-migrate>
<step>OCA\Drawio\Migration\RegisterMimeType</step>
</post-migrate>
<uninstall>
<step>OCA\Drawio\Migration\UnregisterMimeType</step>
</uninstall>
</repair-steps>
</info>
8 changes: 2 additions & 6 deletions drawio/controller/editorcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,16 @@ public function create($name, $dir, $shareToken = NULL)
return ["error" => $this->trans->t("You don't have enough permission to create file")];
}


$name = $userFolder->getNonExistingName($name);

$template = " "; //"space" - empty file
$template = " "; //"space" - empty file for drawio

try {
if (\version_compare(\implode(".", \OCP\Util::getVersion()), "19", "<")) {
$file = $folder->newFile($name);
$file->putContent($template);
} else {
$file = $folder->newFile($name, $template);
$file = $folder->newFile($name, $template);
}
} catch (NotPermittedException $e) {
$this->logger->logException($e, ["message" => "Can't create file: $name", "app" => $this->appName]);
Expand Down Expand Up @@ -169,7 +168,6 @@ public function index($fileId, $shareToken = NULL, $filePath = NULL) {

$drawioUrl = $this->config->GetDrawioUrl();
$theme = $this->config->GetTheme();
$overrideXml = $this->config->GetOverrideXml();
$offlineMode = $this->config->GetOfflineMode();
$lang = $this->config->GetLang();
$lang = trim(strtolower($lang));
Expand Down Expand Up @@ -220,7 +218,6 @@ public function index($fileId, $shareToken = NULL, $filePath = NULL) {
"drawioUrlArgs" => $drawioUrlArgs,
"drawioTheme" => $theme,
"drawioLang" => $lang,
"drawioOverrideXml" => $overrideXml,
"drawioOfflineMode" => $offlineMode,
"drawioFilePath" => rawurlencode($relativePath),
"drawioAutosave" =>$this->config->GetAutosave(),
Expand Down Expand Up @@ -311,7 +308,6 @@ public function PublicFile($fileId, $filePath = NULL, $shareToken = NULL) {
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$format = $this->config->formats[$ext];

//TODO: add for xml override
if (!isset($format)) {
$this->logger->info("Format is not supported for editing: $fileName", array("app" => $this->appName));
return ["error" => $this->trans->t("Format is not supported")];
Expand Down
20 changes: 14 additions & 6 deletions drawio/controller/settingscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\IRequest;

use OCA\Drawio\AppConfig;
use OCA\Drawio\Migration;



Expand Down Expand Up @@ -60,7 +61,6 @@ public function __construct($AppName,
public function index() {
$data = [
"drawioUrl" => $this->config->GetDrawioUrl(),
"drawioOverrideXml" => $this->config->GetOverrideXml(),
"drawioOfflineMode" => $this->config->GetOfflineMode(),
"drawioTheme" => $this->config->GetTheme(),
"drawioLang" => $this->config->GetLang(),
Expand All @@ -71,25 +71,34 @@ public function index() {



public function settings($drawio, $overridexml, $theme)
public function settings()
{
$drawio = trim($_POST['drawioUrl']);
$overridexml = trim($_POST['overrideXml']);
$offlinemode = trim($_POST['offlineMode']);
$theme = trim($_POST['theme']);
$lang = trim($_POST['lang']);
$autosave = trim($_POST['autosave']);

$this->config->SetDrawioUrl($drawio);
$this->config->SetOverrideXml($overridexml);
$this->config->SetOfflineMode($offlinemode);
$this->config->SetTheme($theme);
$this->config->SetLang($lang);
$this->config->SetAutosave($autosave);

if (version_compare(implode(".", \OCP\Util::getVersion()), "13", ">=")) {
$checkmime = new \OCA\Drawio\Migration\CheckMimeType();
$registered = $checkmime->run();

if ($registered == false) {
$mimeTypeLoader = \OC::$server->getMimeTypeLoader();
$mime = new \OCA\Drawio\Migration\RegisterMimeType($mimeTypeLoader);
$output = new \OC\Migration\SimpleOutput($this->logger, $this->appName);
$mime->run($output);
}
}

return [
"drawioUrl" => $this->config->GetDrawioUrl(),
"overrideXml" => $this->config->GetOverrideXml(),
"offlineMode" => $this->config->GetOfflineMode(),
"theme" => $this->config->GetTheme(),
"lang" => $this->config->GetLang(),
Expand All @@ -112,7 +121,6 @@ public function getsettings()
$data = array();
$data['formats'] = $this->config->formats;
$data['settings'] = array();
$data['settings']['overrideXml'] = $this->config->GetOverrideXml();
$data['settings']['offlineMode'] = $this->config->GetOfflineMode();
return $data;
}
Expand Down
3 changes: 0 additions & 3 deletions drawio/controller/viewercontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function index($fileId, $shareToken = NULL, $filePath = NULL) {

$drawioUrl = $this->config->GetDrawioUrl();
$theme = $this->config->GetTheme();
$overrideXml = $this->config->GetOverrideXml();
$offlineMode = $this->config->GetOfflineMode();
$lang = $this->config->GetLang();
$lang = trim(strtolower($lang));
Expand Down Expand Up @@ -177,7 +176,6 @@ public function index($fileId, $shareToken = NULL, $filePath = NULL) {
"drawioUrlArgs" => $drawioUrlArgs,
"drawioTheme" => $theme,
"drawioLang" => $lang,
"drawioOverrideXml" => $overrideXml,
"drawioOfflineMode" => $offlineMode,
"drawioFilePath" => rawurlencode($relativePath),
"drawioAutosave" => $this->config->GetAutosave(),
Expand Down Expand Up @@ -270,7 +268,6 @@ public function PublicFile($fileId, $filePath = NULL, $shareToken = NULL) {
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$format = $this->config->formats[$ext];

//TODO: add for xml override
if (!isset($format)) {
$this->logger->info("Format is not supported for editing: $fileName", array("app" => $this->appName));
return ["error" => $this->trans->t("Format is not supported")];
Expand Down
4 changes: 2 additions & 2 deletions drawio/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
ncClient.putFileContents(
filePath,
payload.xml, {
contentType: "x-application/drawio",
contentType: "application/x-drawio",
overwrite: false
}
)
Expand All @@ -169,7 +169,7 @@
ncClient.putFileContents(
filePath,
payload.xml, {
contentType: "x-application/drawio",
contentType: "application/x-drawio",
overwrite: false
}
)
Expand Down
74 changes: 16 additions & 58 deletions drawio/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@
}
});

if ((fileList.fileActions.getDefaultFileAction(attr.mime, "file", OC.PERMISSION_READ) == false) || (OCA.AppSettings.overrideXml == "yes")) {
fileList.fileActions.setDefault(attr.mime, "drawioOpen");
} else if(attr.mime == "application/x-drawio") {
if(attr.mime == "application/x-drawio") {
fileList.fileActions.setDefault(attr.mime, "drawioOpen");
}
}
Expand All @@ -109,11 +107,7 @@
OCA.DrawIO.CreateNewFile = function (name, fileList) {
var dir = fileList.getCurrentDirectory();

var postData = {
name: name,
dir: dir
};

var postData = "name="+encodeURIComponent(name)+"&dir="+encodeURIComponent(dir);

var url = OC.generateUrl("apps/" + OCA.DrawIO.AppName + "/ajax/new");

Expand Down Expand Up @@ -154,30 +148,16 @@
}

var newfilefunc = function () {

if(OCA.AppSettings.overrideXml == "yes") {
menu.addMenuEntry({
id: "drawIoDiagram",
displayName: t(OCA.DrawIO.AppName, "New Diagram"),
templateName: t(OCA.DrawIO.AppName, "New Diagram.xml"),
iconClass: "icon-drawio-new-xml", //fileType: "x-application/drawio",
fileType: "xml",
actionHandler: function (fileName) {
OCA.DrawIO.CreateNewFile(fileName, fileList);
}
});
} else {
menu.addMenuEntry({
id: "drawIoDiagram",
displayName: t(OCA.DrawIO.AppName, "Diagram"),
templateName: t(OCA.DrawIO.AppName, "New Diagram.drawio"),
iconClass: "icon-drawio-new-xml", //fileType: "x-application/drawio",
fileType: "drawio",
actionHandler: function (fileName) {
OCA.DrawIO.CreateNewFile(fileName, fileList);
}
});
}
menu.addMenuEntry({
id: "drawIoDiagram",
displayName: t(OCA.DrawIO.AppName, "New Diagram"),
templateName: t(OCA.DrawIO.AppName, "New Diagram.drawio"),
iconClass: "icon-drawio-new-xml", //fileType: "application/x-drawio",
fileType: "drawio",
actionHandler: function (fileName) {
OCA.DrawIO.CreateNewFile(fileName, fileList);
}
});
};

OCA.DrawIO.GetSettings(newfilefunc);
Expand All @@ -190,15 +170,14 @@

attach: function (manager) {
manager.addPreviewHandler('application/x-drawio', this.handlePreview.bind(this));
if (OCA.AppSettings.overrideXml == "yes") {
manager.addPreviewHandler('application/xml', this.handlePreview.bind(this)); //[todo] conditions
}
},

handlePreview: function (model, $thumbnailDiv, $thumbnailContainer, fallback) {
var previewWidth = Math.floor($thumbnailContainer.parent().width() + 50); // 50px for negative margins
var previewHeight = Math.floor(previewWidth / (16 / 9));

//[TODO]

//var downloadUrl = Files.getDownloadUrl(model.get('name'), model.get('path'));

//var viewer = OC.generateUrl('/apps/drawio/?minmode=true&file={file}', {file: downloadUrl});
Expand Down Expand Up @@ -275,16 +254,6 @@
$(document)
.ready(function () {

PluginDrawIO_ChangeIcons = function () {
$("#filestable").find("tr[data-type=file]").each(function () {
if ((($(this).attr("data-mime") == "application/xml") || ($(this).attr("data-mime") == "application/x-drawio")) && ($(this).find("div.thumbnail").length > 0)) {
if ($(this).find("div.thumbnail").hasClass("icon-drawio-xml") == false) {
$(this).find("div.thumbnail").addClass("icon icon-drawio-xml");
}
}
});
};

PluginDrawIO_ChangeIconsNative = function () {
$("#filestable").find("tr[data-type=file]").each(function () {
if (($(this).attr("data-mime") == "application/x-drawio") && ($(this).find("div.thumbnail").length > 0)) {
Expand All @@ -300,22 +269,11 @@ $(document)
.add('#app-content-extstoragemounts')
.on('changeDirectory', function (e) {
if (OCA.AppSettings == null) return;

if (OCA.AppSettings.overrideXml == "yes") {
PluginDrawIO_ChangeIcons();
} else {
PluginDrawIO_ChangeIconsNative();
}
PluginDrawIO_ChangeIconsNative();
})
.on('fileActionsReady', function (e) {

if (OCA.AppSettings == null) return;

if (OCA.AppSettings.overrideXml == "yes") {
PluginDrawIO_ChangeIcons();
} else {
PluginDrawIO_ChangeIconsNative();
}
PluginDrawIO_ChangeIconsNative();
});
}
});
Loading

0 comments on commit 328938b

Please sign in to comment.