2018-04-22 18:14:49 +00:00
|
|
|
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
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
/* global $:false */
|
2017-12-02 15:10:19 +00:00
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
class Share {
|
|
|
|
init() {
|
2018-04-22 18:20:45 +00:00
|
|
|
$(window).bind('hashchange', (e) => {
|
|
|
|
this.hashLoad();
|
|
|
|
});
|
2018-04-22 18:24:16 +00:00
|
|
|
|
2018-04-22 18:55:46 +00:00
|
|
|
let appTitle = `Share @ ${window.location.host}`;
|
2018-04-22 18:24:16 +00:00
|
|
|
$('title,.apptitle').text(appTitle);
|
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
this.hashLoad();
|
|
|
|
}
|
2017-12-02 18:11:09 +00:00
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
embedFileInfo(file = '') {
|
2017-12-02 18:11:09 +00:00
|
|
|
if (file === '') {
|
2018-04-22 18:14:49 +00:00
|
|
|
this.handleErrorMessage(MSG_NOT_FOUND);
|
2018-04-23 21:34:59 +00:00
|
|
|
return;
|
2017-12-02 18:11:09 +00:00
|
|
|
}
|
2018-04-22 18:14:49 +00:00
|
|
|
|
2017-12-02 15:10:19 +00:00
|
|
|
fileURL = file;
|
2018-04-22 18:14:49 +00:00
|
|
|
$.ajax(file, {
|
2017-12-02 15:10:19 +00:00
|
|
|
method: 'HEAD',
|
2018-04-22 18:14:49 +00:00
|
|
|
success: (data, status, xhr) => {
|
|
|
|
this.handleEmbed(data, status, xhr);
|
|
|
|
},
|
|
|
|
error: (xhr, status) => {
|
|
|
|
this.handleError(xhr, status);
|
|
|
|
},
|
2017-12-02 15:10:19 +00:00
|
|
|
});
|
2018-04-22 18:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleEmbed(data, status, xhr) {
|
|
|
|
let type = xhr.getResponseHeader('Content-Type');
|
2017-12-02 15:10:19 +00:00
|
|
|
|
2018-04-22 18:20:45 +00:00
|
|
|
$('.container').hide();
|
2017-12-02 15:10:19 +00:00
|
|
|
$('.filename').text(fileURL.substring(fileURL.lastIndexOf('/') + 1));
|
2018-04-23 21:27:53 +00:00
|
|
|
$('.filelink-href').attr('href', fileURL);
|
2018-04-22 18:14:49 +00:00
|
|
|
|
2017-12-02 15:10:19 +00:00
|
|
|
if (type.match(/^image\//)) {
|
2018-04-22 18:14:49 +00:00
|
|
|
$('.filelink-src').attr('src', fileURL);
|
2017-12-02 15:10:19 +00:00
|
|
|
$('.show-image').show();
|
|
|
|
return;
|
|
|
|
}
|
2018-04-22 18:55:46 +00:00
|
|
|
|
2017-12-02 15:10:19 +00:00
|
|
|
if (type.match(/^video\//)) {
|
2018-04-22 18:14:49 +00:00
|
|
|
let src = $('<source>');
|
|
|
|
src.attr('src', fileURL);
|
|
|
|
src.appendTo($('video'));
|
2017-12-02 15:10:19 +00:00
|
|
|
$('.show-video').show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-22 19:18:56 +00:00
|
|
|
if (type.match(/^audio\/(aac|mp3|mpeg)$/)) {
|
|
|
|
let src = $('<source>');
|
|
|
|
src.attr('src', fileURL);
|
|
|
|
src.appendTo($('audio'));
|
|
|
|
$('.show-audio').show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-22 18:55:46 +00:00
|
|
|
if (type.match(/^(text\/|application\/javascript)/)) {
|
|
|
|
$.ajax(fileURL, {
|
|
|
|
dataType: 'text',
|
|
|
|
method: 'GET',
|
|
|
|
success: (data) => {
|
|
|
|
$('code').text(data);
|
|
|
|
$('.show-text').show();
|
|
|
|
hljs.initHighlighting();
|
|
|
|
},
|
|
|
|
error: (xhr, status) => {
|
|
|
|
this.handleError(xhr, status);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
$('.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);
|
|
|
|
}
|
2017-12-02 18:11:09 +00:00
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
handleErrorMessage(message) {
|
2017-12-02 18:11:09 +00:00
|
|
|
$('.error').text(message);
|
2018-04-22 18:20:45 +00:00
|
|
|
$('.container').hide();
|
2018-04-22 18:14:49 +00:00
|
|
|
$('.show-error').show();
|
|
|
|
}
|
|
|
|
|
|
|
|
hashLoad() {
|
|
|
|
let file = window.location.hash.substring(1);
|
|
|
|
this.embedFileInfo(file);
|
|
|
|
}
|
|
|
|
}
|
2017-12-02 18:11:09 +00:00
|
|
|
|
2018-04-22 18:14:49 +00:00
|
|
|
$(function() {
|
|
|
|
let share = new Share();
|
|
|
|
share.init();
|
|
|
|
});
|