6 .directive("mediaDisplayDirective", mediaDisplayDirective);
8 function mediaDisplayDirective() {
15 templateUrl: Global.Theme.getURLPortal() + Liferay.ThemeDisplay.getPathJavaScript() + "/angularjs/media-display/media-display.html?t="+Liferay.extTimestamp,
16 controller: MediaDisplayController,
17 controllerAs: "MediaDisplayCtrl",
18 bindToController: true
22 MediaDisplayController.$inject = ["attachmentService", "toastr", "$interval", "$scope"];
24 function MediaDisplayController(attachmentService, toastr, $interval, $scope) {
26 var attachment = vm.attachment;
29 vm.conversionProgress = 0;
30 vm.isConversionInProgress = false;
32 vm.isContentReady = true;
34 vm.isDocument = false;
41 vm.description = undefined;
42 vm.id = "area" + (Math.random() * 100);
48 $scope.$on("$destroy", function() {
49 stopConversionProgress();
53 Get attachment type and return its extension
55 function getAttachmentType() {
56 return attachment.fileName.substr(attachment.fileName.lastIndexOf('.') + 1);
60 Initialise viewer url, width and height depending on the attachment type
62 function initViewer() {
63 var fileExtension = getAttachmentType().toLowerCase();
65 if (fileExtension == "epub") {
72 else if (EntSavoie.documentExtensionArray.indexOf(fileExtension) != -1) {
73 vm.url = Global.Theme.getURLPortal() + "/PDFjs/web/viewer.html?file=" + encodeURIComponent(attachment.fileLink);
77 if (fileExtension != "pdf") {
78 vm.url += "%26targetExtension%3Dpdf";
82 else if (EntSavoie.imageExtensionArray.indexOf(fileExtension) != -1) {
83 vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
86 else if (EntSavoie.audioExtensionArray.indexOf(fileExtension) != -1) {
87 vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
89 if (fileExtension != "mp3") {
90 vm.url += "&targetExtension=mp3";
91 vm.isContentReady = false;
92 initAttachmentConversionProgress();
96 else if (EntSavoie.videoExtensionArray.indexOf(fileExtension) != -1) {
97 vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
99 if (fileExtension != "mp4") {
100 vm.url += "&targetExtension=mp4";
101 vm.isContentReady = false;
102 initAttachmentConversionProgress();
107 else if (fileExtension === "html") {
108 vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
112 else if (EntSavoie.viewableExtensionArray.indexOf(fileExtension) != -1) {
115 vm.url = Global.Theme.getURLPortal() + decodeURIComponent(attachment.fileLink);
121 Get the attachment key and launch progress bar
123 function initAttachmentConversionProgress() {
124 attachmentService.getAttachmentConversionKey(attachment, vm.idProperties)
125 .then(function(data) {
126 attachment.idSecure = data.idSecure;
127 getConversionProgress();
129 ["catch"](function(message) {
130 toastr.error(message);
135 Get conversion state to refresh the progress bar
137 function getConversionProgress() {
138 vm.isConversionInProgress = true;
140 vm.conversion = $interval(function() {
141 attachmentService.getConversionProgress(attachment.idSecure)
142 .then(function(data) {
143 if (!isNaN(data.state)) {
144 vm.conversionProgress = data.state;
146 else if (data.state == "done") {
147 toastr.success(data.message);
148 vm.isContentReady = true;
149 stopConversionProgress();
152 ["catch"](function(message) {
153 toastr.error(message);
154 stopConversionProgress();
160 var reader = new FileReader();
161 reader.addEventListener("load", function() {
163 vm.epubViewer = new ePub(reader.result, { width: "400px", height: "500px", spreads: true });
164 vm.epubViewer.renderTo("area");
167 vm.description = reader.result;
174 var xhr = new XMLHttpRequest();
175 xhr.open("GET", decodeURIComponent(attachment.fileLink));
176 xhr.responseType = "blob";
177 xhr.onload = function() {
180 reader.readAsArrayBuffer(blob);
183 reader.readAsText(blob);
190 Stop interval function getting conversion progress
191 Kill conversion process if not finished properly
193 function stopConversionProgress() {
194 if (attachment.idSecure != undefined && vm.isContentReady == false) {
195 attachmentService.killConversionProcess(attachment.idSecure);
198 if (angular.isDefined(vm.conversion)) {
199 $interval.cancel(vm.conversion);
200 vm.conversion= undefined;
202 vm.isConversionInProgress = false;