-
Notifications
You must be signed in to change notification settings - Fork 32
/
bis_genericio.js
756 lines (618 loc) · 21.7 KB
/
bis_genericio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* LICENSE
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._
BioImage Suite Web is licensed under the Apache License, Version 2.0 (the "License");
- you may not use this software except in compliance with the License.
- You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
__Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.__
ENDLICENSE */
"use strict";
/**
* @file Browser/Node Js module. Contains {@link BisGenericIO}.
* @author Xenios Papademetris
* @version 1.0
*/
const biscoreio=require('./bis_coregenericio');
const util=require('./bis_util');
const glob=biscoreio.getglobmodule();
const fs=biscoreio.getfsmodule();
const path=biscoreio.getpathmodule();
const rimraf=biscoreio.getrimrafmodule();
// -------------------------------------------------------------------------------------------------------
// File server stuff
let fileServerClient=null;
const inBrowser= biscoreio.getmode() === 'browser';
const isInBrowser=function() {
return inBrowser;
};
/** Sets the file server object
* @alias BisGenericIO#setFileServerObject
* @param {Object} obj - the server object
*/
const setFileServerObject=function(obj) {
fileServerClient=obj;
};
/** Gets he file server object
* @alias BisGenericIO#getFileServerObject
* @returns {Object} - the server object
*/
const getFileServerObject=function() {
return fileServerClient;
};
/** Fixes the save filename depending on whether we have a string, a mouse event, or a FILE
* Essentially if this is an object only use it if has a .name member, else replace it
* @alias BisGenericIO#fixSaveFilename
* @param {Object} url - abstract file handle object
* @param {String} replacement
* @returns {Object} - a string or the file object as needed
*/
let getFixedSaveFileName = function(fobj,replacement) {
if (fobj=== undefined || fobj===null || fobj == {} )
return replacement;
if (typeof fobj === "object") {
if (!fobj.name && !fobj.filename) {
fobj=replacement;
}
}
return fobj;
};
/** Get the load filename
* Essentially if input is an object check if it has a .name variable and return it instead
* @alias BisGenericIO#getLoadFilename
* @param {Object} url - abstract file handle object
* @param {String} replacement
* @returns {Object} - a string or the file object as needed
*/
let getFixedLoadFileName = function(fobj) {
fobj = fobj || '';
if (typeof fobj === "object") {
if (fobj.filename)
return fobj.filename;
if (fobj.name)
return fobj.name;
}
if (fobj.indexOf("?=realname=")>0) {
let a=fobj.indexOf("?=realname=");
let n=fobj.length;
return fobj.substr(a+11,n-a);
}
if (fobj.indexOf("http")===0) {
// Url -- try to get the last parts
let s=fobj.split("/");
if (fobj.indexOf("dl.dropbox")>0) {
let f="dropbox/";
for (let i=6;i<s.length;i++) {
f=f+"/"+s[i];
}
return f;
}
return s[s.length-1];
}
return fobj;
};
// -------------------------------------------------------------------------------------------------------
/** Read data
* @alias BisGenericIO#read
* @param {String} url -- abstact file handle object
* @param {Boolean} isbinary -- is data binary
* @returns {Promise} -- the .then function of the promise has an object with two members data and filename
* that contain the actual data read and the actual filename read respectively.
*/
let read = function (url, isbinary = false) {
if (fileServerClient && typeof url === 'string') {
if (url.indexOf('http')!==0) {
return fileServerClient.downloadFile(url,isbinary);
}
}
if (url) {
if (url.responseFunction!==undefined) {
return url.responseFunction(url,isbinary);
}
}
return new Promise(function (resolve, reject) {
let success = function (data, fname) {
resolve({
data: data,
filename: fname
});
};
let failure = function (e) {
reject(e);
};
if (isbinary) {
biscoreio.readbinarydata(url, success, failure);
} else {
biscoreio.readtextdata(url, success, failure);
}
});
};
/** Generic read json data. This is a text file. The object to be read must have an attribute 'bisformat' which must match the desired one.
* This first calls @{link BisGenericIO~read} and then does parsing as needed
* @alias BisGenericIO#readJSON
* @param {object} url - the url or filename or file object
* @param {string} bisformat - the desired bisformat
*/
let readJSON = function (url, bisformat) {
return new Promise( (resolve,reject) => {
read(url).then( (obj) => {
let jsonstring=obj.data;
let b;
try {
b = JSON.parse(jsonstring);
} catch (e) {
reject(url + " is not a valid JSON File --pick something with a .json extension");
}
if (b.bisformat !== bisformat) {
reject("Bad JSON File " + url + " element bisformat does not equal " + bisformat);
}
resolve( { data : b,
filename : url
});
}).catch( (e) => { reject(e.trace); });
});
};
/** Write data
* @alias BisGenericIO#write
* @param {String} url -- abstact file handle object
* @param {Data} data -- the data to save, either a sting or a Uint8Array
* @param {Boolean} isbinary -- is data binary
* @returns {Promise}
*/
let write = function (url, data,isbinary=false) {
if (fileServerClient && typeof url === 'string') {
if (url.indexOf('http')!==0) {
return fileServerClient.uploadFile(url,data,isbinary);
}
}
if (url) {
if (url.responseFunction !== undefined) {
return url.responseFunction(url, data, isbinary);
}
}
return new Promise(function (resolve, reject) {
let success = function (msg) {
resolve(msg);
};
let failure = function (e) {
reject(e);
};
if (isbinary)
biscoreio.writebinarydata(url, data, success, failure);
else
biscoreio.writetextdata(url, data, success, failure);
});
};
/** Returns the size in bytes of a file
* @alias BisGenericIO#getFileSize
* @param {String} url - the filename
* @returns {Promise} - the payload is the size of the file in bytes
*/
let getFileSize=function(url) {
if (fileServerClient) {
return fileServerClient.getFileSize(url);
}
if (inBrowser) {
return Promise.rejected('getFileSize can not be done in a Browser');
}
let fileSizeInBytes=-1;
try {
let stats = fs.statSync(url);
fileSizeInBytes = stats["size"];
return Promise.resolve(fileSizeInBytes);
} catch(e) {
return Promise.reject(e);
}
};
/** Checks is a path is a directory
* @alias BisGenericIO#isDirectory
* @param {String} url - the directory string
* @returns {Promise} - the payload is true or false
*/
let isDirectory=function(url) {
if (fileServerClient) {
return fileServerClient.isDirectory(url);
}
if (inBrowser) {
return Promise.reject('getFileSize can not be done in a Browser');
}
let m=true;
if (!fs.lstatSync(url).isDirectory())
m=false;
return Promise.resolve(m);
};
/**
* Gets the stats object for a file. Currently only for file server.
* @alias BisGenericIO#getFileStats
* @param {String} url - the file string
* @returns {Promise} - promise resolving
*/
let getFileStats=function(url) {
console.log('get file stats');
if (fileServerClient) {
return fileServerClient.getFileStats(url);
}
if (inBrowser) {
return Promise.reject('getFileStats cannot be done in a Browser');
}
let m=fs.lstatSync(url);
return Promise.resolve(m);
};
/** Create the directory in url
* @alias BisGenericIO#makeDirectory
* @param {String} url - the directory string
* @returns {Promise} - the payload is true or false
*/
let makeDirectory=function(url) {
if (fileServerClient) {
return fileServerClient.makeDirectory(url);
}
if (inBrowser) {
return Promise.reject('getFileSize can not be done in a Browser');
}
let m=false;
if (!fs.existsSync(url)){
fs.mkdirSync(url);
m=true;
}
return Promise.resolve(m);
};
/** Checks is a path is a directory, then deletes it if it can
* @alias BisGenericIO#deleteDirectory
* @param {String} url - the directory name
* @returns {Promise} - the payload is true or false
*/
let deleteDirectory=function(url) {
if (fileServerClient) {
return fileServerClient.deleteDirectory(url);
}
if (inBrowser) {
return Promise.reject('getFileSize can not be done in a Browser');
}
if (!fs.lstatSync(url).isDirectory())
return Promise.reject(url+' is not a directory');
return new Promise( (resolve,reject) => {
try {
rimraf(url, function () { resolve('removed '+url); });
} catch(e) {
reject(e);
}
});
};
/**
* Tries to move a directory from source to destination. Currently only for fileserver.
*
* @alias BisGenericIO#moveDirectory
* @param {String} url - name of source and destination, separated by '&&'
* @returns {Promise} - the payload is true or false
*/
let moveDirectory=function(url) {
if (fileServerClient) {
return fileServerClient.moveDirectory(url);
}
if (inBrowser) {
return Promise.reject('moveDirectory can not be done in a Browser');
}
let splitNames = splitFilenames(url);
let src = splitNames[0], dest = splitNames[1];
if (!fs.lstatSync(src).isFile())
return Promise.reject(src + ' does not describe a file, cannot move it.');
return new Promise( (resolve, reject) => {
fs.copyFile(src, dest, (err) => {
if (err) { console.log('Encountered an error trying to move file', src, err); reject(false); return; }
fs.unlink(src, (err) => {
if (err) { console.log('Encountered an error trying to delete', src, err); reject(false); return; }
resolve(true);
});
});
});
};
/** Copies a single file
* @param {String} url - Source and destination
* @returns {Promise} without payload
*/
let copyFile=function(url) {
if (fileServerClient) {
try {
return fileServerClient.copyFile(url);
} catch (e) {
return Promise.reject('No copy file functionality');
}
}
if (inBrowser) {
return Promise.rejected('copyFile can not be done in a Browser');
}
let splitNames = splitFilenames(url);
let src = splitNames[0], dest = splitNames[1];
//console.log('Copying ',src,' to',dest);
// https://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js
let rd = fs.createReadStream(src);
let wr = fs.createWriteStream(dest);
return new Promise(function(resolve, reject) {
rd.on('error', reject);
wr.on('error', reject);
wr.on('finish', resolve);
try {
rd.pipe(wr);
} catch(e) {
rd.destroy();
wr.end();
reject(e);
}
});
};
/** Returns matching files in a path
* @alias BisGenericIO#getMatchingFiles
* @param {String} matchstring - the query string
* @returns {Promise} - the payload is the file list
*/
let getMatchingFiles=function(matchstring) {
if (fileServerClient) {
return fileServerClient.getMatchingFiles(matchstring);
}
if (inBrowser) {
return Promise.reject('getMatchingFiles can not be done in a Browser');
}
let m=glob.sync(matchstring);
if (path.sep==='\\') {
let m2=[];
for (let i=0;i<m.length;i++) {
m2.push(m[i].replace(/\//g,path.sep));
}
m=m2;
}
return Promise.resolve(m);
};
/** Returns the base name of a file (i.e. strips out the directory)
* @alias BisGenericIO#getBaseName
* @param {String} fname - the filename
* @param {Boolean} forceinternal - if true use internal implementation (not path in node.js)
* @returns {string} - the base name
*/
let getBaseName=function(fname,forceinternal=false) {
if (!inBrowser && !forceinternal) {
return path.basename(fname);
}
fname=util.filenameWindowsToUnix(fname);
let q=fname.split('/');
return q[q.length-1];
};
/** Returns the directory part of the name of a file (i.e. strips out the basename)
* @alias BisGenericIO#getDirectoryName
* @param {String} fname - the filename
* @param {Boolean} forceinternal - if true use internal implementation (not path in node.js)
* @returns {string} - the directory name
*/
let getDirectoryName=function(fname,forceinternal=false) {
if (!inBrowser && !forceinternal) {
return path.dirname(fname);
}
fname=util.filenameWindowsToUnix(fname);
let i=fname.lastIndexOf('/');
if (i<0)
return fname;
return fname.substr(0,i);
};
/** Returns a normalized filename (removes ../ and makes absolute)
* @alias BisGenericIO#getNormalizedFilename
* @param {String} fname - the filename
* @param {Boolean} forceinternal - if true use internal implementation (not path in node.js)
* @returns {string} - the normalized name
*/
let getNormalizedFilename=function(fname,root="",forceinternal=false) {
if (!inBrowser && !forceinternal) {
if (root==="")
return path.resolve(path.normalize(fname));
return path.normalize(fname);
}
// console.log('---\n---\nInitial fname=',fname);
fname=util.filenameWindowsToUnix(fname);
// First replace '/./' with '/'
// eslint-disable-next-line no-useless-escape
fname=fname.trim().replace(/\/\.\//g,'\/');
// console.log('fname=',fname);
let ind1=fname.indexOf(root+'/');
let ind2=fname.indexOf('..');
if (ind1===0 && ind2<0)
return fname; // nothing to do
// console.log('\n ------------- Beginning \n');
fname=root+fname;
let parts=fname.split('/');
// console.log('fname=',fname,'\n\t parts='+parts.join("__")+'\n');
let j=parts.length-1;
while (j>=0) {
if (parts[j].length<1) {
parts.splice(j,1);
j=parts.length-1;
} else if (parts[j]=="..") {
// console.log('\t\t before ',j,parts[j],' -->',parts.join('/'));
if (j>0) {
let k=j-1,done=false;
while(k>=0 && done===false) {
if (parts[k]!=="..")
done=true;
else
k=k-1;
}
if (done) {
let diff=j-k;
parts.splice(j-2*diff+1,2*diff);
// console.log('\t\t\t after ',parts.join(' / '));
j=j-2*diff;
}
} else {
parts.splice(j,1);
// console.log('\t\t\t after ',parts.join(' / '));
j=j-1;
}
} else {
j=j-1;
}
// console.log('J=',j,' parts=',parts.join('__'),parts.length);
}
let out='/'+parts.join('/');
// console.log('Returning fname=',out);
//console.log('\n ------------- Done \n');
return out;
};
/** Returns a joined file name (path.join)
* @alias BisGenericIO#joinFilenames
* @param {String} path1 - the first path
* @param {String} path2 - the seconf path
* @param {Boolean} forceinternal - if true use internal implementation (not path in node.js)
* @returns {string} - the normalized name
*/
let joinFilenames=function(path1,path2,forceinternal=false) {
if (!inBrowser && !forceinternal) {
return path.join(path1,path2);
}
return util.filenameWindowsToUnix(path1+'/'+path2);
};
/** Returns the path separator , either path.sep or '/'
* @alias BisGenericIO#getPathSeparator
* @returns {string} - the path separator
*/
let getPathSeparator=function() {
if (!inBrowser)
return path.sep;
return '/';
};
/** isSaveDownload -- if we are in a browser and not using a fileserver or the cloud then
save operations are download ops
@returns{Boolean} - true or false
*/
let isSaveDownload =function() {
if (inBrowser && fileServerClient===null)
return true;
return false;
};
/**
* Splits a filename concatenated by the symbol '&&' into two names.
*
* @param {String} url - Two filenames, concatenated by '&&'.
* @returns Array of filenames.
*/
let splitFilenames = (url) => {
return url.split('&&');
};
// ------------------------------------------------------------------------------
/**
* Large File Support
*/
/** Read some data from front of a file
* @param{String} fobj -- filename
* @param{Number} end -- max num bytes
* @returns{Promise} -- payload is an arraybuffer with the uncompressed (if gzipped) data
*/
var readPartialDataFromStartOfFile=function(filename,end=1024) {
const zlib=biscoreio.getzlibmodule();
if (bisgenericio.getmode() ==='browser') {
console.log('Load Header Only can only be used in Node or Electron');
return Promise.reject('Failed');
}
let gzip=false;
if (filename.split('.').pop()==='gz') {
gzip=true;
}
//console.log('++++ Reading '+filename+' end='+end+' gzip='+gzip);
const gunzip = zlib.createGunzip();
const bufs=[];
return new Promise( (resolve,reject) => {
let readstream = fs.createReadStream(filename, {
'start' : 0,
'end' : end,
}).on('error', (e) => {
console.log('Error=',e);
reject('error'+e);
});
if (gzip) {
readstream.pipe(gunzip).on('finish', () => {
let headerBuffer = new Uint8Array(Buffer.concat(bufs)).buffer;
readstream.close();
resolve(headerBuffer);
});
gunzip.on('data', (chunk) => {
bufs.push(chunk);
});
gunzip.on('error', () => {
// Ignore this
});
} else {
readstream.on('end', async () => {
let headerBuffer = new Uint8Array(Buffer.concat(bufs)).buffer;
readstream.close();
resolve(headerBuffer);
});
readstream.on('readable', async () => {
let done=false;
while (!done) {
let chunk = readstream.read();
if (chunk) {
bufs.push(chunk);
} else {
readstream.close();
done=true;
}
}
});
}
});
};
// -------------------------------------------------------------------------------------------------------
// Export object
const bisgenericio = {
// Reexport stuff from core io
getmode : biscoreio.getmode,
getenvironment : biscoreio.getenvironment,
createBuffer : biscoreio.createBuffer,
getfsmodule : biscoreio.getfsmodule,
getzlibmodule : biscoreio.getzlibmodule,
getpathmodule : biscoreio.getpathmodule,
getosmodule : biscoreio.getosmodule,
getglobmodule : biscoreio.getglobmodule,
getcolorsmodule : biscoreio.getcolorsmodule,
getchildprocessmodule : biscoreio.getchildprocessmodule,
tozbase64 : biscoreio.tozbase64,
fromzbase64 : biscoreio.fromzbase64,
string2binary : biscoreio.string2binary ,
binary2string : biscoreio.binary2string ,
dataURLToBlob : biscoreio.dataURLToBlob,
iscompressed : biscoreio.iscompressed, // ends in .gz
inIOS : biscoreio.inIOS, // are we running in iOS Safari
setWebWorkerScope : biscoreio.setWebWorkerScope,
readtextdatafromurl : biscoreio.readtextdatafromurl, // read from url
readbinarydatafromurl : biscoreio.readbinarydatafromurl, // read from url
getimagepath : biscoreio.getimagepath,
getElectronDialogFilename : biscoreio.getElectronDialogFilename,
// New functions internal to this
inBrowser : isInBrowser,
setFileServerObject : setFileServerObject,
getFileServerObject : getFileServerObject,
readJSON : readJSON, // Gloabl ReadJSON
read : read, // Global Read data
write : write, // Global Write data
getFixedSaveFileName : getFixedSaveFileName,
getFixedLoadFileName : getFixedLoadFileName,
// Operations needed for Bruker and more
copyFile : copyFile,
getFileSize : getFileSize,
getFileStats: getFileStats,
isDirectory : isDirectory,
getMatchingFiles : getMatchingFiles,
makeDirectory : makeDirectory,
deleteDirectory : deleteDirectory,
moveDirectory : moveDirectory,
// Filename operations
getBaseName : getBaseName,
getDirectoryName : getDirectoryName,
getNormalizedFilename : getNormalizedFilename,
joinFilenames : joinFilenames,
getPathSeparator : getPathSeparator,
//
isSaveDownload : isSaveDownload,
//
readPartialDataFromStartOfFile : readPartialDataFromStartOfFile
};
module.exports = bisgenericio;