lychee/src/scripts/upload.js

420 lines
9.3 KiB
JavaScript
Raw Normal View History

/**
* @description Takes care of every action an album can handle and execute.
* @copyright 2014 by Tobias Reich
*/
2014-10-21 12:04:14 +00:00
upload = {}
2014-10-21 12:04:14 +00:00
upload.show = function(title, files, callback) {
2014-10-21 12:04:14 +00:00
upload.close(true);
2014-10-21 12:21:40 +00:00
$('body').append(build.uploadModal(title, files));
2014-10-21 12:04:14 +00:00
if (callback!==null&&callback!==undefined) callback();
2014-07-23 19:23:10 +00:00
2014-10-21 12:04:14 +00:00
}
2014-02-17 15:22:53 +00:00
2014-10-21 12:04:14 +00:00
upload.notify = function(title, text) {
2014-02-17 15:22:53 +00:00
2014-10-21 12:04:14 +00:00
var popup;
2014-10-21 12:21:40 +00:00
if (!text||text==='') text = 'You can now manage your new photo(s).';
2014-02-17 15:22:53 +00:00
2014-10-21 12:04:14 +00:00
if (!window.webkitNotifications) return false;
2014-02-17 15:22:53 +00:00
2014-10-21 12:04:14 +00:00
if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
2014-02-17 15:22:53 +00:00
2014-10-21 12:04:14 +00:00
if (window.webkitNotifications.checkPermission()===0&&title) {
2014-10-21 12:21:40 +00:00
popup = window.webkitNotifications.createNotification('', title, text);
2014-10-21 12:04:14 +00:00
popup.show();
}
2014-10-21 12:04:14 +00:00
}
2014-10-21 12:04:14 +00:00
upload.start = {
2014-10-21 12:04:14 +00:00
local: function(files) {
2014-10-21 12:21:40 +00:00
var albumID = album.getID(),
error = false,
process = function(files, file) {
2014-10-21 12:21:40 +00:00
var formData = new FormData(),
xhr = new XMLHttpRequest(),
pre_progress = 0,
progress = 0,
2014-10-21 12:04:14 +00:00
finish = function() {
2014-10-21 12:04:14 +00:00
window.onbeforeunload = null;
2014-10-21 12:21:40 +00:00
$('#upload_files').val('');
2014-10-21 12:04:14 +00:00
if (error===false) {
2014-10-21 12:04:14 +00:00
// Success
upload.close();
2014-10-21 12:21:40 +00:00
upload.notify('Upload complete');
2014-10-21 12:04:14 +00:00
} else {
2014-10-21 12:04:14 +00:00
// Error
2014-10-21 12:21:40 +00:00
$('.upload_message a.close').show();
upload.notify('Upload complete', 'Failed to upload one or more photos.');
2014-10-21 12:04:14 +00:00
}
2014-09-19 21:14:53 +00:00
2014-10-21 12:04:14 +00:00
albums.refresh();
2014-10-21 12:21:40 +00:00
if (album.getID()===false) lychee.goto('0');
2014-10-21 12:04:14 +00:00
else album.load(albumID);
2014-10-21 12:04:14 +00:00
};
2014-07-26 14:31:38 +00:00
2014-10-21 12:04:14 +00:00
// Check if file is supported
if (file.supported===false) {
2014-10-21 12:04:14 +00:00
// Skip file
if (file.next!==null) process(files, file.next);
else {
2014-10-21 12:04:14 +00:00
// Look for supported files
// If zero files are supported, hide the upload after a delay
2014-10-21 12:04:14 +00:00
var hasSupportedFiles = false;
2014-10-21 12:04:14 +00:00
for (var i = 0; i < files.length; i++) {
2014-10-21 12:04:14 +00:00
if (files[i].supported===true) {
hasSupportedFiles = true;
break;
}
}
2014-10-21 12:04:14 +00:00
if (hasSupportedFiles===false) setTimeout(finish, 2000);
2014-07-26 14:31:38 +00:00
}
2014-10-21 12:04:14 +00:00
return false;
2014-10-21 12:04:14 +00:00
}
2014-10-21 12:21:40 +00:00
formData.append('function', 'upload');
formData.append('albumID', albumID);
formData.append('tags', '');
2014-10-21 12:04:14 +00:00
formData.append(0, file);
2014-10-21 12:21:40 +00:00
xhr.open('POST', lychee.api_path);
2014-10-21 12:04:14 +00:00
xhr.onload = function() {
2014-10-21 12:21:40 +00:00
var wait = false,
errorText = '';
2014-08-16 17:51:21 +00:00
2014-10-21 12:04:14 +00:00
file.ready = true;
2014-10-21 12:04:14 +00:00
// Set status
2014-10-21 12:21:40 +00:00
if (xhr.status===200&&xhr.responseText==='1') {
2014-10-21 12:04:14 +00:00
// Success
2014-10-21 12:21:40 +00:00
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status')
.html('Finished')
.addClass('success');
2014-10-21 12:04:14 +00:00
} else {
2014-10-21 12:04:14 +00:00
// Error
2014-10-21 12:21:40 +00:00
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status')
.html('Error')
.addClass('error');
2014-10-21 12:21:40 +00:00
if (xhr.responseText.substr(0, 6)==='Error:') errorText = xhr.responseText.substr(6) + ' Please take a look at the console of your browser for further details.';
else errorText = 'Server returned an unknown response. Please take a look at the console of your browser for further details.';
2014-10-21 12:21:40 +00:00
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') p.notice')
2014-10-21 12:04:14 +00:00
.html(errorText)
.show();
2014-08-16 18:18:37 +00:00
2014-10-21 12:04:14 +00:00
// Set global error
error = true;
2014-10-21 12:04:14 +00:00
// Throw error
2014-10-21 12:21:40 +00:00
lychee.error('Upload failed. Server returned the status code ' + xhr.status + '!', xhr, xhr.responseText);
2014-10-21 12:04:14 +00:00
}
// Check if there are file which are not finished
for (var i = 0; i < files.length; i++) {
2014-10-21 12:04:14 +00:00
if (files[i].ready===false) {
wait = true;
break;
2014-05-11 22:15:01 +00:00
}
2014-10-21 12:04:14 +00:00
}
2014-08-16 17:51:21 +00:00
2014-10-21 12:04:14 +00:00
// Finish upload when all files are finished
if (wait===false) finish();
2014-10-21 12:04:14 +00:00
};
2014-10-21 12:04:14 +00:00
xhr.upload.onprogress = function(e) {
2014-10-21 12:04:14 +00:00
if (e.lengthComputable) {
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
// Calculate progress
progress = (e.loaded / e.total * 100 | 0);
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
// Set progress when progress has changed
if (progress>pre_progress) {
2014-10-21 12:21:40 +00:00
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status').html(progress + '%');
2014-10-21 12:04:14 +00:00
pre_progress = progress;
}
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
if (progress>=100) {
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
// Scroll to the uploading file
var scrollPos = 0;
if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
2014-10-21 12:21:40 +00:00
$('.upload_message .rows').scrollTop(scrollPos);
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
// Set status to processing
2014-10-21 12:21:40 +00:00
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status').html('Processing');
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
// Upload next file
if (file.next!==null) process(files, file.next);
}
2014-10-21 12:04:14 +00:00
}
2014-05-11 22:15:01 +00:00
};
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
xhr.send(formData);
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
};
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
if (files.length<=0) return false;
if (albumID===false||visible.albums()===true) albumID = 0;
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
for (var i = 0; i < files.length; i++) {
2014-05-11 22:15:01 +00:00
2014-10-21 12:21:40 +00:00
files[i].num = i;
files[i].ready = false;
files[i].supported = true;
2014-05-11 22:15:01 +00:00
2014-10-21 12:21:40 +00:00
if (i < files.length-1) files[i].next = files[i+1];
else files[i].next = null;
2014-05-11 22:15:01 +00:00
2014-10-21 12:04:14 +00:00
// Check if file is supported
2014-10-21 12:21:40 +00:00
if (files[i].type!=='image/jpeg'&&files[i].type!=='image/jpg'&&files[i].type!=='image/png'&&files[i].type!=='image/gif') {
2014-10-21 12:04:14 +00:00
2014-10-21 12:21:40 +00:00
files[i].ready = true;
files[i].supported = false;
2014-05-11 22:15:01 +00:00
}
2014-10-21 12:04:14 +00:00
}
2014-10-21 12:21:40 +00:00
window.onbeforeunload = function() { return 'Lychee is currently uploading!'; };
2014-10-21 12:21:40 +00:00
upload.show('Uploading', files);
2014-10-21 12:04:14 +00:00
// Upload first file
process(files, files[0]);
2014-10-21 12:04:14 +00:00
},
2014-10-21 12:04:14 +00:00
url: function() {
2014-10-21 12:04:14 +00:00
var albumID = album.getID(),
action;
2014-10-21 12:04:14 +00:00
if (albumID===false) albumID = 0;
action = function(data) {
var params,
extension,
files = [];
basicModal.close();
if (data.link&&data.link.length>3) {
2014-10-21 12:04:14 +00:00
extension = data.link.split('.').pop();
if (extension!=='jpeg'&&extension!=='jpg'&&extension!=='png'&&extension!=='gif'&&extension!=='webp') {
loadingBar.show('error', 'The file format of this link is not supported.');
return false;
}
files[0] = {
name: data.link,
supported: true
}
2014-07-18 15:27:45 +00:00
upload.show('Importing URL', files, function() {
$('.upload_message .rows .row .status').html('Importing');
});
params = 'importUrl&url=' + escape(encodeURI(data.link)) + '&albumID=' + albumID;
lychee.api(params, function(data) {
upload.close();
upload.notify('Import complete');
2014-09-19 21:14:53 +00:00
albums.refresh();
if (album.getID()===false) lychee.goto('0');
else album.load(albumID);
if (data!==true) lychee.error(null, params, data);
});
} else loadingBar.show('error', 'Link to short or too long. Please try another one!');
2014-02-17 15:22:53 +00:00
}
basicModal.show({
body: "<p>Please enter the direct link to a photo to import it: <input class='text' data-name='link' type='text' placeholder='http://' value='http://'></p>",
buttons: {
action: {
title: 'Import',
fn: action
},
cancel: {
title: 'Cancel',
fn: basicModal.close
}
}
});
2014-10-21 12:04:14 +00:00
},
2014-10-21 12:04:14 +00:00
server: function() {
2014-10-21 12:04:14 +00:00
var albumID = album.getID(),
action;
2014-10-21 12:04:14 +00:00
if (albumID===false) albumID = 0;
action = function(data) {
var params,
files = [];
2014-07-23 19:58:41 +00:00
files[0] = {
name: data.path,
supported: true
};
upload.show('Importing from server', files, function() {
$('.upload_message .rows .row .status').html('Importing');
});
params = 'importServer&albumID=' + albumID + '&path=' + escape(encodeURI(data.path));
lychee.api(params, function(data) {
upload.close();
upload.notify('Import complete');
albums.refresh();
2014-09-19 21:14:53 +00:00
if (data==='Notice: Import only contains albums!') {
if (visible.albums()) lychee.load();
else lychee.goto('');
}
else if (album.getID()===false) lychee.goto('0');
else album.load(albumID);
if (data==='Notice: Import only contains albums!') return true;
else if (data==='Warning: Folder empty!') lychee.error('Folder empty. No photos imported!', params, data);
else if (data!==true) lychee.error(null, params, data);
});
}
2014-02-17 15:22:53 +00:00
basicModal.show({
body: "<p>This action will import all photos, folders and sub-folders which are located in the following directory. The <b>original files will be deleted</b> after the import when possible. <input class='text' data-name='path' type='text' maxlength='100' placeholder='Absolute path to directory' value='" + lychee.location + "uploads/import/'></p>",
buttons: {
action: {
title: 'Import',
fn: action
},
cancel: {
title: 'Cancel',
fn: basicModal.close
}
}
});
2014-10-21 12:04:14 +00:00
},
2014-10-21 12:04:14 +00:00
dropbox: function() {
2014-10-21 12:04:14 +00:00
var albumID = album.getID(),
params,
2014-10-21 12:21:40 +00:00
links = '';
2014-10-21 12:04:14 +00:00
if (albumID===false) albumID = 0;
2014-10-21 12:04:14 +00:00
lychee.loadDropbox(function() {
Dropbox.choose({
2014-10-21 12:21:40 +00:00
linkType: 'direct',
2014-10-21 12:04:14 +00:00
multiselect: true,
success: function(files) {
2014-10-21 12:04:14 +00:00
for (var i = 0; i < files.length; i++) {
2014-10-21 12:21:40 +00:00
links += files[i].link + ',';
2014-10-21 12:04:14 +00:00
files[i] = {
2014-10-21 12:21:40 +00:00
name: files[i].link,
supported: true
2014-10-21 12:04:14 +00:00
};
2014-10-21 12:04:14 +00:00
}
2014-10-21 12:04:14 +00:00
// Remove last comma
links = links.substr(0, links.length-1);
2014-10-21 12:21:40 +00:00
upload.show('Importing from Dropbox', files, function() {
$('.upload_message .rows .row .status').html('Importing');
2014-10-21 12:04:14 +00:00
});
2014-10-21 12:21:40 +00:00
params = 'importUrl&url=' + escape(links) + '&albumID=' + albumID;
2014-10-21 12:04:14 +00:00
lychee.api(params, function(data) {
2014-10-21 12:04:14 +00:00
upload.close();
2014-10-21 12:21:40 +00:00
upload.notify('Import complete');
2014-10-21 12:04:14 +00:00
albums.refresh();
2014-09-19 21:14:53 +00:00
2014-10-21 12:21:40 +00:00
if (album.getID()===false) lychee.goto('0');
2014-10-21 12:04:14 +00:00
else album.load(albumID);
2014-10-21 12:04:14 +00:00
if (data!==true) lychee.error(null, params, data);
2014-10-21 12:04:14 +00:00
});
2014-10-21 12:04:14 +00:00
}
});
2014-10-21 12:04:14 +00:00
});
2014-10-21 12:04:14 +00:00
}
2014-10-21 12:04:14 +00:00
}
2014-10-21 12:04:14 +00:00
upload.close = function(force) {
2014-10-21 12:04:14 +00:00
if (force===true) {
2014-10-21 12:21:40 +00:00
$('.upload_overlay').remove();
2014-10-21 12:04:14 +00:00
} else {
2014-10-21 12:21:40 +00:00
$('.upload_overlay').removeClass('fadeIn').css('opacity', 0);
setTimeout(function() { $('.upload_overlay').remove() }, 300);
}
2014-10-21 12:04:14 +00:00
}