123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- /**
- * @description Takes care of every action an album can handle and execute.
- * @copyright 2015 by Tobias Reich
- */
- upload = {}
- upload.show = function(title, files, callback) {
- basicModal.show({
- body: build.uploadModal(title, files),
- buttons: {
- action: {
- title: 'Close',
- class: 'hidden',
- fn: basicModal.close
- }
- },
- callback
- });
- }
- upload.notify = function(title, text) {
- var popup;
- if (!text||text==='') text = 'You can now manage your new photo(s).';
- if (!window.webkitNotifications) return false;
- if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
- if (window.webkitNotifications.checkPermission()===0&&title) {
- popup = window.webkitNotifications.createNotification('', title, text);
- popup.show();
- }
- }
- upload.start = {
- local: function(files) {
- var albumID = album.getID(),
- error = false,
- warning = false,
- process = function(files, file) {
- var formData = new FormData(),
- xhr = new XMLHttpRequest(),
- pre_progress = 0,
- progress = 0,
- finish = function() {
- window.onbeforeunload = null;
- $('#upload_files').val('');
- if (error===false&&warning===false) {
- // Success
- basicModal.close();
- upload.notify('Upload complete');
- } else if (error===false&&warning===true) {
- // Warning
- $('.basicModal #basicModal__action.hidden').show();
- upload.notify('Upload complete');
- } else {
- // Error
- $('.basicModal #basicModal__action.hidden').show();
- upload.notify('Upload complete', 'Failed to upload one or more photos.');
- }
- albums.refresh();
- if (album.getID()===false) lychee.goto('0');
- else album.load(albumID);
- };
- // Check if file is supported
- if (file.supported===false) {
- // Skip file
- if (file.next!==null) process(files, file.next);
- else {
- // Look for supported files
- // If zero files are supported, hide the upload after a delay
- var hasSupportedFiles = false;
- for (var i = 0; i < files.length; i++) {
- if (files[i].supported===true) {
- hasSupportedFiles = true;
- break;
- }
- }
- if (hasSupportedFiles===false) setTimeout(finish, 2000);
- }
- return false;
- }
- formData.append('function', 'Photo::add');
- formData.append('albumID', albumID);
- formData.append('tags', '');
- formData.append(0, file);
- xhr.open('POST', api.path);
- xhr.onload = function() {
- var wait = false,
- errorText = '';
- file.ready = true;
- // Set status
- if (xhr.status===200&&xhr.responseText==='1') {
- // Success
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
- .html('Finished')
- .addClass('success');
- } else {
- 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.';
- error = true;
- // Error Status
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
- .html('Failed')
- .addClass('error');
- } else if (xhr.responseText.substr(0, 8)==='Warning:') {
- errorText = xhr.responseText.substr(8);
- warning = true;
- // Warning Status
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
- .html('Skipped')
- .addClass('warning');
- } else {
- errorText = 'Server returned an unknown response. Please take a look at the console of your browser for further details.';
- error = true;
- // Error Status
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
- .html('Failed')
- .addClass('error');
- }
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') p.notice')
- .html(errorText)
- .show();
- // Throw error
- if (error===true) lychee.error('Upload failed. Server returned the status code ' + xhr.status + '!', xhr, xhr.responseText);
- }
- // Check if there are file which are not finished
- for (var i = 0; i < files.length; i++) {
- if (files[i].ready===false) {
- wait = true;
- break;
- }
- }
- // Finish upload when all files are finished
- if (wait===false) finish();
- };
- xhr.upload.onprogress = function(e) {
- if (e.lengthComputable) {
- // Calculate progress
- progress = (e.loaded / e.total * 100 | 0);
- // Set progress when progress has changed
- if (progress>pre_progress) {
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html(progress + '%');
- pre_progress = progress;
- }
- if (progress>=100) {
- // Scroll to the uploading file
- var scrollPos = 0;
- if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
- $('.basicModal .rows').scrollTop(scrollPos);
- // Set status to processing
- $('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html('Processing');
- // Upload next file
- if (file.next!==null) process(files, file.next);
- }
- }
- };
- xhr.send(formData);
- };
- if (files.length<=0) return false;
- if (albumID===false||visible.albums()===true) albumID = 0;
- 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;
- // 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') {
- files[i].ready = true;
- files[i].supported = false;
- }
- }
- window.onbeforeunload = function() { return 'Lychee is currently uploading!'; };
- upload.show('Uploading', files, function() {
- // Upload first file
- process(files, files[0]);
- });
- },
- url: function(url = '') {
- var albumID = album.getID(),
- url = (typeof url === 'string' ? url : ''),
- action;
- if (albumID===false) albumID = 0;
- action = function(data) {
- var extension,
- files = [];
- if (data.link&&data.link.length>3) {
- basicModal.close();
- 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
- }
- upload.show('Importing URL', files, function() {
- var params;
- $('.basicModal .rows .row .status').html('Importing');
- params = {
- url: data.link,
- albumID
- }
- api.post('Import::url', params, function(data) {
- basicModal.close();
- upload.notify('Import complete');
- albums.refresh();
- if (album.getID()===false) lychee.goto('0');
- else album.load(albumID);
- if (data!==true) lychee.error(null, params, data);
- });
- });
- } else basicModal.error('link');
- }
- 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='" + url + "'></p>",
- buttons: {
- action: {
- title: 'Import',
- fn: action
- },
- cancel: {
- title: 'Cancel',
- fn: basicModal.close
- }
- }
- });
- },
- server: function() {
- var albumID = album.getID(),
- action;
- if (albumID===false) albumID = 0;
- action = function(data) {
- var files = [];
- files[0] = {
- name: data.path,
- supported: true
- };
- upload.show('Importing from server', files, function() {
- var params;
- $('.basicModal .rows .row .status').html('Importing');
- params = {
- albumID,
- path: data.path
- }
- api.post('Import::server', params, function(data) {
- basicModal.close();
- upload.notify('Import complete');
- albums.refresh();
- 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);
- });
- });
- }
- 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
- }
- }
- });
- },
- dropbox: function() {
- var albumID = album.getID(),
- links = '',
- success;
- if (albumID===false) albumID = 0;
- success = function(files) {
- for (var i = 0; i < files.length; i++) {
- links += files[i].link + ',';
- files[i] = {
- name: files[i].link,
- supported: true
- };
- }
- // Remove last comma
- links = links.substr(0, links.length-1);
- upload.show('Importing from Dropbox', files, function() {
- var params;
- $('.basicModal .rows .row .status').html('Importing');
- params = {
- url: links,
- albumID
- }
- api.post('Import::url', params, function(data) {
- basicModal.close();
- upload.notify('Import complete');
- albums.refresh();
- if (album.getID()===false) lychee.goto('0');
- else album.load(albumID);
- if (data!==true) lychee.error(null, params, data);
- });
- });
- }
- lychee.loadDropbox(function() {
- Dropbox.choose({
- linkType: 'direct',
- multiselect: true,
- success
- });
- });
- }
- }
|