From 9a25ac537a1bfd009f9a67eb021fc961ae2ad70f Mon Sep 17 00:00:00 2001 From: ClemDee Date: Mon, 22 Jul 2019 12:02:24 +0200 Subject: [PATCH] Add file plugin to display plain text files --- www/common/media-tag.js | 33 +++++++++++++++++++++++++++++++++ www/file/app-file.less | 10 ++++++++++ 2 files changed, 43 insertions(+) diff --git a/www/common/media-tag.js b/www/common/media-tag.js index c820d9201..95830e296 100644 --- a/www/common/media-tag.js +++ b/www/common/media-tag.js @@ -30,9 +30,22 @@ }; + var isplainTextFile = function (metadata) { + // does it type begins with "text/" + if (metadata.type.indexOf("text/") === 0) { return true; } + // no type and no file extension -> let's guess it's plain text + var parsedName = /^(\.?.+?)(\.[^.]+)?$/.exec(metadata.name) || []; + if (!metadata.type && !parsedName[2]) { return true; } + // other exceptions + if (metadata.type === 'application/x-javascript') { return true; } + if (metadata.type === 'application/xml') { return true; } + return false; + } + // Default config, can be overriden per media-tag call var config = { allowed: [ + 'text/plain', 'image/png', 'image/jpeg', 'image/jpg', @@ -53,6 +66,23 @@ text: "Download" }, Plugins: { + /** + * @param {object} metadataObject {name, metadatatype, owners} containing metadata of the file + * @param {strint} url Url of the blob object + * @param {Blob} content Blob object containing the data of the file + * @param {object} cfg Object {Plugins, allowed, download, pdf} containing infos about plugins + * @param {function} cb Callback function: (err, pluginElement) => {} + */ + text: function (metadata, url, content, cfg, cb) { + var plainText = document.createElement('div'); + plainText.className = "plain-text-reader"; + var reader = new FileReader(); + reader.addEventListener('loadend', function (e) { + plainText.innerText = e.srcElement.result; + cb(void 0, plainText); + }); + reader.readAsText(content); + }, image: function (metadata, url, content, cfg, cb) { var img = document.createElement('img'); img.setAttribute('src', url); @@ -271,6 +301,9 @@ var blob = decrypted.content; var mediaType = getType(mediaObject, metadata, cfg); + if (isplainTextFile(metadata)) { + mediaType = "text"; + } if (mediaType === 'application') { mediaType = mediaObject.extension; diff --git a/www/file/app-file.less b/www/file/app-file.less index 20428b382..4ac94ca33 100644 --- a/www/file/app-file.less +++ b/www/file/app-file.less @@ -52,6 +52,16 @@ max-width: 100%; max-height: ~"calc(100vh - 96px)"; } + .plain-text-reader { + align-self: flex-start; + width: 90vw; + height: 100%; + padding: 2em; + background-color: white; + overflow-y: auto; + word-wrap: break-word; + white-space: pre-wrap; + } } #cp-app-file-upload-form, #cp-app-file-download-form {