2020-01-28 12:51:21 +01:00
|
|
|
function Viewer(type, viewToken, data) {
|
2020-01-27 16:56:16 +01:00
|
|
|
// Set defaults
|
2020-01-28 12:51:21 +01:00
|
|
|
this.toolbar = null;
|
|
|
|
this.listNavigator = null;
|
|
|
|
this.detailsWindow = null;
|
|
|
|
this.divFilepreview = null;
|
|
|
|
this.currentFile = "";
|
|
|
|
this.title = ""; // Contains either the file name or list title
|
|
|
|
this.listId = "";
|
|
|
|
this.viewToken = "";
|
|
|
|
this.isList = false;
|
|
|
|
this.isFile = false;
|
|
|
|
this.initialized = false;
|
|
|
|
|
|
|
|
this.viewToken = viewToken;
|
|
|
|
this.toolbar = new Toolbar(this);
|
|
|
|
this.detailsWindow = new DetailsWindow(this);
|
|
|
|
|
|
|
|
this.divFilepreview = document.getElementById("filepreview");
|
2020-01-27 16:56:16 +01:00
|
|
|
|
|
|
|
// On small screens the toolbar takes too much space, so it collapses
|
|
|
|
// automatically
|
2020-01-28 12:51:21 +01:00
|
|
|
if (this.divFilepreview.clientWidth > 600 && !this.toolbar.visible) {
|
|
|
|
this.toolbar.toggle();
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
// The close button only works if the window has an opener. So we hide
|
|
|
|
// the button if it does not
|
|
|
|
if (window.opener === null && window.history.length !== 1) {
|
|
|
|
document.getElementById("button_close_file_viewer").remove()
|
|
|
|
}
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
if (type === "file") {
|
2020-01-28 12:51:21 +01:00
|
|
|
this.isFile = true;
|
|
|
|
this.currentFile = data.id;
|
|
|
|
this.title = data.name;
|
|
|
|
this.setFile(data);
|
2020-01-27 16:56:16 +01:00
|
|
|
} else if (type === "list") {
|
2020-01-28 12:51:21 +01:00
|
|
|
this.isList = true;
|
|
|
|
this.listId = data.id;
|
|
|
|
this.title = data.title;
|
|
|
|
this.listNavigator = new ListNavigator(this, data.files);
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 12:51:21 +01:00
|
|
|
this.renderSponsors();
|
|
|
|
window.addEventListener("resize", e => { this.renderSponsors(e); });
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
// Register keyboard shortcuts
|
2020-01-28 12:51:21 +01:00
|
|
|
document.addEventListener("keydown", e => { this.keyboardEvent(e); });
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-28 12:51:21 +01:00
|
|
|
this.initialized = true;
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-28 12:51:21 +01:00
|
|
|
Viewer.prototype.setFile = function(file) {
|
|
|
|
this.currentFile = file.id;
|
|
|
|
if (this.isList) {
|
2020-01-27 16:56:16 +01:00
|
|
|
document.getElementById("file_viewer_headerbar_title").style.lineHeight = "1em";
|
|
|
|
document.getElementById("file_viewer_list_title").innerText = this.title;
|
|
|
|
document.getElementById("file_viewer_file_title").innerText = file.name;
|
2020-01-28 12:51:21 +01:00
|
|
|
document.title = this.title + " ~ " + file.name + " ~ pixeldrain";
|
2020-01-27 16:56:16 +01:00
|
|
|
} else {
|
|
|
|
document.getElementById("file_viewer_file_title").innerText = file.name;
|
|
|
|
document.title = file.name + " ~ pixeldrain";
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
// Update the file details
|
2020-01-28 12:51:21 +01:00
|
|
|
this.detailsWindow.setDetails(file);
|
|
|
|
this.toolbar.setStats(file);
|
2020-01-27 16:56:16 +01:00
|
|
|
|
|
|
|
// Register a new view. We don't care what this returns becasue we can't
|
|
|
|
// do anything about it anyway
|
|
|
|
fetch(apiEndpoint+"/file/"+file.id+"/view",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {"Content-Type": "application/x-www-form-urlencoded"},
|
2020-01-28 12:51:21 +01:00
|
|
|
body: "token="+this.viewToken
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
2020-01-27 16:56:16 +01:00
|
|
|
);
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
// Clear the canvas
|
2020-01-28 12:51:21 +01:00
|
|
|
this.divFilepreview.innerHTML = "";
|
2020-01-27 16:56:16 +01:00
|
|
|
|
|
|
|
let nextItem = () => {
|
2020-01-28 12:51:21 +01:00
|
|
|
if (this.listNavigator !== null) {
|
|
|
|
this.listNavigator.nextItem();
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
2020-01-27 16:56:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
|
|
|
file.mime_type.startsWith("image")
|
|
|
|
) {
|
2020-01-28 12:51:21 +01:00
|
|
|
new ImageViewer(this, file).render(this.divFilepreview);
|
2020-01-27 16:56:16 +01:00
|
|
|
} else if (
|
|
|
|
file.mime_type.startsWith("video") ||
|
|
|
|
file.mime_type === "application/matroska" ||
|
|
|
|
file.mime_type === "application/x-matroska"
|
|
|
|
) {
|
2020-01-28 12:51:21 +01:00
|
|
|
new VideoViewer(this, file, nextItem).render(this.divFilepreview);
|
2020-01-27 16:56:16 +01:00
|
|
|
} else if (
|
|
|
|
file.mime_type.startsWith("audio") ||
|
|
|
|
file.mime_type === "application/ogg" ||
|
|
|
|
file.name.endsWith(".mp3")
|
|
|
|
) {
|
2020-01-28 12:51:21 +01:00
|
|
|
new AudioViewer(this, file, nextItem).render(this.divFilepreview);
|
2020-01-27 16:56:16 +01:00
|
|
|
} else if (
|
|
|
|
file.mime_type === "application/pdf" ||
|
|
|
|
file.mime_type === "application/x-pdf"
|
|
|
|
) {
|
2020-01-28 12:51:21 +01:00
|
|
|
new PDFViewer(this, file).render(this.divFilepreview);
|
2020-01-27 16:56:16 +01:00
|
|
|
} else if (
|
|
|
|
file.mime_type.startsWith("text") ||
|
|
|
|
file.id === "demo"
|
|
|
|
) {
|
2020-01-28 12:51:21 +01:00
|
|
|
new TextViewer(this, file).render(this.divFilepreview);
|
2020-01-27 16:56:16 +01:00
|
|
|
} else {
|
2020-01-28 12:51:21 +01:00
|
|
|
new FileViewer(this, file).render(this.divFilepreview);
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
Viewer.prototype.renderSponsors = function() {
|
|
|
|
let scale = 1;
|
|
|
|
let scaleWidth = 1;
|
|
|
|
let scaleHeight = 1;
|
|
|
|
let minWidth = 728;
|
|
|
|
let minHeight = 800;
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
if (window.innerWidth < minWidth) {
|
|
|
|
scaleWidth = window.innerWidth/minWidth;
|
|
|
|
}
|
|
|
|
if (window.innerHeight < minHeight) {
|
|
|
|
scaleHeight = window.innerHeight/minHeight;
|
|
|
|
}
|
|
|
|
scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
|
|
|
|
|
|
|
|
// Because of the scale transformation the automatic margins don't work
|
|
|
|
// anymore. So we have to maunally calculate the margin. Where we take the
|
|
|
|
// width of the viewport - the width of the ad to calculate the amount of
|
|
|
|
// pixels around the ad. We multiply the ad size by the scale we calcualted
|
|
|
|
// to account for the smaller size.
|
|
|
|
let offset = (window.innerWidth - (minWidth*scale)) / 2
|
|
|
|
if (offset < 0) {
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
document.querySelector(".sponsors > iframe").style.marginLeft = offset+"px";
|
|
|
|
|
|
|
|
if (scale == 1) {
|
|
|
|
document.querySelector(".sponsors > iframe").style.transform = "none";
|
|
|
|
document.querySelector(".sponsors").style.height = "90px";
|
|
|
|
} else {
|
|
|
|
document.querySelector(".sponsors > iframe").style.transform = "scale("+scale+")";
|
|
|
|
document.querySelector(".sponsors").style.height = (scale*90)+"px";
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 19:55:51 +01:00
|
|
|
|
2020-01-28 12:51:21 +01:00
|
|
|
Viewer.prototype.keyboardEvent = function(evt) {
|
2020-01-27 16:56:16 +01:00
|
|
|
if (evt.ctrlKey || evt.altKey) {
|
|
|
|
return // prevent custom shortcuts from interfering with system shortcuts
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 16:56:16 +01:00
|
|
|
switch (evt.keyCode) {
|
|
|
|
case 65: // A or left arrow key go to previous file
|
|
|
|
case 37:
|
2020-01-28 12:51:21 +01:00
|
|
|
if (this.listNavigator != null) {
|
|
|
|
this.listNavigator.previousItem();
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
2020-01-27 16:56:16 +01:00
|
|
|
break;
|
|
|
|
case 68: // D or right arrow key go to next file
|
|
|
|
case 39:
|
2020-01-28 12:51:21 +01:00
|
|
|
if (this.listNavigator != null) {
|
|
|
|
this.listNavigator.nextItem();
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 83:
|
|
|
|
if (evt.shiftKey) {
|
2020-01-28 12:51:21 +01:00
|
|
|
this.listNavigator.downloadList(); // SHIFT + S downloads all files in list
|
2020-01-27 16:56:16 +01:00
|
|
|
} else {
|
2020-01-28 12:51:21 +01:00
|
|
|
this.toolbar.download(); // S to download the current file
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
2020-01-27 16:56:16 +01:00
|
|
|
break;
|
|
|
|
case 82: // R to toggle list shuffle
|
2020-01-28 12:51:21 +01:00
|
|
|
if (this.listNavigator != null) {
|
|
|
|
this.listNavigator.toggleShuffle();
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 67: // C to copy to clipboard
|
2020-01-28 12:51:21 +01:00
|
|
|
this.toolbar.copyUrl();
|
2020-01-27 16:56:16 +01:00
|
|
|
break;
|
|
|
|
case 73: // I to open the details window
|
2020-01-28 12:51:21 +01:00
|
|
|
this.detailsWindow.toggle();
|
2020-01-27 16:56:16 +01:00
|
|
|
break;
|
|
|
|
case 81: // Q to close the window
|
|
|
|
window.close();
|
|
|
|
break;
|
2020-01-20 19:55:51 +01:00
|
|
|
}
|
2020-01-27 16:56:16 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 19:55:51 +01:00
|
|
|
|
|
|
|
// Against XSS attacks
|
|
|
|
function escapeHTML(str) {
|
|
|
|
return String(str)
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"');
|
|
|
|
}
|