-
Notifications
You must be signed in to change notification settings - Fork 0
/
intramine_filetree.pl
727 lines (644 loc) · 23.6 KB
/
intramine_filetree.pl
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
# intramine_filetree.pl: a tree display of local files in two columns, with drive selectors.
# Files open with intramine_viewer.pl. If editing has been configured, there will
# be "pencil" icons following editable files.
# A form at top of page allows opening a file by name or partial or full path.
# Note there is no "context" for this page, in terms of a
# default location on disk, so a partial or full path will more often be needed to avoid
# ambiguity (eg the form has no idea which "main.cpp" you might want, but
# entering "flattreeview\main.cpp" would probably bring up the right file).
# Uses http://www.abeautifulsite.net/jquery-file-tree/
# which is invoked by files.js#startFileTreeUp().
# perl C:\perlprogs\mine\intramine_filetree.pl 81 43131
use strict;
use warnings;
use utf8;
use HTML::Entities;
#use Win32::FindFile;
#use Time::HiRes qw ( time ); # For testing
use Time::Piece;
use Path::Tiny qw(path);
use lib path($0)->absolute->parent->child('libs')->stringify;
use common;
use swarmserver;
use win_wide_filepaths;
use ext; # for ext.pm#IsTextOrImageExtensionNoPeriod() and ext.pm#IsImageExtensionNoPeriod()
#binmode(STDOUT, ":unix:utf8");
$| = 1;
my $PAGENAME = '';
my $SHORTNAME = '';
my $server_port = '';
my $port_listen = '';
SSInitialize(\$PAGENAME, \$SHORTNAME, \$server_port, \$port_listen);
my $FILESIZEUNITS = [qw(B KB MB GB TB PB)];
my $CSS_DIR = FullDirectoryPath('CSS_DIR');
my $JS_DIR = FullDirectoryPath('JS_DIR');
my $UseAppForLocalEditing = CVal('USE_APP_FOR_EDITING');
my $UseAppForRemoteEditing = CVal('USE_APP_FOR_REMOTE_EDITING');
my $AllowLocalEditing = CVal('ALLOW_LOCAL_EDITING');
my $AllowRemoteEditing = CVal('ALLOW_REMOTE_EDITING');
# For the file name / datetime / file size span width array @widths.
my $FILENAMEWIDTH = 0;
my $DATETIMEWIDTH = 1;
my $SIZEWIDTH = 2;
# For file names when making a new file.
my $GOODNAME = 1; # eg file.txt
my $BADNAME = 2; # eg COM7
my $BADCHAR = 3; # eg file?.txt
my $MISSINGNAME = 4; # ''
my $kLOGMESSAGES = 0; # 1 == Log Output() messages
my $kDISPLAYMESSAGES = 0; # 1 == print messages from Output() to console window
# Log is at logs/IntraMine/$SHORTNAME $port_listen datestamp.txt in the IntraMine folder.
# Use the Output() sub for routine log/print.
StartNewLog($kLOGMESSAGES, $kDISPLAYMESSAGES);
Output("Starting $SHORTNAME on port $port_listen\n\n");
my %RequestAction;
$RequestAction{'req|main'} = \&FileTreePage; # req=main
$RequestAction{'dir'} = \&GetDirsAndFiles; # $formH->{'dir'} is directory path
$RequestAction{'req|new'} = \&NewFile; # req=new, $formH->{'path'} holds new path
# Over to swarmserver.pm.
MainLoop(\%RequestAction);
################### subs
# The main page showing two file lists, with drive selectors and a text field to enter
# a file path or name for quick opening.
# This is under "Files" in the top navigation on an IntraMine page.
# URL on the IntraMine box: http://localhost:81/Files
# 2019-12-03 18_09_20-Local Files.png
sub FileTreePage {
my ($obj, $formH, $peeraddress) = @_;
my $theBody = <<'FINIS';
<!doctype html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- <meta http-equiv="content-type" content="text/html; charset=windows-1252"> -->
<title>Files</title>
<link rel="stylesheet" type="text/css" href="main.css" />
<link rel="stylesheet" type="text/css" href="twocolumns.css" />
<link rel="stylesheet" type="text/css" href="tooltip.css" />
<link rel="stylesheet" type="text/css" href="forms.css" />
<link rel="stylesheet" type="text/css" href="jqueryFileTree.css" />
<link rel="stylesheet" type="text/css" href="newFileButton.css" />
</head>
<body>
_TOPNAV_
<table>
<tr><td>
<!-- Simple input field with Open button, allow opening file from a typed-in path. -->
<form class="form-container" id="fileOpenForm" method="get" action=_ACTION_ onsubmit="openUserPath(this); return false;">
<input id="openfile" class="form-field" type="search" name="openthis" placeholder='file name or partial or full path' required />
<input class="submit-button" type="submit" value="Open" />
</form>
</td><td>
<input id="new-button" class="submit-button" type="submit" value="New..." style="display: inline; margin-top: -22px; margin-left: 12px;" />
</td></tr></table>
_NEWFILEPICKER_
<div id='scrollAdjustedHeight'>
<div id='fileTreeLeft'>
<select id="driveselector_1" name="drive selector" onchange="driveChanged('scrollDriveListLeft', this.value);">
_DRIVESELECTOROPTIONS_
</select><span id='sort_by'>Sort files by: </span>
<!-- <select id="sort_1" name="sort order"> -->
<select id="sort_1" name="sort order" onchange="reSortExpandedDirectoriesOnSortChange();">
<option value='name_ascending' selected>Name A_Z</option>
<option value='name_descending'>Name Z_A</option>
<option value='date_newest'>Date newest</option>
<option value='date_oldest'>Date oldest</option>
<option value='size_smallest'>Size smallest</option>
<option value='size_largest'>Size largest</option>
<option value='extension'>File extension</option>
</select>
<span id="errorMessage"> </span>
<div id='scrollDriveListLeft'>
</div>
</div>
<div id='fileTreeRight'>
<div id="tocShrinkExpandDiv"><img src="707788g4.png" id="tocShrinkExpand" onclick="toggleRightListWidth();"></div>
<select id="driveselector_2" name="drive selector" onchange="driveChanged('scrollDriveListRight', this.value);">
_DRIVESELECTOROPTIONS_
</select>
<div id='scrollDriveListRight'>
</div>
</div>
</div>
<script>
let weAreRemote = _WEAREREMOTE_;
let allowEditing = _ALLOW_EDITING;
let useAppForEditing = _USE_APP_FOR_EDITING;
let thePort = '_THEPORT_';
let clientIPAddress = '_CLIENT_IP_ADDRESS_';
let viewerShortName = '_VIEWERSHORTNAME_';
let openerShortName = '_OPENERSHORTNAME_';
let editorShortName = '_EDITORSHORTNAME_';
let linkerShortName = '_LINKERSHORTNAME_';
let videoShortName = '_VIDEOSHORTNAME_';
let contentID = 'scrollAdjustedHeight';
let errorID = "errorMessage";
let initialDirectoryPath = '_INITIALDIR_';
</script>
<script src="intramine_config.js"></script>
<script src="spinner.js"></script>
<script src="websockets.js"></script>
<script src="topnav.js"></script>
<script src="todoFlash.js"></script>
<script src="chatFlash.js"></script>
<script src="tooltip.js"></script>
<script src="jquery-3.4.1.min.js"></script>
<script src="jquery.easing.1.3.min.js"></script>
<script src="jqueryFileTree.js"></script>
<script src="viewerLinks.js"></script>
<script src="newFileButton.js"></script>
<script src="files.js"></script>
</body></html>
FINIS
my $topNav = TopNav($PAGENAME);
$theBody =~ s!_TOPNAV_!$topNav!;
$theBody =~ s!_CSS_DIR_!$CSS_DIR!g;
$theBody =~ s!_JS_DIR_!$JS_DIR!g;
# $peeraddress eq '127.0.0.1' determines whether we are local.
# The IPv4 Address for this server (eg 192.168.0.14);
my $serverAddr = ServerAddress();
# Form action.
my $action = "http://$serverAddr:$port_listen/?rddm=1";
$theBody =~ s!_ACTION_!\'$action\'!;
# Put in drive selector options (two of them). See swarmserver.pm#DriveSelectorOptions().
my $driveSelectorOptions = DriveSelectorOptions();
$theBody =~ s!_DRIVESELECTOROPTIONS_!$driveSelectorOptions!g;
my $clientIsRemote = 0;
# If client is on the server then peeraddress can be either 127.0.0.1 or $serverAddr:
# if client is NOT on the server then peeraddress is not 127.0.0.1 and differs from $serverAddr.
if ($peeraddress ne '127.0.0.1' && $peeraddress ne $serverAddr) #if ($peeraddress ne $serverAddr)
#if ($peeraddress ne '127.0.0.1')
{
$clientIsRemote = 1;
}
my $allowEditing = (($clientIsRemote && $AllowRemoteEditing)
|| (!$clientIsRemote && $AllowLocalEditing));
my $useAppForEditing = 0;
if ($allowEditing)
{
$useAppForEditing = (($clientIsRemote && $UseAppForRemoteEditing)
|| (!$clientIsRemote && $UseAppForLocalEditing));
}
my $amRemoteValue = $clientIsRemote ? 'true' : 'false';
my $tfAllowEditing = ($allowEditing) ? 'true' : 'false';
my $tfUseAppForEditing = ($useAppForEditing) ? 'true' : 'false';
$theBody =~ s!_WEAREREMOTE_!$amRemoteValue!;
$theBody =~ s!_ALLOW_EDITING!$tfAllowEditing!;
$theBody =~ s!_USE_APP_FOR_EDITING!$tfUseAppForEditing!;
$theBody =~ s!_THEPORT_!$port_listen!;
$theBody =~ s!_CLIENT_IP_ADDRESS_!$peeraddress!;
my $viewerShortName = CVal('VIEWERSHORTNAME');
my $openerShortName = CVal('OPENERSHORTNAME');
my $editorShortName = CVal('EDITORSHORTNAME');
my $linkerShortName = CVal('LINKERSHORTNAME');
my $videoShortName = CVal('VIDEOSHORTNAME');
$theBody =~ s!_VIEWERSHORTNAME_!$viewerShortName!;
$theBody =~ s!_OPENERSHORTNAME_!$openerShortName!;
$theBody =~ s!_EDITORSHORTNAME_!$editorShortName!;
$theBody =~ s!_LINKERSHORTNAME_!$linkerShortName!;
$theBody =~ s!_VIDEOSHORTNAME_!$videoShortName!;
my $initialDirectory = defined($formH->{'directory'}) ? $formH->{'directory'}: '';
# Encode: this goes with decodeURIComponent at top of files.js#showDirectory().
$initialDirectory = uri_encode($initialDirectory);
# The Files page will open to the $initialDirectory if provided. This is used
# when opening directory links (see intramine_viewer.pl#OpenDirectory() etc).
$theBody =~ s!_INITIALDIR_!$initialDirectory!;
my $newFilePicker = NewFilePicker();
$theBody =~ s!_NEWFILEPICKER_!$newFilePicker!;
# Put in main IP, main port, our short name for JavaScript.
PutPortsAndShortnameAtEndOfBody(\$theBody); # swarmserver.pm#PutPortsAndShortnameAtEndOfBody()
return $theBody;
}
# Return a list of directories and files for the current drive or directory.
# Called by jqueryFileTree.js on line 69: "$.post(o.script, { dir: t, rmt: o.remote,..."
# which sends a "dir" request to the program (see %RequestAction above).
sub GetDirsAndFiles {
my ($obj, $formH, $peeraddress) = @_;
my $dir = $formH->{'dir'};
my $result = '';
Output("GetDirsAndFiles request for dir: |$dir|\n");
if (FileOrDirExistsWide($dir) != 2)
{
return(' '); # return something (but not too much), to avoid 404
}
my @folders;
my @files;
my @modDates;
my @fileSizes;
GetFoldersFilesDatesAndSizes($dir, \@folders, \@files, \@modDates, \@fileSizes);
my $numFolders = @folders;
my $numFiles = @files;
my $total = $numFolders + $numFiles;
if ($total)
{
$result = "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
}
if ($numFolders)
{
PutFolders($dir, \@folders, \$result);
}
if ($numFiles)
{
my $sortOrder = (defined($formH->{'sort'})) ? $formH->{'sort'}: '';
SortFilesDatesAndSizes($sortOrder, \@files, \@modDates, \@fileSizes);
my $rmt = defined($formH->{'rmt'}) ? $formH->{'rmt'}: 'undef';
if ($rmt eq 'false')
{
$rmt = 0;
}
else
{
$rmt = 1;
}
# TEST ONLY
#print("\$rmt: |$rmt|\n");
my @modDatesStrings;
my @fileSizesStrings;
my @widths;
GetDateSizeStringsAndColumnWidths(\@files, \@modDates, \@fileSizes,
\@modDatesStrings, \@fileSizesStrings, \@widths);
PutFiles($dir, $formH, $rmt, \@files, \@modDatesStrings, \@fileSizesStrings, \@widths, \$result);
}
if ($total)
{
$result .= "</ul>\n";
}
if ($result eq '')
{
$result = ' ' ; # return something (but not too much), to avoid 404
}
return($result);
}
sub GetFoldersFilesDatesAndSizes {
my ($dir, $foldersA, $filesA, $modDatesA, $fileSizesA) = @_;
my $fullDir = $dir . '*';
# win_wide_filepaths.pm#FindFileWide().
my @allEntries = FindFileWide($fullDir);
my $numEntries = @allEntries;
if (!$numEntries)
{
return;
}
# Break entries into folders and files.
for (my $i = 0; $i < @allEntries; ++$i)
{
my $theName = $allEntries[$i];
# Not needed: $fileName = decode("utf8", $fileName);
my $fullPath = "$dir$theName";
if (FileOrDirExistsWide($fullPath) == 2)
{
if ($theName !~ m!^\.\.?$! && substr($theName, 0, 1) ne '$')
{
push @$foldersA, $theName;
}
}
else
{
if ($theName =~ m!\.\w+$! && $theName !~ m!\.sys$! && substr($theName, 0, 1) ne '$')
{
push @$filesA, $theName;
}
}
}
my $numFiles = @$filesA;
if ($numFiles)
{
FileDatesAndSizes($dir, $filesA, $modDatesA, $fileSizesA);
}
}
sub PutFolders {
my ($dir, $foldersA, $resultR) = @_;
foreach my $folderName (sort {lc $a cmp lc $b} @$foldersA)
{
next if (FileOrDirExistsWide($dir . $folderName) == 0);
$$resultR .= '<li class="directory collapsed"><a href="#" rel="' .
&HTML::Entities::encode($dir . $folderName) . '/">' .
&HTML::Entities::encode($folderName) . '</a></li>';
}
}
sub SortFilesDatesAndSizes {
my ($sortOrder, $filesA, $modDatesA, $fileSizesA) = @_;
my @idx;
if ($sortOrder eq 'size_smallest')
{
@idx = sort {$fileSizesA->[$a] <=> $fileSizesA->[$b]} 0..$#$fileSizesA;
}
elsif ($sortOrder eq 'size_largest')
{
@idx = sort {$fileSizesA->[$b] <=> $fileSizesA->[$a]} 0..$#$fileSizesA;
}
elsif ($sortOrder eq 'date_newest')
{
# Newest first, so [$b] <=> [$a].
@idx = sort {$modDatesA->[$b] <=> $modDatesA->[$a]} 0..$#$modDatesA;
}
elsif ($sortOrder eq 'date_oldest')
{
# Newest first, so [$b] <=> [$a].
@idx = sort {$modDatesA->[$a] <=> $modDatesA->[$b]} 0..$#$modDatesA;
}
elsif ($sortOrder eq 'name_descending')
{
@idx = sort {lc $filesA->[$b] cmp lc $filesA->[$a]} 0..$#$filesA;
}
elsif ($sortOrder eq 'extension')
{
my @extensions;
Extensions($filesA, \@extensions);
@idx = sort{$extensions[$a] cmp $extensions[$b]} 0..$#extensions;
}
else # 'name_ascending', the default
{
@idx = sort {lc $filesA->[$a] cmp lc $filesA->[$b]} 0..$#$filesA;
}
@$filesA = @$filesA[@idx];
@$modDatesA = @$modDatesA[@idx];
@$fileSizesA = @$fileSizesA[@idx];
}
sub GetDateSizeStringsAndColumnWidths {
my ($filesA, $modDatesA, $fileSizesA, $modDatesStringsA, $fileSizesStringsA, $widthsA) = @_;
my $numFiles = @$filesA;
my $filesWidth = 0;
my $modDatesWidth = 0;
my $fileSizesWidth = 0;
for (my $i = 0; $i < $numFiles; ++$i)
{
my $widthFiles = length($filesA->[$i]);
if ($filesWidth < $widthFiles)
{
$filesWidth = $widthFiles;
}
my $dateTimeString = DateTimeString($modDatesA->[$i]);
push @$modDatesStringsA, $dateTimeString;
my $widthMDate = length($dateTimeString);
if ($modDatesWidth < $widthMDate)
{
$modDatesWidth = $widthMDate;
}
my $sizeString = SizeInBytesString($fileSizesA->[$i]);
push @$fileSizesStringsA, $sizeString;
my $widthSize = length($sizeString);
if ($fileSizesWidth < $widthSize)
{
$fileSizesWidth = $widthSize;
}
}
# Put file name, date, size in separate spans with fixed width in characters ('ch').
# Add 2 to $filesWidth for hover icons or edit pencil.
$filesWidth += 2;
my $wF = $filesWidth . 'ch';
my $wD = $modDatesWidth . 'ch';
my $wS = $fileSizesWidth . 'ch';
$widthsA->[$FILENAMEWIDTH] = $wF;
$widthsA->[$DATETIMEWIDTH] = $wD;
$widthsA->[$SIZEWIDTH] = $wS;
}
# For each file: file icon based on extension, file name, datetime, size in bytes.
# Fixed-width inline-block <span>s are used to align entries.
sub PutFiles {
my ($dir, $formH, $rmt, $filesA, $modDatesStringsA, $fileSizesStringsA, $widthsA, $resultR) = @_;
my $numFiles = @$filesA;
my $clientIsRemote = ($formH->{'rmt'} eq 'false') ? 0 : 1;
my $allowEditing = ($formH->{'edt'} eq 'false') ? 0 : 1;
my $useAppForEditing = ($formH->{'app'} eq 'false') ? 0 : 1;
my $serverAddr = ServerAddress();
for (my $i = 0; $i < $numFiles; ++$i)
{
my $file = $filesA->[$i];
next if (FileOrDirExistsWide($dir . $file) == 0);
my $modDate = $modDatesStringsA->[$i];
my $size = $fileSizesStringsA->[$i];
$file =~ /\.([^.]+)$/;
my $ext = $1;
# Gray out unsuported file types. Show thumbnail on hover for images.
# Note videos cannot be viewed remotely (at least for now).
if (defined($ext) && IsTextDocxPdfOrImageOrVideoExtensionNoPeriod($ext)
&& !(IsVideoExtensionNoPeriod($ext) && $rmt))
{
if (IsImageExtensionNoPeriod($ext))
{
$$resultR .= ImageLine($serverAddr, $dir, $file, $ext, $modDate, $size, $widthsA);
}
elsif (IsVideoExtensionNoPeriod($ext))
{
$$resultR .= VideoLine($serverAddr, $dir, $file, $ext, $modDate, $size, $widthsA);
}
else # Text, for the most part - could also be pdf or docx
{
$$resultR .= TextDocxPdfLine($dir, $file, $ext, $modDate, $size,
$allowEditing, $clientIsRemote, $widthsA);
}
}
else # Unsupported type (and remote videos), can't produce a read-only HTML view. So no link.
{
my $dateSpanStart = "<span style='display: inline-block; width: $widthsA->[$DATETIMEWIDTH];'>";
my $sizesSpanStart = "<span style='display: inline-block; width: $widthsA->[$SIZEWIDTH];'>";
my $endSpan = '</span>';
my $fileName = &HTML::Entities::encode($file);
$$resultR .= '<li class="file ext_' . $ext . '">' .
"<span class='unsupported' style='display: inline-block; width: $widthsA->[$FILENAMEWIDTH];'>" . $fileName . '</span>' .
$dateSpanStart . $modDate . $endSpan . $sizesSpanStart . $size . $endSpan . '</li>';
}
}
}
sub FileDatesAndSizes {
my ($dir, $filesA, $modDatesA, $sizesA) = @_;
my $numFiles = @$filesA;
for (my $i = 0; $i < $numFiles; ++$i)
{
my $file = $filesA->[$i];
my @a;
GetFileModTimeAndSizeWide($dir . $file, \@a);
if (!defined($a[0]))
{
$a[0] = '0';
}
if (!defined($a[1]))
{
$a[1] = '';
}
push @$modDatesA, $a[0];
push @$sizesA, $a[1];
}
}
sub Extensions {
my ($filesA, $extA) = @_;
my $numFiles = @$filesA;
for (my $i = 0; $i < $numFiles; ++$i)
{
my $file = $filesA->[$i];
my $ext;
if ($file =~ m!\.(\w+)$!)
{
$ext = lc $1;
}
else
{
$ext = '_NONE';
}
push @$extA, $ext;
}
}
# Images get showhint() "hover" event listeners, as well as a link to open in a new tab.
# Put file name, mod date, and size in separate fixed-width spans for alignment.
sub ImageLine {
my ($serverAddr, $dir, $file, $ext, $modDate, $size, $widthsA) = @_;
my $imagePath = $dir . $file;
my $imageHoverPath = $imagePath;
$imageHoverPath =~ s!%!%25!g;
my $imageName = $file;
$imageName = &HTML::Entities::encode($imageName); # YES this works fine.
$imagePath = &HTML::Entities::encode($imagePath);
$imageHoverPath = &HTML::Entities::encode($imageHoverPath);
my $serverImageHoverPath = "http://$serverAddr:$port_listen/$imageHoverPath";
my $leftHoverImg = "<img src='http://$serverAddr:$port_listen/hoverleft.png' width='17' height='12'>";
my $rightHoverImg = "<img src='http://$serverAddr:$port_listen/hoverright.png' width='17' height='12'>";
my $result = '<li class="file ext_' . $ext . '">' .
"<span style='display: inline-block; width: $widthsA->[$FILENAMEWIDTH];'>" .
'<a href="#" rel="' . $imagePath . '"' . "onmouseOver=\"showhint('<img src="$serverImageHoverPath">', this, event, '250px', true);\"" . '>' .
"$leftHoverImg$imageName$rightHoverImg" . '</a></span>' .
"<span style='display: inline-block; width: $widthsA->[$DATETIMEWIDTH];'>" .
$modDate . '</span>' .
"<span style='display: inline-block; width: $widthsA->[$SIZEWIDTH];'>" .
$size . '</span></li>';
return($result);
}
sub VideoLine {
my ($serverAddr, $dir, $file, $ext, $modDate, $size, $widthsA) = @_;
my $imagePath = $dir . $file . 'VIDEO';
my $imageName = $file;
$imageName = &HTML::Entities::encode($imageName); # YES this works fine.
$imagePath = &HTML::Entities::encode($imagePath);
my $result = '<li class="file ext_' . $ext . '">' .
"<span style='display: inline-block; width: $widthsA->[$FILENAMEWIDTH];'>" .
'<a href="#" rel="' . $imagePath . '"' . '>' .
"$imageName" . '</a></span>' .
"<span style='display: inline-block; width: $widthsA->[$DATETIMEWIDTH];'>" .
$modDate . '</span>' .
"<span style='display: inline-block; width: $widthsA->[$SIZEWIDTH];'>" .
$size . '</span></li>';
return($result);
}
# Put link on file name, with optional edit pencil link.
# PDF and docx can't be edited on a remote PC, hence a bit of special handling.
# Put file name, mod date, and size in separate fixed-width spans for alignment.
sub TextDocxPdfLine {
my ($dir, $file, $ext, $modDate, $size, $allowEditing, $clientIsRemote, $widthsA) = @_;
my $filePath = &HTML::Entities::encode($dir . $file);
my $fileName = &HTML::Entities::encode($file);
my $result = '';
# No editing if config says no, or it's pdf or docx on a remote PC.
if (!$allowEditing || ($clientIsRemote && $ext =~ m!^(docx|pdf)!i))
{
$result .= '<li class="file ext_' . $ext . '">' .
"<span style='display: inline-block; width: $widthsA->[$FILENAMEWIDTH];'>" .
'<a href="#" rel="' . $filePath . '">' .
$fileName . '</a>' . '</span>' .
"<span style='display: inline-block; width: $widthsA->[$DATETIMEWIDTH];'>" .
$modDate . '</span>' .
"<span style='display: inline-block; width: $widthsA->[$SIZEWIDTH];'>" .
$size . '</span></li>';
}
else # editing allowed
{
$result .= '<li class="file ext_' . $ext . '">' .
"<span style='display: inline-block; width: $widthsA->[$FILENAMEWIDTH];'>" .
'<a href="#" rel="' . $filePath . '">' .
$fileName . '</a>' .
'<a href="#"><img src="edit1.png" width="17" height="12" rel="' .
$filePath . '" />' . '</a>' . '</span>' .
"<span style='display: inline-block; width: $widthsA->[$DATETIMEWIDTH];'>" .
$modDate . '</span>' .
"<span style='display: inline-block; width: $widthsA->[$SIZEWIDTH];'>" .
$size . '</span></li>';
}
return($result);
}
# This is a simplified version of the file picker, file links are omitted here
# and there is a text field for entering the name of the new file.
sub NewFilePicker {
my $theSource = <<'FINIS';
<!--<form id="dirform">-->
<div id='dirpickerMainContainer'>
<p id="directoryPickerTitle">New File</p>
<div id='dirpicker'>
<div id="scrollAdjustedHeightDirPicker">
<div id="fileTreeNew">
<select id="driveselector_3" name="drive selector" onchange="driveChangedNew('scrollDriveListNew', this.value);">
_DRIVESELECTOROPTIONS_
</select>
<div id='scrollDriveListNew'>placeholder for drive list
</div>
</div>
</div>
<div id="pickerDisplayDiv">Selected directory: <span id="pickerDisplayedDirectory"></span></div>
<div id="newFileDiv">File name: <input type="text" id="newFileName" name="newFileName" required minlength="3"></div>
</div>
<div id="okCancelHolder">
<input type="button" id="dirOkButton" value="OK" onclick="setFullPathFromPicker(); return false;" />
<input type="button" id="dirCancelButton" value="Cancel" onclick="hideNewFilePicker(); return false;" />
</div>
<input type="hidden" id="newFullPathField" name="newFullPathField" />
</div>
<!--</form>-->
FINIS
# Put a list of drives in the drive selector.
my $driveSelectorOptions = DriveSelectorOptions();
$theSource =~ s!_DRIVESELECTOROPTIONS_!$driveSelectorOptions!g;
return $theSource;
}
# Returns ok, noname, badname, badchar, exists, error (latter if system trouble).
# Note this will not overwrite an existing file unless $formH->{'allowOverwrite'} is defined.
# (That's currently not supported.)
sub NewFile {
my ($obj, $formH, $peeraddress) = @_;
my $path = (defined($formH->{'path'})) ? $formH->{'path'} : '';
if ($path eq '')
{
return('nopath');
}
my $fileName = FileNameFromPath($path);
my $nameStatus = IsGoodFileName($fileName);
if ($nameStatus == $BADNAME)
{
return('badname');
}
elsif ($nameStatus == $BADCHAR)
{
return('badchar');
}
elsif ($nameStatus == $MISSINGNAME)
{
return('noname');
}
if (FileOrDirExistsWide($path) == 1 && !defined($formH->{'allowOverwrite'}))
{
return('exists');
}
if (!WriteTextFileWide($path, ''))
{
return('error');
}
return('ok');
}
# Returns
# my $GOODNAME = 1; # eg file.txt
# my $BADNAME = 2; # eg COM7
# my $BADCHAR = 3; # eg file?.txt
# my $MISSINGNAME = 4; # ''
sub IsGoodFileName {
my ($fileName) = @_;
my $result = $GOODNAME;
if ($fileName eq '')
{
$result = $MISSINGNAME;
}
elsif ($fileName =~ m!^CON|PRN|AUX|NUL|COM0|COM1|COM2|COM3|COM4|COM5|COM6|COM7|COM8|COM9|LPT0|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|LPT7|LPT8|LPT9$!i)
{
$result = $BADNAME;
}
elsif ($fileName =~ m!["*:<>?/\\|]!)
{
$result = $BADCHAR;
}
return($result);
}