You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lychee/src/scripts/upload.js

404 lines
9.2 KiB

/**
* @description Takes care of every action an album can handle and execute.
* @copyright 2014 by Tobias Reich
*/
10 years ago
upload = {}
10 years ago
upload.show = function(title, files, callback) {
10 years ago
upload.close(true);
$('body').append(build.uploadModal(title, files));
10 years ago
if (callback!==null&&callback!==undefined) callback();
10 years ago
}
11 years ago
10 years ago
upload.notify = function(title, text) {
11 years ago
10 years ago
var popup;
if (!text||text==='') text = 'You can now manage your new photo(s).';
11 years ago
10 years ago
if (!window.webkitNotifications) return false;
11 years ago
10 years ago
if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
11 years ago
10 years ago
if (window.webkitNotifications.checkPermission()===0&&title) {
popup = window.webkitNotifications.createNotification('', title, text);
10 years ago
popup.show();
}
10 years ago
}
10 years ago
upload.start = {
10 years ago
local: function(files) {
var albumID = album.getID(),
error = false,
process = function(files, file) {
var formData = new FormData(),
xhr = new XMLHttpRequest(),
pre_progress = 0,
progress = 0,
10 years ago
finish = function() {
10 years ago
window.onbeforeunload = null;
$('#upload_files').val('');
10 years ago
if (error===false) {
10 years ago
// Success
upload.close();
upload.notify('Upload complete');
10 years ago
} else {
10 years ago
// Error
$('.upload_message a.close').show();
upload.notify('Upload complete', 'Failed to upload one or more photos.');
10 years ago
}
10 years ago
albums.refresh();
if (album.getID()===false) lychee.goto('0');
10 years ago
else album.load(albumID);
10 years ago
};
10 years ago
// Check if file is supported
if (file.supported===false) {
10 years ago
// Skip file
if (file.next!==null) process(files, file.next);
else {
10 years ago
// Look for supported files
// If zero files are supported, hide the upload after a delay
10 years ago
var hasSupportedFiles = false;
10 years ago
for (var i = 0; i < files.length; i++) {
10 years ago
if (files[i].supported===true) {
hasSupportedFiles = true;
break;
}
}
10 years ago
if (hasSupportedFiles===false) setTimeout(finish, 2000);
}
10 years ago
return false;
10 years ago
}
formData.append('function', 'upload');
formData.append('albumID', albumID);
formData.append('tags', '');
10 years ago
formData.append(0, file);
xhr.open('POST', lychee.api_path);
10 years ago
xhr.onload = function() {
var wait = false,
errorText = '';
10 years ago
file.ready = true;
10 years ago
// Set status
if (xhr.status===200&&xhr.responseText==='1') {
10 years ago
// Success
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status')
.html('Finished')
.addClass('success');
10 years ago
} else {
10 years ago
// Error
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status')
.html('Error')
.addClass('error');
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.';
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') p.notice')
10 years ago
.html(errorText)
.show();
10 years ago
// Set global error
error = true;
10 years ago
// Throw error
lychee.error('Upload failed. Server returned the status code ' + xhr.status + '!', xhr, xhr.responseText);
10 years ago
}
// Check if there are file which are not finished
for (var i = 0; i < files.length; i++) {
10 years ago
if (files[i].ready===false) {
wait = true;
break;
}
10 years ago
}
10 years ago
// Finish upload when all files are finished
if (wait===false) finish();
10 years ago
};
10 years ago
xhr.upload.onprogress = function(e) {
10 years ago
if (e.lengthComputable) {
10 years ago
// Calculate progress
progress = (e.loaded / e.total * 100 | 0);
10 years ago
// Set progress when progress has changed
if (progress>pre_progress) {
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status').html(progress + '%');
10 years ago
pre_progress = progress;
}
10 years ago
if (progress>=100) {
10 years ago
// Scroll to the uploading file
var scrollPos = 0;
if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
$('.upload_message .rows').scrollTop(scrollPos);
10 years ago
// Set status to processing
$('.upload_message .rows .row:nth-child(' + (file.num+1) + ') .status').html('Processing');
10 years ago
// Upload next file
if (file.next!==null) process(files, file.next);
}
10 years ago
}
};
10 years ago
xhr.send(formData);
10 years ago
};
10 years ago
if (files.length<=0) return false;
if (albumID===false||visible.albums()===true) albumID = 0;
10 years ago
for (var i = 0; i < files.length; i++) {
files[i].num = i;
files[i].ready = false;
files[i].supported = true;
if (i < files.length-1) files[i].next = files[i+1];
else files[i].next = null;
10 years ago
// Check if file is supported
if (files[i].type!=='image/jpeg'&&files[i].type!=='image/jpg'&&files[i].type!=='image/png'&&files[i].type!=='image/gif') {
10 years ago
files[i].ready = true;
files[i].supported = false;
}
10 years ago
}
window.onbeforeunload = function() { return 'Lychee is currently uploading!'; };
upload.show('Uploading', files);
10 years ago
// Upload first file
process(files, files[0]);
10 years ago
},
10 years ago
url: function() {
10 years ago
var albumID = album.getID(),
params,
extension,
buttons,
link,
files = [];
10 years ago
if (albumID===false) albumID = 0;
10 years ago
buttons = [
['Import', function() {
link = $('.message input.text').val();
10 years ago
if (link&&link.length>3) {
10 years ago
extension = 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.');
10 years ago
return false;
}
files[0] = {
name: link,
supported: true
10 years ago
}
upload.show('Importing URL', files, function() {
$('.upload_message .rows .row .status').html('Importing');
10 years ago
});
params = 'importUrl&url=' + escape(encodeURI(link)) + '&albumID=' + albumID;
10 years ago
lychee.api(params, function(data) {
10 years ago
upload.close();
upload.notify('Import complete');
10 years ago
albums.refresh();
if (album.getID()===false) lychee.goto('0');
10 years ago
else album.load(albumID);
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
} else loadingBar.show('error', 'Link to short or too long. Please try another one!');
10 years ago
}],
['Cancel', function() {}]
10 years ago
];
11 years ago
modal.show('Import from Link', "Please enter the direct link to a photo to import it: <input class='text' type='text' placeholder='http://' value='http://'>", buttons);
10 years ago
},
10 years ago
server: function() {
10 years ago
var albumID = album.getID(),
params,
buttons,
files = [],
path;
10 years ago
if (albumID===false) albumID = 0;
10 years ago
buttons = [
['Import', function() {
path = $('.message input.text').val();
10 years ago
files[0] = {
name: path,
supported: true
10 years ago
};
upload.show('Importing from server', files, function() {
$('.upload_message .rows .row .status').html('Importing');
10 years ago
});
params = 'importServer&albumID=' + albumID + '&path=' + escape(encodeURI(path));
10 years ago
lychee.api(params, function(data) {
10 years ago
upload.close();
upload.notify('Import complete');
10 years ago
albums.refresh();
if (data==='Notice: Import only contains albums!') {
10 years ago
if (visible.albums()) lychee.load();
else lychee.goto('');
10 years ago
}
else if (album.getID()===false) lychee.goto('0');
10 years ago
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);
10 years ago
else if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}],
['Cancel', function() {}]
10 years ago
];
11 years ago
modal.show('Import from Server', "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' type='text' maxlength='100' placeholder='Absolute path to directory' value='" + lychee.location + "uploads/import/'>", buttons);
10 years ago
},
10 years ago
dropbox: function() {
10 years ago
var albumID = album.getID(),
params,
links = '';
10 years ago
if (albumID===false) albumID = 0;
10 years ago
lychee.loadDropbox(function() {
Dropbox.choose({
linkType: 'direct',
10 years ago
multiselect: true,
success: function(files) {
10 years ago
for (var i = 0; i < files.length; i++) {
links += files[i].link + ',';
10 years ago
files[i] = {
name: files[i].link,
supported: true
10 years ago
};
10 years ago
}
10 years ago
// Remove last comma
links = links.substr(0, links.length-1);
upload.show('Importing from Dropbox', files, function() {
$('.upload_message .rows .row .status').html('Importing');
10 years ago
});
params = 'importUrl&url=' + escape(links) + '&albumID=' + albumID;
10 years ago
lychee.api(params, function(data) {
10 years ago
upload.close();
upload.notify('Import complete');
10 years ago
albums.refresh();
if (album.getID()===false) lychee.goto('0');
10 years ago
else album.load(albumID);
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}
});
10 years ago
});
10 years ago
}
10 years ago
}
10 years ago
upload.close = function(force) {
10 years ago
if (force===true) {
$('.upload_overlay').remove();
10 years ago
} else {
$('.upload_overlay').removeClass('fadeIn').css('opacity', 0);
setTimeout(function() { $('.upload_overlay').remove() }, 300);
}
10 years ago
}