-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjfile.ijs
543 lines (480 loc) · 10.9 KB
/
jfile.ijs
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
NB. J HTTP Server - jfile app
coclass'jfile'
coinsert'jhs'
require'~addons/convert/misc/base64.ijs'
HBS=: 0 : 0
jhmenu''
'menu0' jhmenugroup ''
jhmpage''
'close' jhmenuitem 'close';'q'
jhmenugroupz''
jhmpagez''
'edit' jhb 'edit'
'load' jhb 'load'
'rename' jhb 'rename'
'new' jhb 'new'
'del' jhb 'del'
'copy' jhb 'copy'
'cut' jhb 'cut'
'paste' jhb 'paste'
'download' jhb 'copy to downloads'
'renamedlg' jhdiva''
jhbr
'renamedo' jhb'rename as'
'renpx' jhtext'';20
'rennx' jhtext'';10
'rensx' jhtext'';4
'renameclose'jhb'X'
'<hr></div>'
'deletedlg' jhdiva''
jhbr
'deletedo' jhb'delete'
'deletename' jhspan''
'deleteclose' jhb'X'
'<hr></div>'
'newdlg' jhdiva''
jhbr
'newfile' jhb'newfile'
'newname' jhtext'';20
'newfolder' jhb'newfolder'
'newclose' jhb'X'
'<hr></div>'
'report' jhdiv'<R>'
jhhr
'recent' jhrad 'Recent';0;'paths'
shorts''
'path' jhhidden'<F>'
'pathd' jhdiv'<F>'
jhresize''
'sel' jhdiv'<FILES>'
)
LASTPATH=: jpath'~temp/'
NB. get valid path (based on folderinfo) ; folderinfo
getfandp=: 3 : 0
d=. folderinfo remlev y
if. '/'={:y do. y;<d return. end.
n=. <jgetfile y
i=. d i. n
if. i<#d do. y;<d return. end.
b=. ((;{:each d)i:'/')}.d
i=. (<:#b)<.>:(nsort b,n)i.n
if. '/'={:;i{b do. ('/',~remlev y);<d return. end.
((remlev y),'/',;i{b);<d
)
NB. y - error;file
create=: 3 : 0
'r f'=. y
if. r-:'' do. r=. ' ' end. NB. so div does not collapse
'f d'=. getfandp f
d=. (<d),<(('/'=;{:each d){'';'▶'),each d
'jfile' jhr 'R F FILES';r;(jshortname f);(buttons 'files';d,<'<br>')
)
NB. get file with mid/path opens the file
jev_get=: 3 : 0
create ' ';LASTPATH
)
buttons=: 3 : 0
'mid sids values sep'=. y
p=. ''
for_i. i.#sids do.
id=. mid,'*',>i{sids
p=. p,sep,~id jhab i{values
end.
)
radios=: 3 : 0
'mid sids values sep'=. y
p=. ''
for_i. i.#sids do.
t=. ,'/'
id=. mid,'*',>i{sids
b=. LASTPATH-:t,~jpath'~',;i{values
b=. b+.(LASTPATH-:t)*.t-:;i{values
p=. p,sep,~id jhrad mid;(;i{values);b
end.
)
shorts=: 3 : 0
radios 'paths';(2#<((roots''),~{."1 UserFolders_j_,SystemFolders_j_)-.;:'Demos User bin config system break snap tools'),<''
)
ev_paths_click=: 3 : 0
f=. getv'jsid'
if. -.f-:,'/' do.
LASTPATH=: jpath '~',f,'/'
else.
LASTPATH=: ,'/'
end.
create '';LASTPATH
)
NB. folder clicked (file click handled in js)
ev_files_click=: 3 : 0
sid=. getv'jsid'
path=. jpath getv'path'
sid=. sid-.PS
if. sid-:'..' do.
f=. PS,~remlev remlev path
else.
f=. (remlev path),PS,sid,PS
end.
LASTPATH=: f
create '';f
)
nsort=: 3 : '/:~y'
NB. y path - result is folders,files
folderinfo=: 3 : 0
a=. 1!:0 <jpath y,'/*'
n=. {."1 a
d=. 'd'=;4{each 4{"1 a
('/',~each(<'..'),nsort d#n),nsort (-.d)#n
)
NB. y '' for file and '/' for folder
new=: 3 : 0
F=. jpath getv'path'
n=. getv'newname'
f=. (remlev F),PS,n
if. +./ '/\' e. n do.
r=. n,' must not contain / or \'
elseif. fexist f do.
r=. n,' already exists'
else.
try.
if. ''-:y do. ''1!:2<f else. 1!:5<f end.
r=. n,' created'
catch.
r=. n,' create failed'
end.
end.
create r;f
)
ev_newfile_click=: 3 : 'new'''''
ev_newfolder_click=: 3 : 'new''/'''
ev_renamedo_click=: 3 : 0
F=. jpath getv'path'
s=. getv'rensx'
f=. jpath (getv'renpx'),(getv'rennx'),s
if. '/'={:F do. f=. f,('/'~:{:f)#'/' end.
if. F-:f do.
r=. 'to same name'
elseif. fexist f do.
r=. 'failed - already exists'
else.
if. f frename F do.
r=. 'ok'
F=. f
else.
r=. 'failed'
end.
end.
create F;~'Rename: ',r
)
ev_deletedo_click=: 3 : 0
create delete getv'path'
)
delete=: 3 : 0
newf=. p=. y
F=. jpath p
try.
t=. jpath'~temp/deleted/'
if. PS={:F do. NB. delete folder
srcfolder=. F
snkfolder=. t,p
if. -.t-:(#t){.F do. NB. backup folder
deletefolder snkfolder
copyfiles (srcfolder,'*');snkfolder
deletefolder srcfolder
r=. 'deleted ',p,' (backup at ~temp/deleted/...)'
else.
deletefolder srcfolder
r=. 'deleted ',p
end.
else. NB. delete file
if. -.t-:(#t){.F do. NB. backup file
mkdir_j_ t,remlev p
(1!:1 <F) 1!:2 <jpath t,p
1!:55 <F
r=. 'deleted ',p,' (backup at ~temp/deleted/...)'
else.
1!:55 <F
r=. 'deleted ',p
end.
end.
catch.
echo 13!:12''
r=. 'delete ',p,' failed'
end.
r;newf
)
ev_load_click=: 3 : 0
f=. jgetfile F=. jpath getv'path'
if. f-:'' do.
create'No file selected to load';F
else.
try.
load__ F
create'file loaded';F
catch.
smoutput 13!:12''
create ('load error: ',13!:12'');F
end.
end.
)
copy=: _1 NB. _1 not ready, 0 copy, 1 cut
copycut=: 3 : 0
copy=: y
srcfile=: jpath getv'path'
create 'Ready for paste';srcfile
)
ev_copy_click=: 3 : 'copycut 0'
ev_cut_click=: 3 : 'copycut 1'
ev_paste_click=: 3 : 0
F=. jpath getv'path'
f=. jgetfile srcfile
if. ''-:f do.
folder=. jgetfile remlev srcfile
srcfolder=. srcfile
snkfolder=. F,jgetfile remlev srcfile
if. srcfile-:(#srcfile){.snkfolder do.
create 'Paste: destination can not be in source';F
return.
end.
try.
1!:5<snkfolder
catch.
create 'Paste: folder already exists';F
return.
end.
copyfiles (srcfile,'*');snkfolder
if. copy=1 do. deletefolder }:srcfile end.
create ('Paste: created folder ',jshortname snkfolder);F
return.
end.
d=. fread srcfile
i=. f i: '.'
a=. i{.f
z=. i}.f
c=. 0
while. fexist snkfile=. (remlev F),PS,f do.
c=. >:c
f=. a,'-Copy(',(":c),')',z
end.
if. _1=copy do. create 'Paste: no copy or cut';F return. end.
if. _1-:d do. create 'Paste: read source file failed';F return. end.
if. _1-:d fwrite snkfile do. create 'Paste: write newfile failed';F return. end.
if. copy=1 do. try. 1!:55 <srcfile catch. end. end.
create ('Paste: created file ',f);F
)
ev_download_click=: 3 : 0
f=. getv'path'
if. '/'={:f do.
NB. tar it
f=. }:jpath f
n=. f}.~>:f i:'/'
f=. (-#n)}.f
shell q__=: 'tar -C "',f,'" -czf ',n,'.tgz ',n
n=. n,'.tgz'
d=. fread n
else.
d=. fread f
n=. f}.~>:f i:'/'
end.
jhrjson qq__=: 'report';(jhfroma n,' copied to server Downloads');'data';(tobase64 d);'name';n
)
NB. copyfiles src;snk
NB. src ends with \fspec which can have wildcards
copyfiles=: 3 : 0
'src snk'=. y
src=. jpath src
snk=. jpath snk
i=. src i:PS
fspec=. (>:i)}.src
src=. i{.src
mkdir_j_ snk
ns=. 1!:0 <jpath src,'\',fspec
for_n. ns do.
pn=. jpath'\',>{.n
srcpn=.src,pn
snkpn=.snk,pn
if. 'd'~:4{>4{n do.
t=. 1!:1<srcpn
p=. 1!:7<srcpn
t 1!:2<snkpn
p 1!:7<snkpn NB. permissions
else.
if. -.srcpn-:snk do.
1!:5 :: [ <snkpn
copyfiles (srcpn,'\*');snkpn
end.
end.
end.
i.0 0
)
NB. deletefolder y
deletefolder=: 3 : 0
y=. (-PS={:y)}.y NB. remove trailing /
p=. <jpath y
if. 1=#1!:0 p do.
if. 'd'=4{,>4{"1 (1!:0) p do.
deleterecursive y
1!:55 p
end.
end.
i.0 0
)
NB. deletesub y
deleterecursive=: 3 : 0
assert. 3<+/PS=jpath y
ns=. 1!:0 <jpath y,'\*'
for_n. ns do.
s=. jpath y,'\',>{.n
if. 'd'=4{>4{n do.
deleterecursive s
end.
1!:55<s
end.
)
remlev=: 3 : '(y i: PS){.y' NB. remove level from path
roots=: 3 : 0
if. IFWIN do.
a=. 26}.Alpha_j_
b=. (<"0 a),each <':/'
d=. a#~0~:;#each dir each b
(<"0 d),each <':'
else.
<'/'
end.
)
fx=: 3 : 0
s=. ;shorts_jsp_ y
t=. jshortname y
NB.!f=. ;(1>.10-#s)#<' '
f=. ;(1>.10-#s)#<' '
(('file*',jurlencode t)jhab s,f,t),jhbr
)
ev_recent_click=: 3 : 0
if. 0=#SPFILES_jsp_ do.
t=. '</div>',~'<div>The recent files list is empty.'
else.
t=. '</div>',~'<div>',;fx each SPFILES_jsp_
end.
jhrajax t
)
CSS=: 0 : 0
*{font-family:<PC_FONTFIXED>;}
#report{color:red;}
#pathd{color:blue;}
#renamedlg{display:none;}
#deletedlg{display:none;}
#newdlg{display:none;}
)
JS=: 0 : 0
var anchor=null;
function ev_body_load(){jresize();setanchor(true);}
function setpath(t){jform.path.value= t;jbyid("pathd").innerHTML= t;}
function ev_paths_click(){jsubmit();}
function ev_paths_dblclick(){;}
//function ev_recall_click(){jsubmit();}
//function document_recall(v){jform.path.value= v;jbyid("pathd").innerHTML= v;jscdo("recall",v);}
function ev_x_shortcut(){jscdo("cut");}
function ev_c_shortcut(){jscdo("copy");}
function ev_v_shortcut(){jscdo("paste");}
//function ev_2_shortcut(){jbyid("sel").childNodes[0].focus();}
function setanchor(scroll){
t= path.value;
i= t.lastIndexOf('/');
if(i==t.length-1) return;
t= t.substring(i+1);
if(anchor!=null) anchor.style.color= jbyid('sel').firstElementChild.style.color;
anchor= jbyid('files*'+t);
anchor.style.color='red';
if(scroll) anchor.scrollIntoView({behavior: 'auto', block: 'nearest'});
}
//! kill off files/fif/copy
function ev_files_click() // file select
{
clr();
if('/'!=jform.jsid.value.charAt(jform.jsid.value.length-1))
{
var t= jform.path.value;
var i= t.lastIndexOf('/');
setpath(t.substring(0,++i)+jform.jsid.value);
setanchor(false);
}
else
jsubmit();
}
function ev_files_dblclick()
{
if('/'!=jform.path.value[jform.path.value.length-1])
ev_file_dblclick();
}
function clr(){
jbyid('report').innerHTML = " ";
jhide("renamedlg");jhide("deletedlg");jhide('newdlg');
}
function ev_rename_click()
{
clr();
t= jbyid("path").value;
i= t.lastIndexOf('/');
if(i==t.length-1)
{
t= t.substring(0,i); // remove trailing /
i= t.lastIndexOf('/');
}
jbyid("renpx").value= t.substring(0,i+1);
t= t.substring(i+1);
i= t.lastIndexOf('.');
if(i==-1) i= t.length;
jbyid("rennx").value= t.substring(0,i);
jbyid("rensx").value= t.substring(i);
jdlgshow("renamedlg","rennx");
}
function ev_renameclose_click(){clr();}
function ev_new_click(){
clr();
jdlgshow('newdlg','newname');
}
function ev_newclose_click(){clr();}
function ev_del_click()
{
clr();
jbyid("deletename").innerHTML=jform.path.value;
jdlgshow("deletedlg","deleteclose");
}
function ev_deleteclose_click(){clr();}
function ev_recent_click(){jdoajax();}
function ev_recent_click_ajax(t){jbyid('sel').innerHTML= t;}
// handler must be defined - no longer defaults to jsubmit if not defined
function ev_edit_click(){ev_files_dblclick();}
function ev_load_click(){jsubmit();}
function ev_copy_click(){jsubmit();}
function ev_cut_click(){jsubmit();}
function ev_paste_click(){jsubmit();}
function ev_newfile_click(){jsubmit();}
function ev_newfolder_click(){jsubmit();}
function ev_renamedo_click(){jsubmit();}
function ev_renpx_enter(){jscdo('renamedo');}
function ev_rennx_enter(){jscdo('renamedo');}
function ev_rensx_enter(){jscdo('renamedo');}
function ev_newname_enter(){jscdo('newfile');}
function ev_deletedo_click(){jsubmit();}
//function ev_adrecall_click(){adrecall("document",jbyid('path').value,"0");}
function ev_file_click(){
setpath(decodeURIComponent(jsid.value));
//! setanchor(false);
}
function ev_file_dblclick(){
if(null!=window.frameElement){
var a= 'jifr-jijs?jwid='+jform.path.value;
var b= 'jijs?jwid='+encodeURIComponent(jform.path.value);
jijxwindow.newpage(a,'jifr',b);
}else{
var t= 'jijs?jwid='+jform.path.value;
jijxwindow.pageopen(t,t);
}
}
function ev_close_click(){winclose();}
function ev_download_click(){clr();jdoajax(['path']);}
function ev_download_click_ajax_json(t){
if(t.data!=undefined){saveAs(base64ToArrayBuffer(t.data),t.name);}
jbyid('report').innerHTML= t.report;
}
)