7987 |
30 Jun 21 |
nicklas |
/* $Id $ |
7987 |
30 Jun 21 |
nicklas |
2 |
------------------------------------------------------------------ |
7987 |
30 Jun 21 |
nicklas |
Copyright (C) 2021 Nicklas Nordborg |
7987 |
30 Jun 21 |
nicklas |
4 |
|
7987 |
30 Jun 21 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
7987 |
30 Jun 21 |
nicklas |
Available at http://base.thep.lu.se/ |
7987 |
30 Jun 21 |
nicklas |
7 |
|
7987 |
30 Jun 21 |
nicklas |
BASE is free software; you can redistribute it and/or |
7987 |
30 Jun 21 |
nicklas |
modify it under the terms of the GNU General Public License |
7987 |
30 Jun 21 |
nicklas |
as published by the Free Software Foundation; either version 3 |
7987 |
30 Jun 21 |
nicklas |
of the License, or (at your option) any later version. |
7987 |
30 Jun 21 |
nicklas |
12 |
|
7987 |
30 Jun 21 |
nicklas |
BASE is distributed in the hope that it will be useful, |
7987 |
30 Jun 21 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
7987 |
30 Jun 21 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7987 |
30 Jun 21 |
nicklas |
GNU General Public License for more details. |
7987 |
30 Jun 21 |
nicklas |
17 |
|
7987 |
30 Jun 21 |
nicklas |
You should have received a copy of the GNU General Public License |
7987 |
30 Jun 21 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
7987 |
30 Jun 21 |
nicklas |
20 |
------------------------------------------------------------------ |
7987 |
30 Jun 21 |
nicklas |
21 |
|
7987 |
30 Jun 21 |
nicklas |
@author Nicklas |
7987 |
30 Jun 21 |
nicklas |
23 |
*/ |
7987 |
30 Jun 21 |
nicklas |
'use strict'; |
7987 |
30 Jun 21 |
nicklas |
25 |
|
7987 |
30 Jun 21 |
nicklas |
var Upload = function() |
7987 |
30 Jun 21 |
nicklas |
27 |
{ |
7987 |
30 Jun 21 |
nicklas |
var upload = {}; |
8013 |
13 Aug 21 |
nicklas |
var zipFormats = []; |
7987 |
30 Jun 21 |
nicklas |
30 |
|
7988 |
30 Jun 21 |
nicklas |
var filesAndFolders; |
7988 |
30 Jun 21 |
nicklas |
var currentFolder; |
7988 |
30 Jun 21 |
nicklas |
33 |
|
7988 |
30 Jun 21 |
nicklas |
var files = []; |
7987 |
30 Jun 21 |
nicklas |
var uploadRequest; |
7991 |
01 Jul 21 |
nicklas |
var uploadAborted = false; |
7987 |
30 Jun 21 |
nicklas |
37 |
|
7987 |
30 Jun 21 |
nicklas |
var totalFiles = 0; |
7987 |
30 Jun 21 |
nicklas |
var currentFile = 0; |
7990 |
01 Jul 21 |
nicklas |
var uploadedFiles = 0; |
7990 |
01 Jul 21 |
nicklas |
var skippedFiles = 0; |
7987 |
30 Jun 21 |
nicklas |
42 |
|
7991 |
01 Jul 21 |
nicklas |
var uploadStartTime; |
7987 |
30 Jun 21 |
nicklas |
var totalBytesToUpload = 0; |
7987 |
30 Jun 21 |
nicklas |
var bytesAlreadyUploaded = 0; |
7990 |
01 Jul 21 |
nicklas |
var skippedBytes = 0; |
7987 |
30 Jun 21 |
nicklas |
47 |
|
7987 |
30 Jun 21 |
nicklas |
48 |
/** |
7987 |
30 Jun 21 |
nicklas |
Initialize the page. |
7987 |
30 Jun 21 |
nicklas |
50 |
*/ |
7987 |
30 Jun 21 |
nicklas |
upload.initPage = function() |
7987 |
30 Jun 21 |
nicklas |
52 |
{ |
8013 |
13 Aug 21 |
nicklas |
zipFormats = Data.json('page-data', 'zip-formats'); |
8013 |
13 Aug 21 |
nicklas |
54 |
|
7990 |
01 Jul 21 |
nicklas |
Buttons.addClickHandler('btnUpload', upload.beginFileUpload); |
7990 |
01 Jul 21 |
nicklas |
Buttons.addClickHandler('close', App.closeWindow); |
7987 |
30 Jun 21 |
nicklas |
Buttons.addClickHandler('btnAbort', upload.abortUpload); |
7987 |
30 Jun 21 |
nicklas |
Buttons.addClickHandler('failClose', App.closeWindow); |
7987 |
30 Jun 21 |
nicklas |
59 |
|
7988 |
30 Jun 21 |
nicklas |
filesAndFolders = window.opener.FileManager.getFilesToUpload(); |
7988 |
30 Jun 21 |
nicklas |
upload.processFilesAndFolders(); |
7988 |
30 Jun 21 |
nicklas |
62 |
} |
7988 |
30 Jun 21 |
nicklas |
63 |
|
8013 |
13 Aug 21 |
nicklas |
64 |
/** |
8013 |
13 Aug 21 |
nicklas |
Get the ID of the best matching compressed format |
8013 |
13 Aug 21 |
nicklas |
66 |
*/ |
8013 |
13 Aug 21 |
nicklas |
upload.getCompressedFormat = function(filename) |
8013 |
13 Aug 21 |
nicklas |
68 |
{ |
8013 |
13 Aug 21 |
nicklas |
var longestMatch = 0; |
8013 |
13 Aug 21 |
nicklas |
var bestFormat = null; |
8013 |
13 Aug 21 |
nicklas |
71 |
|
8013 |
13 Aug 21 |
nicklas |
for (var formatNo = 0; formatNo < zipFormats.length; formatNo++) |
8013 |
13 Aug 21 |
nicklas |
73 |
{ |
8013 |
13 Aug 21 |
nicklas |
var zf = zipFormats[formatNo]; |
8013 |
13 Aug 21 |
nicklas |
for (var extNo = 0; extNo < zf.extensions.length; extNo++) |
8013 |
13 Aug 21 |
nicklas |
76 |
{ |
8013 |
13 Aug 21 |
nicklas |
var ext = '.' + zf.extensions[extNo]; |
8013 |
13 Aug 21 |
nicklas |
var index = filename.lastIndexOf(ext); |
8013 |
13 Aug 21 |
nicklas |
if (index > 0 && (index + ext.length == filename.length)) |
8013 |
13 Aug 21 |
nicklas |
80 |
{ |
8013 |
13 Aug 21 |
nicklas |
if (ext.length > longestMatch) |
8013 |
13 Aug 21 |
nicklas |
82 |
{ |
8013 |
13 Aug 21 |
nicklas |
longestMatch = ext.length; |
8013 |
13 Aug 21 |
nicklas |
bestFormat = zf; |
8013 |
13 Aug 21 |
nicklas |
85 |
} |
8013 |
13 Aug 21 |
nicklas |
86 |
} |
8013 |
13 Aug 21 |
nicklas |
87 |
} |
8013 |
13 Aug 21 |
nicklas |
88 |
} |
8013 |
13 Aug 21 |
nicklas |
return bestFormat; |
8013 |
13 Aug 21 |
nicklas |
90 |
} |
8013 |
13 Aug 21 |
nicklas |
91 |
|
7990 |
01 Jul 21 |
nicklas |
var currentDirectoryReader = null; |
7988 |
30 Jun 21 |
nicklas |
93 |
/* |
7988 |
30 Jun 21 |
nicklas |
Process all selected items (files and folder) and |
7988 |
30 Jun 21 |
nicklas |
put them in the 'files[]' upload queue. |
7988 |
30 Jun 21 |
nicklas |
96 |
*/ |
7988 |
30 Jun 21 |
nicklas |
upload.processFilesAndFolders = function() |
7988 |
30 Jun 21 |
nicklas |
98 |
{ |
7988 |
30 Jun 21 |
nicklas |
if (filesAndFolders.length == 0) |
7987 |
30 Jun 21 |
nicklas |
100 |
{ |
7988 |
30 Jun 21 |
nicklas |
// Nothing more to process -- start upload |
7990 |
01 Jul 21 |
nicklas |
upload.summarizePreProcessing(); |
7988 |
30 Jun 21 |
nicklas |
return; |
7987 |
30 Jun 21 |
nicklas |
104 |
} |
7988 |
30 Jun 21 |
nicklas |
105 |
|
7988 |
30 Jun 21 |
nicklas |
// Get and remove the first entry from the process queue |
7988 |
30 Jun 21 |
nicklas |
// The filesAndFolder should contain 'FileSystemFileEntry' |
7988 |
30 Jun 21 |
nicklas |
// and 'FileSystemDirectoryEntry' object |
7988 |
30 Jun 21 |
nicklas |
var ff = filesAndFolders.splice(0, 1)[0]; |
7988 |
30 Jun 21 |
nicklas |
currentFolder = ff.subfolder || ''; |
8013 |
13 Aug 21 |
nicklas |
// Ignore files/directories inside subfolders starting with '.' unless |
8013 |
13 Aug 21 |
nicklas |
// the top folder that was selected is also hidden |
8013 |
13 Aug 21 |
nicklas |
var ignore = ff.name.charAt(0)=='.' && (currentFolder.length > 0 && currentFolder.charAt(0)!='.'); |
8013 |
13 Aug 21 |
nicklas |
if (!ignore) |
7988 |
30 Jun 21 |
nicklas |
115 |
{ |
8013 |
13 Aug 21 |
nicklas |
if (ff.isFile) |
8013 |
13 Aug 21 |
nicklas |
117 |
{ |
8013 |
13 Aug 21 |
nicklas |
// Get the file item as a 'File' object (its an async operation) |
8013 |
13 Aug 21 |
nicklas |
ff.file(upload.gotFile); |
8013 |
13 Aug 21 |
nicklas |
return; |
8013 |
13 Aug 21 |
nicklas |
121 |
} |
8013 |
13 Aug 21 |
nicklas |
else if (ff.isDirectory) |
8013 |
13 Aug 21 |
nicklas |
123 |
{ |
8013 |
13 Aug 21 |
nicklas |
// Save the currentFolder for later use in 'upload.gotFolder' |
8013 |
13 Aug 21 |
nicklas |
currentFolder += ff.name+'/'; |
8013 |
13 Aug 21 |
nicklas |
// Read files and subfolders (its an async operation) |
8013 |
13 Aug 21 |
nicklas |
currentDirectoryReader = ff.createReader(); |
8013 |
13 Aug 21 |
nicklas |
currentDirectoryReader.readEntries(upload.gotFolder); |
8013 |
13 Aug 21 |
nicklas |
return; |
8013 |
13 Aug 21 |
nicklas |
130 |
} |
7988 |
30 Jun 21 |
nicklas |
131 |
} |
8013 |
13 Aug 21 |
nicklas |
132 |
|
8013 |
13 Aug 21 |
nicklas |
// Process next entry |
8013 |
13 Aug 21 |
nicklas |
upload.processFilesAndFolders(); |
7987 |
30 Jun 21 |
nicklas |
135 |
} |
7987 |
30 Jun 21 |
nicklas |
136 |
|
7988 |
30 Jun 21 |
nicklas |
// We have a File item that should be upload |
7988 |
30 Jun 21 |
nicklas |
// Store and continue processing |
7988 |
30 Jun 21 |
nicklas |
upload.gotFile = function(file) |
7988 |
30 Jun 21 |
nicklas |
140 |
{ |
7988 |
30 Jun 21 |
nicklas |
file.subfolder = currentFolder; |
7988 |
30 Jun 21 |
nicklas |
files[files.length] = file; |
7988 |
30 Jun 21 |
nicklas |
upload.processFilesAndFolders(); |
7988 |
30 Jun 21 |
nicklas |
144 |
} |
7988 |
30 Jun 21 |
nicklas |
145 |
|
7988 |
30 Jun 21 |
nicklas |
// We have a list of more 'FileSystemFileEntry'/'FileSystemDirectoryEntry' |
7988 |
30 Jun 21 |
nicklas |
// object. Add them to the processing queue and continue with the next element |
7988 |
30 Jun 21 |
nicklas |
upload.gotFolder = function(moreFilesAndFolders) |
7988 |
30 Jun 21 |
nicklas |
149 |
{ |
7990 |
01 Jul 21 |
nicklas |
if (moreFilesAndFolders.length == 0) |
7990 |
01 Jul 21 |
nicklas |
151 |
{ |
7990 |
01 Jul 21 |
nicklas |
currentDirectoryReader = null; |
7990 |
01 Jul 21 |
nicklas |
upload.processFilesAndFolders(); |
7990 |
01 Jul 21 |
nicklas |
return; |
7990 |
01 Jul 21 |
nicklas |
155 |
} |
7988 |
30 Jun 21 |
nicklas |
for (var i = 0; i < moreFilesAndFolders.length; i++) |
7988 |
30 Jun 21 |
nicklas |
157 |
{ |
7988 |
30 Jun 21 |
nicklas |
var item = moreFilesAndFolders[i]; |
7988 |
30 Jun 21 |
nicklas |
item.subfolder = currentFolder; |
7988 |
30 Jun 21 |
nicklas |
filesAndFolders[filesAndFolders.length] = item; |
7988 |
30 Jun 21 |
nicklas |
161 |
} |
7990 |
01 Jul 21 |
nicklas |
// We need to repeat this call until we get an empty result, since |
7990 |
01 Jul 21 |
nicklas |
// some browsers (eg. chrome/edge) return a subset for each call. |
7990 |
01 Jul 21 |
nicklas |
currentDirectoryReader.readEntries(upload.gotFolder); |
7988 |
30 Jun 21 |
nicklas |
165 |
} |
7988 |
30 Jun 21 |
nicklas |
166 |
|
7990 |
01 Jul 21 |
nicklas |
upload.summarizePreProcessing = function() |
7988 |
30 Jun 21 |
nicklas |
168 |
{ |
7988 |
30 Jun 21 |
nicklas |
if (files.length > 0) |
7988 |
30 Jun 21 |
nicklas |
170 |
{ |
7990 |
01 Jul 21 |
nicklas |
files.sort(upload.sortFilesByFolder); |
8013 |
13 Aug 21 |
nicklas |
var html = '<table class="filelist" style="width: 100%;">'; |
7990 |
01 Jul 21 |
nicklas |
var lastSubfolder = ''; |
7990 |
01 Jul 21 |
nicklas |
var pad = '<td colspan="2" class="name">'; |
7990 |
01 Jul 21 |
nicklas |
var numFolders = 1; |
8013 |
13 Aug 21 |
nicklas |
var numCompressedFiles = 0; |
7988 |
30 Jun 21 |
nicklas |
for (var i = 0; i < files.length; i++) |
7988 |
30 Jun 21 |
nicklas |
178 |
{ |
7988 |
30 Jun 21 |
nicklas |
var f = files[i]; |
7988 |
30 Jun 21 |
nicklas |
totalBytesToUpload += f.size; |
7988 |
30 Jun 21 |
nicklas |
totalFiles++; |
8013 |
13 Aug 21 |
nicklas |
if (upload.getCompressedFormat(f.name)) numCompressedFiles++; |
7990 |
01 Jul 21 |
nicklas |
if (totalFiles <= 500) |
7990 |
01 Jul 21 |
nicklas |
184 |
{ |
7990 |
01 Jul 21 |
nicklas |
if (f.subfolder != lastSubfolder) |
7990 |
01 Jul 21 |
nicklas |
186 |
{ |
7990 |
01 Jul 21 |
nicklas |
html += '<tr class="folder"><td colspan="3">'+Strings.encodeTags(f.subfolder)+'</td></tr>'; |
7990 |
01 Jul 21 |
nicklas |
lastSubfolder = f.subfolder; |
7990 |
01 Jul 21 |
nicklas |
pad = '<td class="pad"></td><td class="name">'; |
7990 |
01 Jul 21 |
nicklas |
numFolders++; |
7990 |
01 Jul 21 |
nicklas |
191 |
} |
7990 |
01 Jul 21 |
nicklas |
html += '<tr class="file">'+pad+Strings.encodeTags(f.name)+'</td><td class="size">'+Numbers.formatBytes(f.size)+'</td></tr>'; |
7990 |
01 Jul 21 |
nicklas |
193 |
} |
7988 |
30 Jun 21 |
nicklas |
194 |
} |
7990 |
01 Jul 21 |
nicklas |
if (totalFiles > 500) |
7990 |
01 Jul 21 |
nicklas |
196 |
{ |
7990 |
01 Jul 21 |
nicklas |
html += '<tr><td colspan="3" class="morefiles">+'+(totalFiles-500)+' more files...</td></tr>'; |
7990 |
01 Jul 21 |
nicklas |
198 |
} |
8013 |
13 Aug 21 |
nicklas |
if (numCompressedFiles > 0) Doc.show('unpackSection'); |
7988 |
30 Jun 21 |
nicklas |
var numFilesMsg = totalFiles==1?'1 file' : totalFiles+' files'; |
7988 |
30 Jun 21 |
nicklas |
document.title = 'Uploading '+numFilesMsg+' to '+Data.get('page-data', 'directory-path'); |
7990 |
01 Jul 21 |
nicklas |
Doc.element('mainTitle').innerHTML = document.title; |
7990 |
01 Jul 21 |
nicklas |
Doc.element('progressTitle').innerHTML = document.title; |
7988 |
30 Jun 21 |
nicklas |
Doc.element('totalBytes').innerHTML = Numbers.formatBytes(totalBytesToUpload); |
7990 |
01 Jul 21 |
nicklas |
205 |
|
7990 |
01 Jul 21 |
nicklas |
Doc.element('allFiles').innerHTML = html; |
7990 |
01 Jul 21 |
nicklas |
Doc.removeClass('btnUpload', 'disabled'); |
7988 |
30 Jun 21 |
nicklas |
208 |
} |
7988 |
30 Jun 21 |
nicklas |
else |
7988 |
30 Jun 21 |
nicklas |
210 |
{ |
7988 |
30 Jun 21 |
nicklas |
upload.displayError({'message': 'There are no files to upload'}); |
7988 |
30 Jun 21 |
nicklas |
212 |
} |
7988 |
30 Jun 21 |
nicklas |
213 |
} |
7988 |
30 Jun 21 |
nicklas |
214 |
|
7990 |
01 Jul 21 |
nicklas |
upload.sortFilesByFolder = function(f1, f2) |
7990 |
01 Jul 21 |
nicklas |
216 |
{ |
7990 |
01 Jul 21 |
nicklas |
if (f1.subfolder == f2.subfolder) return f1.name < f2.name ? -1 : 1; |
7990 |
01 Jul 21 |
nicklas |
return f1.subfolder < f2.subfolder ? -1 : 1; |
7990 |
01 Jul 21 |
nicklas |
219 |
} |
7990 |
01 Jul 21 |
nicklas |
220 |
|
7990 |
01 Jul 21 |
nicklas |
// Start the file uploading |
7990 |
01 Jul 21 |
nicklas |
upload.beginFileUpload = function() |
7990 |
01 Jul 21 |
nicklas |
223 |
{ |
7990 |
01 Jul 21 |
nicklas |
Doc.hide('preUploadForm'); |
7990 |
01 Jul 21 |
nicklas |
Doc.show('progressForm'); |
7991 |
01 Jul 21 |
nicklas |
uploadStartTime = new Date().getTime(); |
7990 |
01 Jul 21 |
nicklas |
upload.uploadNextFile(); |
7990 |
01 Jul 21 |
nicklas |
upload.sendProgressUpdateRequest(); |
7990 |
01 Jul 21 |
nicklas |
229 |
} |
7990 |
01 Jul 21 |
nicklas |
230 |
|
7990 |
01 Jul 21 |
nicklas |
var file; |
7987 |
30 Jun 21 |
nicklas |
upload.uploadNextFile = function() |
7987 |
30 Jun 21 |
nicklas |
233 |
{ |
7991 |
01 Jul 21 |
nicklas |
if (uploadAborted) return; |
7990 |
01 Jul 21 |
nicklas |
if (files.length == 0) |
7990 |
01 Jul 21 |
nicklas |
236 |
{ |
7990 |
01 Jul 21 |
nicklas |
var numFilesMsg = uploadedFiles==1?'1 file' : uploadedFiles+' files'; |
7990 |
01 Jul 21 |
nicklas |
var msg = numFilesMsg+' upload sucessfully'; |
7990 |
01 Jul 21 |
nicklas |
if (skippedFiles > 0) |
7990 |
01 Jul 21 |
nicklas |
240 |
{ |
7990 |
01 Jul 21 |
nicklas |
numFilesMsg = skippedFiles==1?'1 file' : skippedFiles+' files'; |
7990 |
01 Jul 21 |
nicklas |
msg += ' ('+numFilesMsg+' skipped)'; |
7990 |
01 Jul 21 |
nicklas |
243 |
} |
7990 |
01 Jul 21 |
nicklas |
var url = '../../common/close_popup.jsp?refresh_opener=1&message='+encodeURIComponent(msg); |
7990 |
01 Jul 21 |
nicklas |
window.top.location = url; |
7990 |
01 Jul 21 |
nicklas |
return; |
7990 |
01 Jul 21 |
nicklas |
247 |
} |
7990 |
01 Jul 21 |
nicklas |
248 |
|
7990 |
01 Jul 21 |
nicklas |
file = files.splice(0, 1)[0]; // Get first file and remove it from array |
7987 |
30 Jun 21 |
nicklas |
currentFile++; |
7990 |
01 Jul 21 |
nicklas |
251 |
|
7992 |
01 Jul 21 |
nicklas |
var timeInSeconds = Math.round((new Date().getTime()-uploadStartTime)/1000); |
7992 |
01 Jul 21 |
nicklas |
upload.displayProgress(bytesAlreadyUploaded, totalBytesToUpload, timeInSeconds); |
7991 |
01 Jul 21 |
nicklas |
Doc.element('fileCount').innerHTML = currentFile+' of '+totalFiles; |
7991 |
01 Jul 21 |
nicklas |
Doc.element('fileName').innerHTML = Strings.encodeTags(file.name)+' ('+Numbers.formatBytes(file.size)+')'; |
7991 |
01 Jul 21 |
nicklas |
Doc.element('path').innerHTML = Data.get('page-data', 'directory-path')+'/'+file.subfolder; |
7991 |
01 Jul 21 |
nicklas |
257 |
|
7990 |
01 Jul 21 |
nicklas |
var frm = document.forms['upload']; |
7990 |
01 Jul 21 |
nicklas |
if (frm.overwrite.value == 'always') |
7990 |
01 Jul 21 |
nicklas |
260 |
{ |
7990 |
01 Jul 21 |
nicklas |
upload.doUpload(file, true); |
7990 |
01 Jul 21 |
nicklas |
262 |
} |
7990 |
01 Jul 21 |
nicklas |
else |
7990 |
01 Jul 21 |
nicklas |
264 |
{ |
7990 |
01 Jul 21 |
nicklas |
var path = Data.get('page-data', 'directory-path')+'/'+file.subfolder+file.name; |
7990 |
01 Jul 21 |
nicklas |
var url = 'ajax.jsp?ID='+App.getSessionId(); |
7990 |
01 Jul 21 |
nicklas |
url += '&cmd=CheckExistingFile'; |
7990 |
01 Jul 21 |
nicklas |
url += '&path='+encodeURIComponent(path); |
7990 |
01 Jul 21 |
nicklas |
269 |
|
7990 |
01 Jul 21 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7990 |
01 Jul 21 |
nicklas |
request.open("GET", url, true); |
7990 |
01 Jul 21 |
nicklas |
Ajax.setReadyStateHandler(request, upload.fileCheckCallback); |
7990 |
01 Jul 21 |
nicklas |
request.send(null); |
7990 |
01 Jul 21 |
nicklas |
274 |
} |
7987 |
30 Jun 21 |
nicklas |
275 |
} |
7987 |
30 Jun 21 |
nicklas |
276 |
|
7990 |
01 Jul 21 |
nicklas |
upload.fileCheckCallback = function(request) |
7987 |
30 Jun 21 |
nicklas |
278 |
{ |
7990 |
01 Jul 21 |
nicklas |
var check = JSON.parse(request.responseText); |
7990 |
01 Jul 21 |
nicklas |
var frm = document.forms['upload']; |
7990 |
01 Jul 21 |
nicklas |
var overwrite = frm.overwrite.value; |
7990 |
01 Jul 21 |
nicklas |
282 |
|
7990 |
01 Jul 21 |
nicklas |
if (check.fileExists) |
7990 |
01 Jul 21 |
nicklas |
284 |
{ |
7990 |
01 Jul 21 |
nicklas |
if (overwrite == 'skipOrReplaceSize') |
7990 |
01 Jul 21 |
nicklas |
286 |
{ |
7990 |
01 Jul 21 |
nicklas |
overwrite = check.fileSize != file.size ? 'always' : 'never'; |
7990 |
01 Jul 21 |
nicklas |
288 |
} |
7990 |
01 Jul 21 |
nicklas |
if (overwrite == 'never') |
7990 |
01 Jul 21 |
nicklas |
290 |
{ |
7990 |
01 Jul 21 |
nicklas |
skippedFiles++; |
7990 |
01 Jul 21 |
nicklas |
skippedBytes += file.size; |
7991 |
01 Jul 21 |
nicklas |
bytesAlreadyUploaded += file.size; |
7990 |
01 Jul 21 |
nicklas |
upload.uploadNextFile(); |
7990 |
01 Jul 21 |
nicklas |
return; |
7990 |
01 Jul 21 |
nicklas |
296 |
} |
7990 |
01 Jul 21 |
nicklas |
297 |
} |
7990 |
01 Jul 21 |
nicklas |
upload.doUpload(file, overwrite == 'always'); |
7990 |
01 Jul 21 |
nicklas |
299 |
} |
7990 |
01 Jul 21 |
nicklas |
300 |
|
7990 |
01 Jul 21 |
nicklas |
upload.doUpload = function(file, overwrite) |
7990 |
01 Jul 21 |
nicklas |
302 |
{ |
7991 |
01 Jul 21 |
nicklas |
if (uploadAborted) return; |
7991 |
01 Jul 21 |
nicklas |
304 |
|
7988 |
30 Jun 21 |
nicklas |
// File name |
7990 |
01 Jul 21 |
nicklas |
var frm = document.forms['upload']; |
7988 |
30 Jun 21 |
nicklas |
307 |
|
7987 |
30 Jun 21 |
nicklas |
var upl = new FormData(); |
7988 |
30 Jun 21 |
nicklas |
upl.set('subfolder', file.subfolder); |
7988 |
30 Jun 21 |
nicklas |
upl.set('autoSelect', totalFiles == 1 ? 1 : 0); // No automatic selection if we upload more than one file |
7990 |
01 Jul 21 |
nicklas |
upl.set('replace_existing', overwrite); |
8013 |
13 Aug 21 |
nicklas |
if (frm.unpackCompressed.checked) |
8013 |
13 Aug 21 |
nicklas |
313 |
{ |
8013 |
13 Aug 21 |
nicklas |
var zipFormat = upload.getCompressedFormat(file.name); |
8013 |
13 Aug 21 |
nicklas |
if (zipFormat) |
8013 |
13 Aug 21 |
nicklas |
316 |
{ |
8013 |
13 Aug 21 |
nicklas |
upl.set('zip_format', zipFormat.id); |
8013 |
13 Aug 21 |
nicklas |
upl.set('zip_unpack', 1); |
8013 |
13 Aug 21 |
nicklas |
upl.set('zip_overwrite', overwrite); |
8013 |
13 Aug 21 |
nicklas |
upl.set('zip_keep', 0); |
8013 |
13 Aug 21 |
nicklas |
321 |
} |
8013 |
13 Aug 21 |
nicklas |
322 |
} |
7990 |
01 Jul 21 |
nicklas |
upl.set('description', frm.description.value); |
7987 |
30 Jun 21 |
nicklas |
upl.set('the_file', file); |
7987 |
30 Jun 21 |
nicklas |
325 |
|
7987 |
30 Jun 21 |
nicklas |
var url = 'Upload?ID='+App.getSessionId(); |
7987 |
30 Jun 21 |
nicklas |
url += '&directory_id='+Data.get('page-data', 'directory-id'); |
7987 |
30 Jun 21 |
nicklas |
328 |
|
7987 |
30 Jun 21 |
nicklas |
uploadRequest = Ajax.getXmlHttpRequest(); |
7987 |
30 Jun 21 |
nicklas |
Ajax.setReadyStateHandler(uploadRequest, upload.uploadComplete, upload.uploadComplete); |
7987 |
30 Jun 21 |
nicklas |
uploadRequest.open('POST', url, true); |
7987 |
30 Jun 21 |
nicklas |
uploadRequest.send(upl); |
7987 |
30 Jun 21 |
nicklas |
333 |
} |
7987 |
30 Jun 21 |
nicklas |
334 |
|
7987 |
30 Jun 21 |
nicklas |
335 |
|
7987 |
30 Jun 21 |
nicklas |
upload.uploadComplete = function(request) |
7987 |
30 Jun 21 |
nicklas |
337 |
{ |
7987 |
30 Jun 21 |
nicklas |
// If we have called uploadRequest.abort() |
7987 |
30 Jun 21 |
nicklas |
// we do not need error handling here |
7987 |
30 Jun 21 |
nicklas |
if (request.status == 0) return; |
7987 |
30 Jun 21 |
nicklas |
var result = {}; |
7987 |
30 Jun 21 |
nicklas |
try |
7987 |
30 Jun 21 |
nicklas |
343 |
{ |
7987 |
30 Jun 21 |
nicklas |
var contentType = request.getResponseHeader('Content-Type'); |
7987 |
30 Jun 21 |
nicklas |
if (request.status != 200 || contentType.indexOf('json') == -1) |
7987 |
30 Jun 21 |
nicklas |
346 |
{ |
7987 |
30 Jun 21 |
nicklas |
throw new Error(request.responseText); |
7987 |
30 Jun 21 |
nicklas |
348 |
} |
7987 |
30 Jun 21 |
nicklas |
result = JSON.parse(request.responseText); |
7987 |
30 Jun 21 |
nicklas |
350 |
} |
7987 |
30 Jun 21 |
nicklas |
catch (err) |
7987 |
30 Jun 21 |
nicklas |
352 |
{ |
7987 |
30 Jun 21 |
nicklas |
result.status = 'error'; |
7987 |
30 Jun 21 |
nicklas |
result.message = err; |
7987 |
30 Jun 21 |
nicklas |
355 |
} |
7987 |
30 Jun 21 |
nicklas |
if (result.status == 'ok') |
7987 |
30 Jun 21 |
nicklas |
357 |
{ |
7990 |
01 Jul 21 |
nicklas |
bytesAlreadyUploaded += result.transferredBytes; |
7990 |
01 Jul 21 |
nicklas |
uploadedFiles++; |
7990 |
01 Jul 21 |
nicklas |
upload.uploadNextFile(); |
7987 |
30 Jun 21 |
nicklas |
361 |
} |
7987 |
30 Jun 21 |
nicklas |
else |
7987 |
30 Jun 21 |
nicklas |
363 |
{ |
7987 |
30 Jun 21 |
nicklas |
upload.displayError(result); |
7987 |
30 Jun 21 |
nicklas |
365 |
} |
7987 |
30 Jun 21 |
nicklas |
366 |
} |
7987 |
30 Jun 21 |
nicklas |
367 |
|
7987 |
30 Jun 21 |
nicklas |
upload.sendProgressUpdateRequest = function() |
7987 |
30 Jun 21 |
nicklas |
369 |
{ |
7987 |
30 Jun 21 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7987 |
30 Jun 21 |
nicklas |
var url = 'ajax.jsp?ID='+App.getSessionId(); |
7987 |
30 Jun 21 |
nicklas |
url += '&cmd=GetProgress&'+Math.random(); |
7987 |
30 Jun 21 |
nicklas |
request.open("GET", url, true); |
7987 |
30 Jun 21 |
nicklas |
Ajax.setReadyStateHandler(request, upload.progressUpdateCallback); |
7987 |
30 Jun 21 |
nicklas |
request.send(null); |
7987 |
30 Jun 21 |
nicklas |
376 |
} |
7987 |
30 Jun 21 |
nicklas |
377 |
|
7987 |
30 Jun 21 |
nicklas |
upload.progressUpdateCallback = function(request) |
7987 |
30 Jun 21 |
nicklas |
379 |
{ |
7987 |
30 Jun 21 |
nicklas |
var progress = JSON.parse(request.responseText); |
7987 |
30 Jun 21 |
nicklas |
if (progress) |
7987 |
30 Jun 21 |
nicklas |
382 |
{ |
7987 |
30 Jun 21 |
nicklas |
if (progress.status == 'ok') |
7987 |
30 Jun 21 |
nicklas |
384 |
{ |
7987 |
30 Jun 21 |
nicklas |
if (progress.file) |
7987 |
30 Jun 21 |
nicklas |
386 |
{ |
7987 |
30 Jun 21 |
nicklas |
var transferredBytes = bytesAlreadyUploaded + progress.transferredBytes; |
7991 |
01 Jul 21 |
nicklas |
var timeInSeconds = Math.round((new Date().getTime()-uploadStartTime)/1000); |
7987 |
30 Jun 21 |
nicklas |
var message = progress.message; |
7992 |
01 Jul 21 |
nicklas |
upload.displayProgress(transferredBytes, totalBytesToUpload, timeInSeconds, message); |
7987 |
30 Jun 21 |
nicklas |
391 |
} |
7987 |
30 Jun 21 |
nicklas |
392 |
} |
7987 |
30 Jun 21 |
nicklas |
else |
7987 |
30 Jun 21 |
nicklas |
394 |
{ |
7987 |
30 Jun 21 |
nicklas |
upload.displayError(progress); |
7991 |
01 Jul 21 |
nicklas |
if (uploadRequest) uploadRequest.abort(); // Important!! Otherwise the browser may try to re-send the request |
7987 |
30 Jun 21 |
nicklas |
return; |
7987 |
30 Jun 21 |
nicklas |
398 |
} |
7987 |
30 Jun 21 |
nicklas |
399 |
} |
7987 |
30 Jun 21 |
nicklas |
setTimeout(upload.sendProgressUpdateRequest, 1500); |
7987 |
30 Jun 21 |
nicklas |
401 |
} |
7987 |
30 Jun 21 |
nicklas |
402 |
|
7992 |
01 Jul 21 |
nicklas |
upload.displayProgress = function(transferredBytes, totalBytes, transferTime, message) |
7987 |
30 Jun 21 |
nicklas |
404 |
{ |
7987 |
30 Jun 21 |
nicklas |
// Percent done |
7987 |
30 Jun 21 |
nicklas |
var percentDone = totalBytes == 0 ? 0 : Math.round(100 * transferredBytes / totalBytes); |
7987 |
30 Jun 21 |
nicklas |
Doc.element('percentText').innerHTML = percentDone+'%'; |
7987 |
30 Jun 21 |
nicklas |
var doneElement = Doc.element('percentDone'); |
7987 |
30 Jun 21 |
nicklas |
doneElement.style.width = percentDone+'%'; |
7987 |
30 Jun 21 |
nicklas |
var remainElement = Doc.element('percentRemain'); |
7987 |
30 Jun 21 |
nicklas |
remainElement.style.width = (100-percentDone)+'%'; |
7987 |
30 Jun 21 |
nicklas |
if (percentDone == 100) Doc.hide('percentRemain'); |
7987 |
30 Jun 21 |
nicklas |
413 |
|
7987 |
30 Jun 21 |
nicklas |
// Bytes |
7987 |
30 Jun 21 |
nicklas |
Doc.element('transferredBytes').innerHTML = Numbers.formatBytes(transferredBytes); |
7990 |
01 Jul 21 |
nicklas |
Doc.element('totalBytes').innerHTML = Numbers.formatBytes(totalBytes); |
7990 |
01 Jul 21 |
nicklas |
if (skippedBytes > 0) |
7990 |
01 Jul 21 |
nicklas |
418 |
{ |
7990 |
01 Jul 21 |
nicklas |
var numFilesMsg = skippedFiles==1?'1 file' : skippedFiles+' files'; |
7990 |
01 Jul 21 |
nicklas |
Doc.element('skippedBytes').innerHTML = ' (Skipped '+numFilesMsg+'; '+Numbers.formatBytes(skippedBytes)+')'; |
7990 |
01 Jul 21 |
nicklas |
421 |
} |
7987 |
30 Jun 21 |
nicklas |
422 |
|
7987 |
30 Jun 21 |
nicklas |
// Times and rate |
7987 |
30 Jun 21 |
nicklas |
Doc.element('transferTime').innerHTML = Numbers.formatSeconds(transferTime); |
7987 |
30 Jun 21 |
nicklas |
var transferRate = transferTime == 0 ? 0 : transferredBytes / transferTime; |
7992 |
01 Jul 21 |
nicklas |
var fileRate = transferTime == 0 ? 0 : currentFile / transferTime; |
7987 |
30 Jun 21 |
nicklas |
Doc.element('transferRate').innerHTML = '(' + Numbers.formatBytes(transferRate) + '/s)'; |
7992 |
01 Jul 21 |
nicklas |
if (transferRate > 0 && fileRate > 0) |
7987 |
30 Jun 21 |
nicklas |
429 |
{ |
7992 |
01 Jul 21 |
nicklas |
var remainTime = Math.max(Math.ceil((totalBytes-transferredBytes)/transferRate), Math.ceil((totalFiles-currentFile)/fileRate)); |
7992 |
01 Jul 21 |
nicklas |
if (remainTime < 0) remainTime = 0; |
7987 |
30 Jun 21 |
nicklas |
Doc.element('remainTime').innerHTML = Numbers.formatSeconds(remainTime) + ' (estimated)'; |
7987 |
30 Jun 21 |
nicklas |
433 |
} |
7987 |
30 Jun 21 |
nicklas |
434 |
|
7987 |
30 Jun 21 |
nicklas |
// Message |
7987 |
30 Jun 21 |
nicklas |
if (message) |
7987 |
30 Jun 21 |
nicklas |
437 |
{ |
7987 |
30 Jun 21 |
nicklas |
Doc.element('message').innerHTML = message.replace(/(.*):(.*)/, '<th>$1</th><td>$2</td>'); |
7987 |
30 Jun 21 |
nicklas |
439 |
} |
7987 |
30 Jun 21 |
nicklas |
else |
7987 |
30 Jun 21 |
nicklas |
441 |
{ |
7987 |
30 Jun 21 |
nicklas |
Doc.element('message').innerHTML = ''; |
7987 |
30 Jun 21 |
nicklas |
443 |
} |
7987 |
30 Jun 21 |
nicklas |
444 |
} |
7987 |
30 Jun 21 |
nicklas |
445 |
|
7987 |
30 Jun 21 |
nicklas |
upload.displayError = function(result) |
7987 |
30 Jun 21 |
nicklas |
447 |
{ |
7987 |
30 Jun 21 |
nicklas |
Doc.show('uploadFailed'); |
7987 |
30 Jun 21 |
nicklas |
Doc.hide('progressForm'); |
7990 |
01 Jul 21 |
nicklas |
Doc.hide('preUploadForm'); |
7987 |
30 Jun 21 |
nicklas |
Doc.element('failTitle').innerHTML = result.title || 'File upload failed'; |
7987 |
30 Jun 21 |
nicklas |
Doc.element('failMessage').innerHTML = result.message || result.stacktrace; |
7987 |
30 Jun 21 |
nicklas |
if (result.status == 'abort') setTimeout(App.closeWindow, 1500); |
7987 |
30 Jun 21 |
nicklas |
454 |
} |
7987 |
30 Jun 21 |
nicklas |
455 |
|
7987 |
30 Jun 21 |
nicklas |
upload.abortUpload = function() |
7987 |
30 Jun 21 |
nicklas |
457 |
{ |
7987 |
30 Jun 21 |
nicklas |
if (confirm('Are you sure you want to abort the upload? It cannot be resumed.')) |
7987 |
30 Jun 21 |
nicklas |
459 |
{ |
7987 |
30 Jun 21 |
nicklas |
460 |
/* |
7987 |
30 Jun 21 |
nicklas |
Aborting is tricky. See tickets: |
7987 |
30 Jun 21 |
nicklas |
http://base.thep.lu.se/ticket/2018 |
7987 |
30 Jun 21 |
nicklas |
http://base.thep.lu.se/ticket/2019 |
7987 |
30 Jun 21 |
nicklas |
464 |
|
7987 |
30 Jun 21 |
nicklas |
Sending "Abort" command to the server AND |
7987 |
30 Jun 21 |
nicklas |
uploadRequest.abort() should tell both ends. |
7987 |
30 Jun 21 |
nicklas |
The "abort" is catched by the progress updater that |
7987 |
30 Jun 21 |
nicklas |
also display the error message. |
7987 |
30 Jun 21 |
nicklas |
469 |
*/ |
7991 |
01 Jul 21 |
nicklas |
uploadAborted = true; |
7991 |
01 Jul 21 |
nicklas |
if (uploadRequest) uploadRequest.abort(); |
7991 |
01 Jul 21 |
nicklas |
472 |
|
7987 |
30 Jun 21 |
nicklas |
var url = 'ajax.jsp?ID='+App.getSessionId(); |
7987 |
30 Jun 21 |
nicklas |
url += '&cmd=Abort&'+Math.random(); |
7991 |
01 Jul 21 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7991 |
01 Jul 21 |
nicklas |
Ajax.setReadyStateHandler(request, upload.abortProgressCallback); |
7987 |
30 Jun 21 |
nicklas |
request.open("GET", url, true); |
7987 |
30 Jun 21 |
nicklas |
request.send(null); |
7991 |
01 Jul 21 |
nicklas |
479 |
} |
7991 |
01 Jul 21 |
nicklas |
480 |
} |
7987 |
30 Jun 21 |
nicklas |
481 |
|
7991 |
01 Jul 21 |
nicklas |
upload.abortProgressCallback = function(request) |
7991 |
01 Jul 21 |
nicklas |
483 |
{ |
7991 |
01 Jul 21 |
nicklas |
var progress = JSON.parse(request.responseText); |
7991 |
01 Jul 21 |
nicklas |
if (!progress.aborted) |
7991 |
01 Jul 21 |
nicklas |
486 |
{ |
7991 |
01 Jul 21 |
nicklas |
// There was no active upload, so we simply abort... |
7991 |
01 Jul 21 |
nicklas |
upload.displayError({'status': 'abort', 'message': 'The upload was aborted'}); |
7987 |
30 Jun 21 |
nicklas |
489 |
} |
7987 |
30 Jun 21 |
nicklas |
490 |
} |
7987 |
30 Jun 21 |
nicklas |
491 |
|
7987 |
30 Jun 21 |
nicklas |
return upload; |
7987 |
30 Jun 21 |
nicklas |
493 |
}(); |
7987 |
30 Jun 21 |
nicklas |
494 |
|
7987 |
30 Jun 21 |
nicklas |
Doc.onLoad(Upload.initPage); |