1
0
Fork 0
mirror of https://github.com/Luzifer/share.git synced 2024-10-18 13:24:23 +00:00
share/frontend/app.js

89 lines
1.8 KiB
JavaScript
Raw Normal View History

let fileURL = null;
const MSG_NOT_FOUND = 'File not found';
const MSG_NOT_PERMITTED = 'Not allowed to access file';
const MSG_GENERIC_ERR = 'Something went wrong';
2017-12-02 15:10:19 +00:00
/* global $:false */
2017-12-02 15:10:19 +00:00
class Share {
init() {
$(window).bind('hashchange', (e) => {
this.hashLoad();
});
this.hashLoad();
}
embedFileInfo(file = '') {
if (file === '') {
this.handleErrorMessage(MSG_NOT_FOUND);
}
2017-12-02 15:10:19 +00:00
fileURL = file;
$.ajax(file, {
2017-12-02 15:10:19 +00:00
method: 'HEAD',
success: (data, status, xhr) => {
this.handleEmbed(data, status, xhr);
},
error: (xhr, status) => {
this.handleError(xhr, status);
},
2017-12-02 15:10:19 +00:00
});
}
handleEmbed(data, status, xhr) {
let type = xhr.getResponseHeader('Content-Type');
2017-12-02 15:10:19 +00:00
$('.container').hide();
2017-12-02 15:10:19 +00:00
$('.filename').text(fileURL.substring(fileURL.lastIndexOf('/') + 1));
2017-12-02 15:10:19 +00:00
if (type.match(/^image\//)) {
$('.filelink-src').attr('src', fileURL);
2017-12-02 15:10:19 +00:00
$('.show-image').show();
return;
}
if (type.match(/^video\//)) {
let src = $('<source>');
src.attr('src', fileURL);
src.appendTo($('video'));
2017-12-02 15:10:19 +00:00
$('.show-video').show();
return;
}
$('.filelink-href').attr('href', fileURL);
$('.show-generic').show();
}
handleError(xhr, status) {
let message = '';
switch (xhr.status) {
case 404:
message = MSG_NOT_FOUND;
break;
case 403:
message = MSG_NOT_PERMITTED;
break;
default:
message = MSG_GENERIC_ERR;
break;
}
this.handleErrorMessage(message);
}
handleErrorMessage(message) {
$('.error').text(message);
$('.container').hide();
$('.show-error').show();
}
hashLoad() {
let file = window.location.hash.substring(1);
this.embedFileInfo(file);
}
}
$(function() {
let share = new Share();
share.init();
});