From dcbefcc1c3933ab1c5da695a27574aaea1a599e6 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Wed, 12 Jun 2019 05:29:19 +0300 Subject: [PATCH] Use blob for previews --- js/privatebin.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/js/privatebin.js b/js/privatebin.js index dcd0b8a2..770bc8ed 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -2547,7 +2547,8 @@ jQuery.PrivateBin = (function($, RawDeflate) { $attachmentLink.attr('download', fileName); } - me.handleAttachmentPreview($attachmentPreview, attachmentData); + //me.handleAttachmentPreview($attachmentPreview, attachmentData); + me.handleBlobAttachmentPreview($attachmentPreview, blobUrl, mediaType); }; /** @@ -2819,6 +2820,73 @@ jQuery.PrivateBin = (function($, RawDeflate) { } }; + /** + * handle the preview of files decoded to blob that can either be an image, video, audio or pdf element + * + * @name AttachmentViewer.handleBlobAttachmentPreview + * @function + * @argument {jQuery} $targetElement element where the preview should be appended + * @argument {string} file as a blob URL + * @argument {string} mime type + */ + me.handleBlobAttachmentPreview = function ($targetElement, blobUrl, mimeType) { + if (blobUrl) { + attachmentHasPreview = true; + if (mimeType.match(/image\//i)) { + $targetElement.html( + $(document.createElement('img')) + .attr('src', blobUrl) + .attr('class', 'img-thumbnail') + ); + } else if (mimeType.match(/video\//i)) { + $targetElement.html( + $(document.createElement('video')) + .attr('controls', 'true') + .attr('autoplay', 'true') + .attr('class', 'img-thumbnail') + + .append($(document.createElement('source')) + .attr('type', mimeType) + .attr('src', blobUrl)) + ); + } else if (mimeType.match(/audio\//i)) { + $targetElement.html( + $(document.createElement('audio')) + .attr('controls', 'true') + .attr('autoplay', 'true') + + .append($(document.createElement('source')) + .attr('type', mimeType) + .attr('src', blobUrl)) + ); + } else if (mimeType.match(/\/pdf/i)) { + // PDFs are only displayed if the filesize is smaller than about 1MB (after base64 encoding). + // Bigger filesizes currently cause crashes in various browsers. + // See also: https://code.google.com/p/chromium/issues/detail?id=69227 + + // Firefox crashes with files that are about 1.5MB + // The performance with 1MB files is bearable + if (data.length > 1398488) { + Alert.showError('File too large, to display a preview. Please download the attachment.'); //TODO: is this error really neccessary? + return; + } + + // Fallback for browsers, that don't support the vh unit + var clientHeight = $(window).height(); + + $targetElement.html( + $(document.createElement('embed')) + .attr('src', blobUrl) + .attr('type', 'application/pdf') + .attr('class', 'pdfPreview') + .css('height', clientHeight) + ); + } else { + attachmentHasPreview = false; + } + } + }; + /** * attaches the file attachment drag & drop handler to the page *