-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMobileFileManager.razor.cs
More file actions
1534 lines (1258 loc) · 45.6 KB
/
Copy pathMobileFileManager.razor.cs
File metadata and controls
1534 lines (1258 loc) · 45.6 KB
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using Syncfusion.Blazor.FileManager;
using System.Timers;
namespace TelegramDownloader.Shared.MobileFileManager
{
public partial class MobileFileManager : ComponentBase
{
#region Parameters
[Parameter]
public string Id { get; set; } = string.Empty;
[Parameter]
public bool IsShared { get; set; } = false;
[Parameter]
public bool CanCopy { get; set; } = true;
[Parameter]
public bool CanCut { get; set; } = true;
[Parameter]
public bool CanDelete { get; set; } = true;
[Parameter]
public bool CanRename { get; set; } = true;
[Parameter]
public bool CanCreate { get; set; } = true;
[Parameter]
public bool CanUpload { get; set; } = true;
[Parameter]
public bool CanDownloadToLocal { get; set; } = false;
[Parameter]
public bool CanShareFile { get; set; } = false;
[Parameter]
public bool CanShowInApp { get; set; } = false;
[Parameter]
public bool CanShowUrlMedia { get; set; } = false;
[Parameter]
public bool CanUploadToTelegram { get; set; } = false;
[Parameter]
public bool CanStrm { get; set; } = false;
[Parameter]
public bool CanPreload { get; set; } = false;
[Parameter]
public bool CanAddToPlaylist { get; set; } = false;
[Parameter]
public bool CanSaveToPlaylist { get; set; } = false;
[Parameter]
public string RootFolderName { get; set; } = "Root";
#endregion
#region Events
[Parameter]
public EventCallback<MfmReadEventArgs> OnRead { get; set; }
[Parameter]
public EventCallback<MfmDeleteEventArgs> OnItemsDeleting { get; set; }
[Parameter]
public EventCallback<MfmMoveEventArgs> OnItemsMoving { get; set; }
[Parameter]
public EventCallback<MfmRenameEventArgs> OnItemRenaming { get; set; }
[Parameter]
public EventCallback<MfmFolderCreateEventArgs> OnFolderCreating { get; set; }
[Parameter]
public EventCallback<MfmSearchEventArgs> OnSearching { get; set; }
[Parameter]
public EventCallback<MfmFileOpenEventArgs> OnFileOpen { get; set; }
[Parameter]
public EventCallback<MfmDownloadEventArgs> OnBeforeDownload { get; set; }
[Parameter]
public EventCallback<string[]> OnSelectedItemsChanged { get; set; }
[Parameter]
public EventCallback<MfmDownloadToLocalEventArgs> OnDownloadToLocal { get; set; }
[Parameter]
public EventCallback<MfmShareFileEventArgs> OnShareFile { get; set; }
[Parameter]
public EventCallback<MfmShowInAppEventArgs> OnShowInApp { get; set; }
[Parameter]
public EventCallback<MfmUrlMediaEventArgs> OnUrlMedia { get; set; }
[Parameter]
public EventCallback<MfmUploadToTelegramEventArgs> OnUploadToTelegram { get; set; }
[Parameter]
public EventCallback<MfmUploadToLocalEventArgs> OnUploadToLocal { get; set; }
[Parameter]
public EventCallback<MfmStrmEventArgs> OnStrm { get; set; }
[Parameter]
public EventCallback<MfmPreloadFilesEventArgs> OnPreloadFiles { get; set; }
[Parameter]
public EventCallback<MfmAddToPlaylistEventArgs> OnAddToPlaylist { get; set; }
[Parameter]
public EventCallback<MfmSaveToPlaylistEventArgs> OnSaveToPlaylist { get; set; }
[Parameter]
public EventCallback<string> OnPathChanged { get; set; }
[Parameter]
public EventCallback<MfmFilterChangedEventArgs> OnFilterChanged { get; set; }
// Initial values from URL
[Parameter]
public string InitialSearch { get; set; } = string.Empty;
[Parameter]
public HashSet<string> InitialFilters { get; set; } = new();
[Parameter]
public string InitialSortBy { get; set; } = "Name";
[Parameter]
public bool InitialSortAscending { get; set; } = true;
[Parameter]
public int InitialPage { get; set; } = 1;
#endregion
#region State
public string CurrentPath { get; set; } = "/";
private FileManagerDirectoryContent? CurrentFolder { get; set; } = null; // Tracks current folder with Id for paste operations
private string ViewMode { get; set; } = "list";
private bool IsLoading { get; set; } = false;
private bool ShowSearch { get; set; } = false;
private string SearchText { get; set; } = string.Empty;
private string SortBy { get; set; } = "Name";
private bool SortAscending { get; set; } = true;
private List<FileManagerDirectoryContent> Files { get; set; } = new();
// Cached display files - invalidated when data/filters change
private List<FileManagerDirectoryContent>? _cachedDisplayFiles;
private bool _displayFilesDirty = true;
private List<FileManagerDirectoryContent> DisplayFiles
{
get
{
if (_displayFilesDirty || _cachedDisplayFiles == null)
{
_cachedDisplayFiles = GetDisplayFiles();
_displayFilesDirty = false;
}
return _cachedDisplayFiles;
}
}
private List<FileManagerDirectoryContent> PagedFiles => GetPagedFiles();
private List<FileManagerDirectoryContent> SelectedItems { get; set; } = new();
private HashSet<string> _selectedIds = new(); // O(1) lookup for selection state
private List<FileManagerDirectoryContent> ClipboardItems { get; set; } = new();
private bool IsCutOperation { get; set; } = false;
// Pagination
private int CurrentPage { get; set; } = 1;
private int PageSize { get; set; } = 50;
private int TotalPages => (int)Math.Ceiling((double)DisplayFiles.Count / PageSize);
private int TotalItems => DisplayFiles.Count;
private bool ShowContextMenu { get; set; } = false;
private FileManagerDirectoryContent? ContextMenuItem { get; set; }
private bool ShowRenameDialog { get; set; } = false;
private string RenameText { get; set; } = string.Empty;
private FileManagerDirectoryContent? RenameItem { get; set; }
private ElementReference renameInput;
private bool ShowNewFolderDialog { get; set; } = false;
private string NewFolderName { get; set; } = string.Empty;
private ElementReference newFolderInput;
private ElementReference searchInput;
private bool ShowDetailsPanel { get; set; } = false;
private FileManagerDirectoryContent? DetailsItem { get; set; }
private bool ShowMoreMenu { get; set; } = false;
private bool ShowFabMenu { get; set; } = false;
private bool IsFullscreen { get; set; } = false;
private bool ShowDeleteConfirmDialog { get; set; } = false;
private FileManagerDirectoryContent[] ItemsToDelete { get; set; } = Array.Empty<FileManagerDirectoryContent>();
// File type filter (multiple selection)
private bool ShowFilterDialog { get; set; } = false;
private HashSet<string> SelectedTypeFilters { get; set; } = new();
private List<string> AvailableFileTypes => GetAvailableFileTypes();
private System.Timers.Timer? searchTimer;
// Track previous Id to detect changes
private string _previousId = string.Empty;
#endregion
#region Lifecycle
protected override async Task OnInitializedAsync()
{
_previousId = Id;
// Initialize from URL parameters
if (!string.IsNullOrEmpty(InitialSearch))
{
SearchText = InitialSearch;
ShowSearch = true;
}
if (InitialFilters.Count > 0)
{
SelectedTypeFilters = new HashSet<string>(InitialFilters);
}
// Initialize sort from URL parameters
if (!string.IsNullOrEmpty(InitialSortBy))
{
SortBy = InitialSortBy;
}
SortAscending = InitialSortAscending;
// Initialize page from URL parameters
if (InitialPage > 0)
{
CurrentPage = InitialPage;
}
await LoadFiles();
}
protected override async Task OnParametersSetAsync()
{
// Reload if Id changes
if (_previousId != Id)
{
_previousId = Id;
CurrentPath = "/";
ResetPagination();
ClearSelection();
ClipboardItems.Clear();
SelectedTypeFilters.Clear(); // Clear filters when Id changes
Files.Clear();
await LoadFiles();
}
}
#endregion
#region File Operations
private async Task LoadFiles()
{
IsLoading = true;
StateHasChanged();
try
{
CurrentPath = NormalizePath(CurrentPath);
var args = new MfmReadEventArgs
{
Path = CurrentPath
};
await OnRead.InvokeAsync(args);
// Always update Files list and invalidate cache, even if response is null/empty
// This prevents showing stale data from previous folder
if (args.Response?.Files != null)
{
Files = args.Response.Files.Select(f =>
{
f.FilterPath = NormalizePath(f.FilterPath);
return f;
}).ToList();
}
else
{
// Clear files if response is null to avoid showing stale data
Files = new List<FileManagerDirectoryContent>();
}
InvalidateDisplayFilesCache();
if (args.Response?.CWD != null)
{
CurrentFolder = args.Response.CWD;
if (CurrentFolder.FilterPath != null)
{
CurrentFolder.FilterPath = NormalizePath(CurrentFolder.FilterPath);
}
}
else
{
// Clear CurrentFolder if not in response
CurrentFolder = null;
}
}
finally
{
IsLoading = false;
StateHasChanged();
}
}
public async Task RefreshFilesAsync()
{
await LoadFiles();
}
private void InvalidateDisplayFilesCache()
{
_displayFilesDirty = true;
}
private bool IsItemSelected(FileManagerDirectoryContent file)
{
return _selectedIds.Contains(GetFileUniqueId(file));
}
private string GetFileUniqueId(FileManagerDirectoryContent file)
{
// Use Id if available, otherwise use FilterPath + Name as unique identifier
return !string.IsNullOrEmpty(file.Id) ? file.Id : $"{file.FilterPath}{file.Name}";
}
private List<FileManagerDirectoryContent> GetDisplayFiles()
{
var files = Files ?? new List<FileManagerDirectoryContent>();
// Apply search filter
if (!string.IsNullOrEmpty(SearchText))
{
files = files.Where(f => f.Name.Contains(SearchText, StringComparison.OrdinalIgnoreCase)).ToList();
}
// Apply file type filter (multiple selection)
if (SelectedTypeFilters.Count > 0)
{
files = files.Where(f => SelectedTypeFilters.Contains(GetFileTypeForFilter(f))).ToList();
}
// Apply sorting
files = SortBy switch
{
"Name" => SortAscending ? files.OrderBy(f => !f.IsFile).ThenBy(f => f.Name).ToList() : files.OrderBy(f => !f.IsFile).ThenByDescending(f => f.Name).ToList(),
"Date" => SortAscending ? files.OrderBy(f => !f.IsFile).ThenBy(f => f.DateModified).ToList() : files.OrderBy(f => !f.IsFile).ThenByDescending(f => f.DateModified).ToList(),
"Size" => SortAscending ? files.OrderBy(f => !f.IsFile).ThenBy(f => f.Size).ToList() : files.OrderBy(f => !f.IsFile).ThenByDescending(f => f.Size).ToList(),
"Type" => SortAscending ? files.OrderBy(f => !f.IsFile).ThenBy(f => f.Type).ToList() : files.OrderBy(f => !f.IsFile).ThenByDescending(f => f.Type).ToList(),
_ => files.OrderBy(f => !f.IsFile).ThenBy(f => f.Name).ToList()
};
return files;
}
private List<string> GetAvailableFileTypes()
{
var files = Files ?? new List<FileManagerDirectoryContent>();
var types = new List<string>();
// Add "Folder" type if there are folders
if (files.Any(f => !f.IsFile))
{
types.Add("Folder");
}
// Get distinct file types from files
var fileTypes = files
.Where(f => f.IsFile && !string.IsNullOrEmpty(f.Type))
.Select(f => f.Type)
.Distinct()
.OrderBy(t => t)
.ToList();
types.AddRange(fileTypes);
return types;
}
private string GetFileTypeForFilter(FileManagerDirectoryContent file)
{
if (!file.IsFile)
{
return "Folder";
}
return file.Type ?? string.Empty;
}
private List<FileManagerDirectoryContent> GetPagedFiles()
{
var files = DisplayFiles;
return files.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
}
#endregion
#region Navigation
private List<PathSegment> GetPathSegments()
{
var segments = new List<PathSegment>();
// Root segment always maps to path "/"
segments.Add(new PathSegment { Name = RootFolderName, Path = "/" });
if (CurrentPath != "/")
{
var parts = CurrentPath.Trim('/').Split('/');
var currentPath = "/";
foreach (var part in parts)
{
if (!string.IsNullOrEmpty(part))
{
currentPath += part + "/";
// Skip adding if this is the RootFolderName (already added as first segment)
if (part == RootFolderName && segments.Count == 1)
{
continue;
}
segments.Add(new PathSegment { Name = part, Path = currentPath });
}
}
}
return segments;
}
private async Task NavigateToPath(string path)
{
CurrentPath = path;
ClearSelection();
ResetPagination();
await LoadFiles();
await OnPathChanged.InvokeAsync(CurrentPath);
}
private async Task GoBack()
{
if (CurrentPath == "/") return;
var parts = CurrentPath.Trim('/').Split('/');
if (parts.Length <= 1)
{
// Only one part (e.g., "Files/") - go to root
CurrentPath = "/";
}
else
{
// Remove last part
var newParts = parts.Take(parts.Length - 1).ToArray();
// If only RootFolderName remains (e.g., "Files"), go to root "/"
if (newParts.Length == 1 && newParts[0] == RootFolderName)
{
CurrentPath = "/";
}
else
{
// Keep the leading "/" to match the format used in NavigateToFolder
CurrentPath = "/" + string.Join("/", newParts) + "/";
}
}
ClearSelection();
ResetPagination();
await LoadFiles();
await OnPathChanged.InvokeAsync(CurrentPath);
}
private async Task OnFileClick(FileManagerDirectoryContent file)
{
if (SelectedItems.Count > 0)
{
ToggleSelection(file);
return;
}
if (file.IsFile)
{
await OpenFile(file);
}
else
{
await NavigateToFolder(file);
}
}
private async Task NavigateToFolder(FileManagerDirectoryContent folder)
{
string newPath;
// If folder has FilterPath (e.g., from search results), use it to build correct path
// FilterPath contains the parent path where the folder is located
if (!string.IsNullOrEmpty(folder.FilterPath) && folder.FilterPath != "/")
{
// FilterPath is the parent path, so we append the folder name
var parentPath = NormalizePath(folder.FilterPath);
if (!parentPath.EndsWith("/"))
{
parentPath += "/";
}
newPath = parentPath + folder.Name + "/";
}
else
{
// Normal navigation from current folder
var basePath = CurrentPath;
if (!basePath.EndsWith("/"))
{
basePath += "/";
}
newPath = basePath + folder.Name + "/";
}
if (newPath == CurrentPath)
{
await LoadFiles();
return;
}
// Clear search when navigating to a folder
if (ShowSearch)
{
ShowSearch = false;
SearchText = string.Empty;
}
CurrentPath = newPath;
ClearSelection();
ResetPagination();
await LoadFiles();
await OnPathChanged.InvokeAsync(CurrentPath);
}
private async Task OpenFile(FileManagerDirectoryContent file)
{
var args = new MfmFileOpenEventArgs
{
FileDetails = file
};
await OnFileOpen.InvokeAsync(args);
CloseContextMenu();
}
#endregion
#region Selection
private void OnFileLongPress(FileManagerDirectoryContent file)
{
ContextMenuItem = file;
ShowContextMenu = true;
}
private void ToggleSelection(FileManagerDirectoryContent file)
{
var fileId = GetFileUniqueId(file);
if (_selectedIds.Contains(fileId))
{
SelectedItems.Remove(file);
_selectedIds.Remove(fileId);
}
else
{
SelectedItems.Add(file);
_selectedIds.Add(fileId);
}
OnSelectedItemsChanged.InvokeAsync(_selectedIds.ToArray());
StateHasChanged();
}
private void ClearSelection()
{
SelectedItems.Clear();
_selectedIds.Clear();
OnSelectedItemsChanged.InvokeAsync(Array.Empty<string>());
StateHasChanged();
}
private void SelectAll()
{
SelectedItems = new List<FileManagerDirectoryContent>(DisplayFiles);
_selectedIds = new HashSet<string>(SelectedItems.Select(f => GetFileUniqueId(f)));
ShowMoreMenu = false;
OnSelectedItemsChanged.InvokeAsync(_selectedIds.ToArray());
StateHasChanged();
}
#endregion
#region Clipboard Operations
private void CopySelected()
{
ClipboardItems = new List<FileManagerDirectoryContent>(SelectedItems);
IsCutOperation = false;
ClearSelection();
}
private void CopyItem(FileManagerDirectoryContent item)
{
ClipboardItems = new List<FileManagerDirectoryContent> { item };
IsCutOperation = false;
CloseContextMenu();
}
private void CutSelected()
{
ClipboardItems = new List<FileManagerDirectoryContent>(SelectedItems);
IsCutOperation = true;
ClearSelection();
}
private void CutItem(FileManagerDirectoryContent item)
{
ClipboardItems = new List<FileManagerDirectoryContent> { item };
IsCutOperation = true;
CloseContextMenu();
}
private async Task PasteItems()
{
if (ClipboardItems.Count == 0) return;
// Normalize paths to use forward slashes
var normalizedTargetPath = NormalizePath(CurrentPath);
var normalizedSourcePath = NormalizePath(ClipboardItems.First().FilterPath);
// Normalize FilterPath in clipboard items before sending
var normalizedClipboardItems = ClipboardItems.Select(f =>
{
// Clone to avoid modifying original
return new FileManagerDirectoryContent
{
Id = f.Id,
Name = f.Name,
FilterPath = NormalizePath(f.FilterPath),
FilterId = f.FilterId,
IsFile = f.IsFile,
Size = f.Size,
DateCreated = f.DateCreated,
DateModified = f.DateModified,
Type = f.Type,
HasChild = f.HasChild,
ParentId = f.ParentId
};
}).ToArray();
// Use CurrentFolder from LoadFiles response which has the correct Id
// This is crucial for the database copy operation to set correct ParentId
FileManagerDirectoryContent targetFolder;
if (CurrentFolder != null)
{
targetFolder = new FileManagerDirectoryContent
{
Id = CurrentFolder.Id,
Name = CurrentFolder.Name,
// Preserve empty FilterPath for root folder - don't normalize it to "/"
FilterPath = string.IsNullOrEmpty(CurrentFolder.FilterPath) ? "" : NormalizePath(CurrentFolder.FilterPath),
FilterId = CurrentFolder.FilterId ?? "",
IsFile = false
};
}
else
{
// Fallback for root or if CWD wasn't available
targetFolder = new FileManagerDirectoryContent
{
FilterPath = normalizedTargetPath,
IsFile = false,
Name = normalizedTargetPath.TrimEnd('/').Split('/').LastOrDefault() ?? ""
};
}
var args = new MfmMoveEventArgs
{
Files = normalizedClipboardItems,
SourcePath = normalizedSourcePath,
TargetPath = normalizedTargetPath,
TargetData = targetFolder,
IsCopy = !IsCutOperation
};
await OnItemsMoving.InvokeAsync(args);
ClipboardItems.Clear();
await LoadFiles();
}
private string NormalizePath(string path)
{
if (string.IsNullOrEmpty(path)) return "/";
// Replace backslashes with forward slashes
var normalized = path.Replace("\\", "/");
// Remove duplicate slashes
while (normalized.Contains("//"))
{
normalized = normalized.Replace("//", "/");
}
// Only root path should start with "/", other paths should not have leading "/"
// Server expects: "/" for root, "Files/Folder/" for subfolders
// Don't add leading "/" here - let the caller decide the format
return normalized;
}
#endregion
#region Delete
private void DeleteSelected()
{
if (SelectedItems.Count == 0) return;
ItemsToDelete = SelectedItems.ToArray();
ShowDeleteConfirmDialog = true;
}
private void DeleteItem(FileManagerDirectoryContent item)
{
CloseContextMenu();
ItemsToDelete = new[] { item };
ShowDeleteConfirmDialog = true;
}
private async Task ConfirmDelete()
{
if (ItemsToDelete.Length == 0) return;
var args = new MfmDeleteEventArgs
{
Files = ItemsToDelete,
Path = CurrentPath
};
await OnItemsDeleting.InvokeAsync(args);
CloseDeleteConfirmDialog();
ClearSelection();
await LoadFiles();
}
private void CloseDeleteConfirmDialog()
{
ShowDeleteConfirmDialog = false;
ItemsToDelete = Array.Empty<FileManagerDirectoryContent>();
}
#endregion
#region Rename
private async Task RenameSelected()
{
if (SelectedItems.Count != 1) return;
RenameItem = SelectedItems.First();
RenameText = RenameItem.Name;
ShowRenameDialog = true;
ClearSelection();
await FocusAndSelectRenameInput();
}
private async Task StartRenameItem(FileManagerDirectoryContent item)
{
RenameItem = item;
RenameText = item.Name;
ShowRenameDialog = true;
CloseContextMenu();
await FocusAndSelectRenameInput();
}
private async Task FocusAndSelectRenameInput()
{
StateHasChanged();
await Task.Delay(50);
try
{
await renameInput.FocusAsync();
await JSRuntime.InvokeVoidAsync("eval", "document.activeElement.select()");
}
catch { }
}
private async Task OnRenameKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Enter")
{
await ConfirmRename();
}
else if (e.Key == "Escape")
{
CloseRenameDialog();
}
}
private async Task ConfirmRename()
{
if (RenameItem == null || string.IsNullOrWhiteSpace(RenameText)) return;
var args = new MfmRenameEventArgs
{
File = RenameItem,
NewName = RenameText,
Path = RenameItem.FilterPath
};
await OnItemRenaming.InvokeAsync(args);
CloseRenameDialog();
await LoadFiles();
}
private void CloseRenameDialog()
{
ShowRenameDialog = false;
RenameItem = null;
RenameText = string.Empty;
}
#endregion
#region New Folder
private async Task CreateNewFolder()
{
ShowFabMenu = false;
NewFolderName = "New Folder";
ShowNewFolderDialog = true;
StateHasChanged();
// Wait for the dialog to render, then focus and select all text
await Task.Delay(50);
try
{
await newFolderInput.FocusAsync();
await JSRuntime.InvokeVoidAsync("eval", "document.activeElement.select()");
}
catch { }
}
private async Task OnNewFolderKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Enter")
{
await ConfirmNewFolder();
}
else if (e.Key == "Escape")
{
CloseNewFolderDialog();
}
}
private async Task ConfirmNewFolder()
{
if (string.IsNullOrWhiteSpace(NewFolderName)) return;
// If CurrentFolder is missing or has no Id, refresh from the server first
if (CurrentFolder == null || string.IsNullOrEmpty(CurrentFolder.Id))
{
await LoadFiles();
}
// After refresh, if we still don't have a valid CurrentFolder with Id, abort
if (CurrentFolder == null || string.IsNullOrEmpty(CurrentFolder.Id))
{
return;
}
var parentFolder = new FileManagerDirectoryContent
{
Id = CurrentFolder.Id,
Name = CurrentFolder.Name,
FilterPath = string.IsNullOrEmpty(CurrentFolder.FilterPath) ? "" : NormalizePath(CurrentFolder.FilterPath),
FilterId = CurrentFolder.FilterId ?? "",
IsFile = false
};
var args = new MfmFolderCreateEventArgs
{
FolderName = NewFolderName,
Path = NormalizePath(CurrentPath),
ParentFolder = parentFolder
};
await OnFolderCreating.InvokeAsync(args);
CloseNewFolderDialog();
await LoadFiles();
}
private void CloseNewFolderDialog()
{
ShowNewFolderDialog = false;
NewFolderName = string.Empty;
}
#endregion
#region Download
private async Task DownloadSelected()
{
if (SelectedItems.Count == 0) return;
var args = new MfmDownloadEventArgs
{
Names = SelectedItems.Select(f => f.Name).ToArray(),
Path = CurrentPath,
Files = SelectedItems.ToArray()
};
await OnBeforeDownload.InvokeAsync(args);
ClearSelection();
}
private async Task DownloadItem(FileManagerDirectoryContent item)
{
var args = new MfmDownloadEventArgs
{
Names = new[] { item.Name },
Path = item.FilterPath,
Files = new[] { item }
};
await OnBeforeDownload.InvokeAsync(args);
CloseContextMenu();
}
#endregion
#region Additional Actions
private async Task DownloadToLocalSelected()
{
if (SelectedItems.Count == 0) return;
var args = new MfmDownloadToLocalEventArgs
{
Files = SelectedItems.ToArray(),
Path = CurrentPath
};
await OnDownloadToLocal.InvokeAsync(args);
ClearSelection();
}
private async Task DownloadToLocalItem(FileManagerDirectoryContent item)
{
var args = new MfmDownloadToLocalEventArgs
{
Files = new[] { item },
Path = item.FilterPath
};
await OnDownloadToLocal.InvokeAsync(args);
CloseContextMenu();
}
private async Task ShareFolderItem(FileManagerDirectoryContent item)
{
if (item.IsFile) return; // Only folders can be shared