@@ -244,17 +244,56 @@ Ext4.define('LABKEY.SignalData.UploadLog', {
244244 if ( Ext4 . isFunction ( callback ) ) {
245245 var me = this ;
246246 var destination = this . fileSystem . concatPaths ( this . fileSystem . getBaseURL ( ) , targetDirectory ) ;
247- this . fileSystem . renamePath ( {
248- source : this . getWorkingPath ( ) ,
249- destination : destination ,
250- isFile : false ,
251- success : function ( ) {
252- me . resolveFileResources ( targetDirectory , callback , scope , runProperties ) ;
247+
248+ const getResourcesCallback = function ( files ) {
249+ if ( files && files . length > 0 ) {
250+ // files currently exist, validate and resolve before creating the run
251+ me . resolveDataFileURL ( files , callback , scope , runProperties ) ;
253252 }
254- } ) ;
253+ else {
254+ // files need to be moved to the target directory
255+ this . fileSystem . renamePath ( {
256+ source : this . getWorkingPath ( ) ,
257+ destination : destination ,
258+ isFile : false ,
259+ success : function ( ) {
260+ me . resolveFileResources ( targetDirectory , callback , scope , runProperties ) ;
261+ }
262+ } ) ;
263+ }
264+ } ;
265+
266+ // get the list of files in the target directory (if any)
267+ this . getTargetDirResources ( targetDirectory , getResourcesCallback , this ) ;
255268 }
256269 } ,
257270
271+ /**
272+ * Returns the list of data files in the target directory. Uploaded run data files are moved from
273+ * a temporary location to the target directory before the run is created.
274+ */
275+ getTargetDirResources : function ( targetDirectory , callback , callbackScope ) {
276+ var fileUri = this . fileSystem . concatPaths ( this . fileSystem . getAbsoluteURL ( ) , targetDirectory ) ;
277+ LABKEY . Ajax . request ( {
278+ url : fileUri ,
279+ method : 'GET' ,
280+ params : { method : 'JSON' } ,
281+ success : function ( response ) {
282+ var json = Ext4 . decode ( response . responseText ) ;
283+ var files = [ ] ;
284+ if ( Ext4 . isDefined ( json ) && Ext4 . isArray ( json . files ) )
285+ files = json . files ;
286+
287+ callback . call ( callbackScope , files ) ;
288+ } ,
289+ failure : function ( ) {
290+ // this is normal in the case where the target directory does not yet exist or
291+ // the files have not yet been moved there
292+ callback . call ( callbackScope , [ ] ) ;
293+ }
294+ } ) ;
295+ } ,
296+
258297 resolveFileResources : function ( targetDirectory , callback , callbackScope , runProperties ) {
259298 var fileUri = this . fileSystem . concatPaths ( this . fileSystem . getAbsoluteURL ( ) , targetDirectory ) ;
260299 LABKEY . Ajax . request ( {
@@ -269,9 +308,10 @@ Ext4.define('LABKEY.SignalData.UploadLog', {
269308 }
270309 }
271310 } ,
272- failure : function ( ) {
273- }
274- , scope : this
311+ failure : LABKEY . Utils . getCallbackWrapper ( function ( json , response , opts ) {
312+ LABKEY . Utils . alert ( 'Error' , 'Unable to resolve file resources : ' + response . exception ) ;
313+ } , this , true ) ,
314+ scope : this
275315 } , this ) ;
276316 } ,
277317
@@ -281,41 +321,42 @@ Ext4.define('LABKEY.SignalData.UploadLog', {
281321 resolveDataFileURL : function ( files , callback , scope , runProperties ) {
282322 if ( Ext4 . isFunction ( callback ) ) {
283323
284- var received = 0 ;
285- var newFiles = [ ] ;
286-
287324 var me = this ;
325+ var paths = [ ] ;
326+ var fileNames = [ ] ;
327+ files . forEach ( function ( file ) {
328+ paths . push ( decodeURIComponent ( file . id ) ) ;
329+ fileNames . push ( file . text ) ;
330+ } , this ) ;
288331
289- function done ( file , results ) {
332+ LABKEY . Ajax . request ( {
333+ url : LABKEY . ActionURL . buildURL ( 'SignalData' , 'getSignalDataResource.api' ) ,
334+ method : 'POST' ,
335+ jsonData : {
336+ paths : paths ,
337+ files : fileNames
338+ } ,
339+ success : function ( response ) {
340+ let result = Ext4 . decode ( response . responseText ) ;
341+ let store = this . getStore ( ) ;
290342
291- var store = me . getStore ( ) ;
292- var idx = store . find ( me . DATA_FILE , file . text ) ;
293- var process = store . getAt ( idx ) ;
343+ Ext4 . each ( result . files , function ( file ) {
294344
295- //Set upload time
296- process . set ( me . FILENAME , results [ me . DATA_FILE ] ) ;
297- process . set ( me . FILE_URL , results [ 'DataFileUrl' ] ) ;
298- process . set ( 'file' , file ) ;
299- newFiles . push ( file ) ;
300- received ++ ;
345+ var idx = store . find ( this . DATA_FILE , file [ "FileName" ] ) ;
346+ var rec = store . getAt ( idx ) ;
301347
302- if ( received == files . length ) {
303- callback . call ( scope || me , newFiles , runProperties ) ;
304- }
305- }
348+ if ( rec ) {
349+ //Set upload time in the store record
350+ rec . set ( me . FILENAME , file [ this . DATA_FILE ] ) ;
351+ rec . set ( me . FILE_URL , file [ 'DataFileUrl' ] ) ;
352+ rec . set ( 'file' , true ) ;
353+ }
354+ } , this ) ;
306355
307- //TODO: This should be refactored to use a single ajax call for the array
308- files . forEach ( function ( file ) {
309- LABKEY . Ajax . request ( {
310- url : LABKEY . ActionURL . buildURL ( 'SignalData' , 'getSignalDataResource.api' ) ,
311- method : 'POST' ,
312- params : { path : decodeURIComponent ( file . id ) , test : true } ,
313- success : function ( response ) {
314- done ( file , Ext4 . decode ( response . responseText ) ) ;
315- } ,
316- scope : this
317- } ) ;
318- } , this ) ;
356+ callback . call ( scope || me , runProperties ) ;
357+ } ,
358+ scope : this
359+ } ) ;
319360 }
320361 } ,
321362
0 commit comments