-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1116 lines (949 loc) · 40.8 KB
/
Copy pathapp.js
File metadata and controls
1116 lines (949 loc) · 40.8 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
/**
* Navigateur de fichiers — JavaScript vanilla, zéro dépendance
* Gère la navigation dans les dossiers, le partage de fichiers, et la copie des liens
*/
// Chemin courant dans le navigateur (relatif à BASE_PATH côté serveur)
let currentPath = '';
// Options d'expiration prédéfinies (valeur en heures, label affiché)
const DUREES_EXPIRATION = [
{ value: '', label: 'Jamais' },
{ value: '24', label: '1 jour' },
{ value: '168', label: '1 semaine' },
{ value: '720', label: '1 mois' },
{ value: 'custom', label: 'Perso…' },
];
// Token CSRF pour les requêtes POST
const CSRF_TOKEN = document.querySelector('meta[name="csrf-token"]')?.content || '';
function escapeHtml(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// Au chargement de la page
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
const openPath = params.get('open') || '';
navigateTo(openPath);
});
/**
* Navigue vers un répertoire donné et met à jour l'affichage
*/
async function navigateTo(path) {
currentPath = path;
const list = document.getElementById('file-list');
if (list) list.classList.add('loading');
try {
const resp = await fetch('/share/ctrl.php?cmd=browse&path=' + encodeURIComponent(path));
if (!resp.ok) {
showToast('Erreur HTTP ' + resp.status, 'error');
return;
}
const data = await resp.json();
if (data.error) {
showToast('Erreur : ' + data.error, 'error');
return;
}
afficherBreadcrumb(path);
afficherFichiers(data.entries);
} catch (e) {
console.error('navigateTo error:', e);
showToast('Erreur de connexion au serveur', 'error');
} finally {
if (list) list.classList.remove('loading');
}
}
/**
* Construit le fil d'Ariane cliquable
*/
function afficherBreadcrumb(path) {
const container = document.getElementById('breadcrumb');
container.innerHTML = '';
// Lien racine
const rootLink = document.createElement('a');
rootLink.textContent = 'Fichiers';
rootLink.href = '#';
rootLink.addEventListener('click', (e) => { e.preventDefault(); navigateTo(''); });
container.appendChild(rootLink);
if (!path) return;
const parts = path.split('/').filter(Boolean);
let cumul = '';
parts.forEach((part, i) => {
const sep = document.createElement('span');
sep.className = 'sep';
sep.textContent = '/';
container.appendChild(sep);
cumul += (cumul ? '/' : '') + part;
if (i < parts.length - 1) {
const link = document.createElement('a');
link.textContent = part;
link.href = '#';
const target = cumul;
link.addEventListener('click', (e) => { e.preventDefault(); navigateTo(target); });
container.appendChild(link);
} else {
const span = document.createElement('span');
span.className = 'current';
span.textContent = part;
container.appendChild(span);
}
});
}
/**
* Affiche la liste des fichiers et dossiers dans le panel
*/
function afficherFichiers(entries) {
const list = document.getElementById('file-list');
list.innerHTML = '';
const filterInput = document.getElementById('file-filter');
if (filterInput) filterInput.value = '';
// Filtrer les fichiers cachés (commençant par un point)
entries = entries.filter(e => !e.name.startsWith('.'));
// Bouton remonter (..) si on est dans un sous-dossier — toute la ligne est cliquable
if (currentPath) {
const li = creerElement('li', 'file-item is-clickable');
li.addEventListener('click', remonter);
const info = creerElement('div', 'file-info');
const icon = creerElement('span', 'file-icon is-up');
icon.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 19V5M5 12l7-7 7 7"/></svg>';
const name = creerElement('span', 'file-name is-folder');
name.textContent = '..';
info.append(icon, name);
li.appendChild(info);
list.appendChild(li);
}
// Message si le dossier est vide
if (entries.length === 0) {
const li = creerElement('li', 'file-item');
li.innerHTML = '<div class="empty-msg"><span class="empty-icon">📭</span>' +
(currentPath ? 'Dossier vide' : 'Aucun fichier visible') + '</div>';
list.appendChild(li);
return;
}
// Afficher chaque entrée (fichier ou dossier)
entries.forEach(entry => {
const isFolder = entry.type === 'folder';
const entryPath = currentPath ? currentPath + '/' + entry.name : entry.name;
const li = creerElement('li', 'file-item' + (isFolder ? ' is-clickable' : ''));
// Clic sur toute la ligne pour ouvrir un dossier
if (isFolder) {
li.addEventListener('click', (e) => {
// Ne pas naviguer si on clique sur le bouton Partager ou le formulaire
if (e.target.closest('.file-actions')) return;
navigateTo(entryPath);
});
}
// Icône
const icon = creerElement('span', 'file-icon ' + (isFolder ? 'is-folder' : 'is-file'));
if (isFolder) {
icon.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" style="color:var(--accent)"><path d="M2 6a2 2 0 012-2h5l2 2h9a2 2 0 012 2v10a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"/></svg>';
} else {
icon.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" style="color:var(--blue)"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>';
}
// Nom
const nameSpan = creerElement('span', isFolder ? 'file-name is-folder' : 'file-name');
nameSpan.textContent = entry.name;
// Taille
const sizeSpan = creerElement('span', 'file-size');
sizeSpan.textContent = isFolder ? '' : formatTaille(entry.size);
const info = creerElement('div', 'file-info');
info.append(icon, nameSpan, sizeSpan);
// Bouton Partager
const actions = creerElement('div', 'file-actions');
const shareBtn = creerElement('button', 'btn btn-share-icon');
shareBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M4 12v8a2 2 0 002 2h12a2 2 0 002-2v-8"/><polyline points="16 6 12 2 8 6"/><line x1="12" y1="2" x2="12" y2="15"/></svg>';
shareBtn.setAttribute('title', 'Partager');
shareBtn.setAttribute('aria-label', 'Partager');
shareBtn.addEventListener('click', (e) => {
e.stopPropagation();
ouvrirShareSheet(entryPath, entry.name);
});
actions.appendChild(shareBtn);
li.append(info, actions);
list.appendChild(li);
});
}
/**
* Remonte d'un niveau dans l'arborescence
*/
function remonter() {
const parts = currentPath.split('/').filter(Boolean);
parts.pop();
navigateTo(parts.join('/'));
}
/**
* Ouvre le bottom sheet de création de lien
*/
function ouvrirShareSheet(path, name) {
fermerShareSheet();
let selectedExpiry = '';
const overlay = creerElement('div', 'sheet-overlay');
overlay.id = 'sheet-overlay';
overlay.addEventListener('click', fermerShareSheet);
const sheet = creerElement('div', 'share-sheet');
sheet.id = 'share-sheet';
// Handle
const handle = creerElement('div', 'sheet-handle');
// Titre + nom du fichier
const titleLabel = creerElement('div', 'sheet-label');
titleLabel.textContent = 'Partager';
const filename = creerElement('div', 'sheet-filename');
filename.textContent = name;
// Champ mot de passe
const pwdLabel = creerElement('div', 'sheet-field-label');
pwdLabel.textContent = 'Mot de passe (optionnel)';
const pwdWrap = creerElement('div', 'sheet-pwd-wrap');
const pwdInput = document.createElement('input');
pwdInput.type = 'password';
pwdInput.placeholder = 'Laisser vide si public';
pwdInput.className = 'sheet-pwd-input';
const pwdToggle = creerElement('button', 'sheet-pwd-toggle');
pwdToggle.type = 'button';
pwdToggle.innerHTML = sheetEyeIcon();
pwdToggle.addEventListener('click', () => {
const show = pwdInput.type === 'password';
pwdInput.type = show ? 'text' : 'password';
pwdToggle.innerHTML = show ? sheetEyeOffIcon() : sheetEyeIcon();
});
pwdWrap.append(pwdInput, pwdToggle);
// Pills expiration
const expLabel = creerElement('div', 'sheet-field-label');
expLabel.textContent = 'Expiration';
const pillsWrap = creerElement('div', 'sheet-pills');
// Champ custom (caché par défaut)
const customWrap = creerElement('div', 'sheet-custom-wrap');
customWrap.style.display = 'none';
const customInput = document.createElement('input');
customInput.type = 'number';
customInput.min = '1';
customInput.placeholder = '12';
customInput.className = 'sheet-pwd-input';
customInput.style.width = '50%';
const customUnit = document.createElement('select');
customUnit.className = 'sheet-custom-unit';
[['h', 'heures'], ['j', 'jours']].forEach(([v, l]) => {
const o = document.createElement('option');
o.value = v; o.textContent = l;
customUnit.appendChild(o);
});
customWrap.append(customInput, customUnit);
DUREES_EXPIRATION.forEach(opt => {
const pill = creerElement('button', 'sheet-pill' + (opt.value === '' ? ' is-active' : ''));
pill.textContent = opt.label;
pill.addEventListener('click', () => {
pillsWrap.querySelectorAll('.sheet-pill').forEach(p => p.classList.remove('is-active'));
pill.classList.add('is-active');
if (opt.value === 'custom') {
customWrap.style.display = 'flex';
selectedExpiry = 'custom';
customInput.focus();
} else {
customWrap.style.display = 'none';
selectedExpiry = opt.value;
}
});
pillsWrap.appendChild(pill);
});
// Champ max_downloads
const maxDlLabel = creerElement('div', 'sheet-field-label');
maxDlLabel.textContent = 'Max téléchargements (optionnel)';
const maxDlInput = document.createElement('input');
maxDlInput.type = 'number';
maxDlInput.min = '1';
maxDlInput.placeholder = 'Illimité';
maxDlInput.className = 'sheet-pwd-input sheet-num-input';
maxDlInput.style.width = '50%';
// Bouton créer
const createBtn = creerElement('button', 'sheet-create-btn');
createBtn.innerHTML = svgUpload() + ' Créer le lien';
createBtn.addEventListener('click', () => {
let expiry = selectedExpiry;
if (expiry === 'custom') {
const val = parseInt(customInput.value);
const unit = customUnit.value;
expiry = isNaN(val) || val < 1 ? '' : String(unit === 'j' ? val * 24 : val);
}
creerLienSheet(path, pwdInput.value, expiry, sheet, maxDlInput.value ? parseInt(maxDlInput.value) : null);
});
sheet.append(handle, titleLabel, filename, pwdLabel, pwdWrap, expLabel, pillsWrap, customWrap, maxDlLabel, maxDlInput, createBtn);
document.body.append(overlay, sheet);
setTimeout(() => pwdInput.focus(), 320);
}
function fermerShareSheet() {
document.getElementById('sheet-overlay')?.remove();
document.getElementById('share-sheet')?.remove();
}
async function creerLienSheet(path, password, expiresStr, sheet, maxDownloads = null) {
const expires = expiresStr ? parseInt(expiresStr) : null;
const createBtn = sheet.querySelector('.sheet-create-btn');
if (createBtn) {
createBtn.disabled = true;
createBtn.innerHTML = svgSpinner() + ' Création…';
}
try {
const resp = await fetch('/share/ctrl.php?cmd=create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path, password: password || '', expires, max_downloads: maxDownloads, csrf_token: CSRF_TOKEN }),
});
if (!resp.ok) { showToast('Erreur HTTP ' + resp.status, 'error'); if (createBtn) createBtn.disabled = false; return; }
const data = await resp.json();
if (data.error) { showToast(data.error, 'error'); if (createBtn) createBtn.disabled = false; return; }
const fullUrl = window.location.origin + data.url;
// État succès
sheet.innerHTML = '';
const handle = creerElement('div', 'sheet-handle');
const successIcon = creerElement('div', 'sheet-success-icon');
successIcon.innerHTML = '<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="20 6 9 17 4 12"/></svg>';
const successLabel = creerElement('div', 'sheet-label');
successLabel.textContent = 'Lien créé';
successLabel.style.textAlign = 'center';
successLabel.style.marginBottom = '.5rem';
const urlBox = creerElement('div', 'sheet-url-box');
urlBox.textContent = fullUrl;
const copyBtn = creerElement('button', 'sheet-copy-btn');
copyBtn.innerHTML = svgCopy() + ' Copier le lien';
copyBtn.addEventListener('click', async () => {
let text = fullUrl;
if (password) text += '\nMot de passe\u00a0: ' + password;
const ok = await copyText(text);
if (ok) {
copyBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="20 6 9 17 4 12"/></svg> Copié\u00a0!';
copyBtn.classList.add('is-copied');
showToast('Lien copié', 'success');
setTimeout(() => {
copyBtn.innerHTML = svgCopy() + ' Copier le lien';
copyBtn.classList.remove('is-copied');
}, 2500);
} else {
showToast('Copie impossible', 'error');
}
});
const closeBtn = creerElement('button', 'sheet-close-btn');
closeBtn.textContent = 'Fermer';
closeBtn.addEventListener('click', fermerShareSheet);
sheet.append(handle, successIcon, successLabel, urlBox, copyBtn, closeBtn);
rafraichirLiens();
} catch { showToast('Erreur de connexion', 'error'); }
}
function sheetEyeIcon() {
return '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>';
}
function sheetEyeOffIcon() {
return '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19m-6.72-1.07a3 3 0 11-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></svg>';
}
function svgUpload() {
return '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M4 12v8a2 2 0 002 2h12a2 2 0 002-2v-8"/><polyline points="16 6 12 2 8 6"/><line x1="12" y1="2" x2="12" y2="15"/></svg>';
}
function svgCopy() {
return '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1"/></svg>';
}
function svgSpinner() {
return '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" style="animation:sheet-spin .8s linear infinite"><path d="M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4M4.93 19.07l2.83-2.83M16.24 7.76l2.83-2.83"/></svg>';
}
/**
* Appelle l'API pour créer un nouveau lien de partage
*/
// eslint-disable-next-line no-unused-vars
async function creerLien(path, password, expiresStr, container) {
const expires = expiresStr ? parseInt(expiresStr) : null;
try {
const resp = await fetch('/share/ctrl.php?cmd=create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path: path, password: password || '', expires: expires, csrf_token: CSRF_TOKEN }),
});
if (!resp.ok) { showToast('Erreur HTTP ' + resp.status, 'error'); return; }
const data = await resp.json();
if (data.error) {
showToast(data.error, 'error');
return;
}
// Afficher le lien créé avec bouton copier
container.innerHTML = '';
const fullUrl = window.location.origin + data.url;
const msg = creerElement('span', 'copy-msg');
msg.innerHTML = '✓ Créé';
const copyBtn = creerElement('button', 'btn btn-primary btn-sm');
copyBtn.innerHTML = '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1"/></svg> Copier le lien';
copyBtn.addEventListener('click', () => copierLien(fullUrl, copyBtn));
container.append(msg, copyBtn);
// Rafraîchir le tableau des liens actifs
rafraichirLiens();
} catch (e) {
showToast('Erreur de connexion', 'error');
}
}
async function copyText(text) {
if (navigator.clipboard && window.isSecureContext) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch { /* fall through */ }
}
const ta = document.createElement('textarea');
ta.value = text;
ta.style.cssText = 'position:fixed;left:-9999px;top:-9999px;opacity:0';
document.body.appendChild(ta);
ta.focus();
ta.select();
let ok = false;
try { ok = document.execCommand('copy'); } catch { /* ignore */ }
ta.remove();
return ok;
}
function promptModal({ title, label, type = 'text', placeholder = '', confirmLabel = 'Confirmer' }) {
return new Promise(resolve => {
const overlay = creerElement('div', 'prompt-modal-overlay');
const card = creerElement('div', 'prompt-modal-card');
const titleEl = creerElement('div', 'prompt-modal-title');
titleEl.textContent = title;
const labelEl = creerElement('label', 'sheet-field-label');
labelEl.textContent = label;
const input = document.createElement('input');
input.type = type;
input.placeholder = placeholder;
input.className = 'sheet-pwd-input';
input.style.marginBottom = '1.2rem';
const actions = creerElement('div', 'prompt-modal-actions');
const cancelBtn = creerElement('button', 'btn btn-ghost');
cancelBtn.textContent = 'Annuler';
const confirmBtn = creerElement('button', 'btn btn-accent');
confirmBtn.textContent = confirmLabel;
actions.append(cancelBtn, confirmBtn);
card.append(titleEl, labelEl, input, actions);
overlay.appendChild(card);
document.body.appendChild(overlay);
input.focus();
function close(value) {
overlay.remove();
document.removeEventListener('keydown', onKey);
resolve(value);
}
function onKey(e) { if (e.key === 'Escape') close(null); }
document.addEventListener('keydown', onKey);
overlay.addEventListener('click', e => { if (e.target === overlay) close(null); });
cancelBtn.addEventListener('click', () => close(null));
confirmBtn.addEventListener('click', () => close(input.value));
input.addEventListener('keydown', e => { if (e.key === 'Enter') close(input.value); });
});
}
/**
* Copie un lien dans le presse-papiers
*/
async function copierLien(url, btn) {
const ok = await copyText(url);
if (ok) {
const orig = btn.innerHTML;
btn.innerHTML = '✓ Copié !';
btn.classList.add('btn-success');
btn.classList.remove('btn-primary');
showToast('Lien copié', 'success');
setTimeout(() => {
btn.innerHTML = orig;
btn.classList.remove('btn-success');
btn.classList.add('btn-primary');
}, 2000);
} else {
showToast('Copie impossible', 'error');
}
}
/**
* Rafraîchit le tableau des liens actifs sans recharger la page
*/
async function rafraichirLiens() {
try {
const resp = await fetch('/share/index.php?fragment=links');
if (resp.ok) {
const html = await resp.text();
const container = document.getElementById('links-container');
if (container && html.trim()) {
container.innerHTML = html;
}
}
} catch {
// Pas grave si ça échoue, la page se recharge au prochain accès
}
}
/**
* Supprime un lien de partage après confirmation
*/
// eslint-disable-next-line no-unused-vars
async function supprimerLien(id) {
if (!confirm('Supprimer ce lien de partage ?')) return;
try {
const resp = await fetch('/share/ctrl.php?cmd=delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: id, csrf_token: CSRF_TOKEN }),
});
if (!resp.ok) { showToast('Erreur HTTP ' + resp.status, 'error'); return; }
const data = await resp.json();
if (data.error) {
showToast(data.error, 'error');
return;
}
location.reload();
} catch {
showToast('Erreur de connexion', 'error');
}
}
/**
* Formate une taille en octets vers une forme lisible (Ko, Mo, Go)
*/
function formatTaille(bytes) {
if (bytes === null || bytes === undefined) return '';
if (bytes < 1024) return bytes + ' o';
if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' Ko';
if (bytes < 1073741824) return (bytes / 1048576).toFixed(1) + ' Mo';
return (bytes / 1073741824).toFixed(2) + ' Go';
}
/**
* Copie le lien (+ mot de passe si présent) dans le presse-papiers
* Appelé depuis les cartes de liens actifs
*/
// eslint-disable-next-line no-unused-vars
async function copierInfoLien(url, hasPwd, pwd, btn) {
const fullUrl = window.location.origin + url;
let text = fullUrl;
if (hasPwd && pwd) {
text += '\nMot de passe : ' + pwd;
}
const ok = await copyText(text);
if (ok) {
const orig = btn.innerHTML;
btn.innerHTML = '✓ Copié !';
showToast('Lien copié', 'success');
setTimeout(() => { btn.innerHTML = orig; }, 2000);
} else {
showToast('Copie impossible', 'error');
}
}
/**
* Envoie un lien de partage par email via l'API
*/
// eslint-disable-next-line no-unused-vars
async function envoyerEmail(linkId) {
const email = await promptModal({
title: 'Envoyer le lien par email',
label: 'Adresse email du destinataire',
type: 'email',
placeholder: 'destinataire@exemple.com',
confirmLabel: 'Envoyer',
});
if (!email || !email.trim()) return;
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim())) {
showToast('Adresse email invalide', 'error');
return;
}
try {
const resp = await fetch('/share/ctrl.php?cmd=send_email', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: linkId, email: email.trim(), csrf_token: CSRF_TOKEN }),
});
if (!resp.ok) { showToast('Erreur HTTP ' + resp.status, 'error'); return; }
const data = await resp.json();
if (data.error) {
showToast(data.error, 'error');
return;
}
showToast('Email envoyé à ' + email.trim() + ' !', 'success');
} catch {
showToast('Erreur de connexion', 'error');
}
}
/**
* Affiche un QR code en popup pour un lien de partage
*/
// eslint-disable-next-line no-unused-vars
function afficherQR(url, btn) {
// Fermer un popup existant
const old = document.getElementById('qr-popup');
if (old) old.remove();
const fullUrl = window.location.origin + url;
const popup = creerElement('div', 'qr-popup');
popup.id = 'qr-popup';
const card = creerElement('div', 'qr-card');
const title = creerElement('div', 'qr-title');
title.textContent = 'Scanner pour accéder';
const canvas = document.createElement('canvas');
canvas.id = 'qr-canvas';
genererQR(canvas, fullUrl);
const urlText = creerElement('div', 'qr-url');
urlText.textContent = fullUrl;
const closeBtn = creerElement('button', 'btn btn-ghost btn-sm');
closeBtn.textContent = 'Fermer';
closeBtn.addEventListener('click', () => popup.remove());
card.append(title, canvas, urlText, closeBtn);
popup.appendChild(card);
// Fermer au clic sur le fond
popup.addEventListener('click', (e) => {
if (e.target === popup) popup.remove();
});
document.body.appendChild(popup);
}
/**
* Génère un QR code sur un canvas (implémentation minimaliste)
* Utilise l'API Canvas pour dessiner un QR code via la lib inline
*/
function genererQR(canvas, text) {
// Encoder les données en QR code (version simplifiée mode byte, ECC L)
const modules = qrEncode(text);
const size = modules.length;
const scale = Math.max(4, Math.floor(240 / size));
const padding = 16;
canvas.width = size * scale + padding * 2;
canvas.height = size * scale + padding * 2;
const ctx = canvas.getContext('2d');
// Fond blanc avec coins arrondis
ctx.fillStyle = '#fff';
ctx.beginPath();
ctx.roundRect(0, 0, canvas.width, canvas.height, 12);
ctx.fill();
// Modules
ctx.fillStyle = '#1a1d28';
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if (modules[y][x]) {
ctx.fillRect(padding + x * scale, padding + y * scale, scale, scale);
}
}
}
}
/**
* QR Code encoder minimal — mode byte, ECC L, versions 1-10
* Suffisant pour des URLs jusqu'à ~270 caractères
*/
function qrEncode(text) {
const data = new TextEncoder().encode(text);
// Version capacities (mode byte, ECC L)
const caps = [0,17,32,53,78,106,134,154,192,230,271];
let ver = 1;
while (ver <= 10 && caps[ver] < data.length) ver++;
if (ver > 10) ver = 10; // clamp
const size = ver * 4 + 17;
const grid = Array.from({length: size}, () => new Uint8Array(size));
const mask = Array.from({length: size}, () => new Uint8Array(size));
// Finder patterns
function finderPattern(cx, cy) {
for (let dy = -3; dy <= 3; dy++) for (let dx = -3; dx <= 3; dx++) {
const x = cx + dx, y = cy + dy;
if (x < 0 || y < 0 || x >= size || y >= size) continue;
const d = Math.max(Math.abs(dx), Math.abs(dy));
grid[y][x] = (d !== 2) ? 1 : 0;
mask[y][x] = 1;
}
// Separators
for (let i = -4; i <= 4; i++) {
[[cx+i,cy-4],[cx+i,cy+4],[cx-4,cy+i],[cx+4,cy+i]].forEach(([x,y]) => {
if (x >= 0 && y >= 0 && x < size && y < size) { grid[y][x] = 0; mask[y][x] = 1; }
});
}
}
finderPattern(3, 3);
finderPattern(size - 4, 3);
finderPattern(3, size - 4);
// Alignment pattern (versions 2+)
if (ver >= 2) {
const pos = [6, size - 7];
for (const ay of pos) for (const ax of pos) {
if (mask[ay]?.[ax]) continue;
for (let dy = -2; dy <= 2; dy++) for (let dx = -2; dx <= 2; dx++) {
const x = ax + dx, y = ay + dy;
if (x >= 0 && y >= 0 && x < size && y < size) {
const d = Math.max(Math.abs(dx), Math.abs(dy));
grid[y][x] = (d === 1) ? 0 : 1;
mask[y][x] = 1;
}
}
}
}
// Timing patterns
for (let i = 8; i < size - 8; i++) {
if (!mask[6][i]) { grid[6][i] = (i % 2 === 0) ? 1 : 0; mask[6][i] = 1; }
if (!mask[i][6]) { grid[i][6] = (i % 2 === 0) ? 1 : 0; mask[i][6] = 1; }
}
// Dark module + reserved areas
grid[size - 8][8] = 1; mask[size - 8][8] = 1;
// Reserve format info areas
for (let i = 0; i < 9; i++) {
if (!mask[8][i]) mask[8][i] = 1;
if (!mask[i][8]) mask[i][8] = 1;
if (i < 8) {
if (!mask[8][size - 1 - i]) mask[8][size - 1 - i] = 1;
if (!mask[size - 1 - i][8]) mask[size - 1 - i][8] = 1;
}
}
// Encode data: mode byte (0100) + length + data + terminator + padding
const ecInfo = [[7,1,19],[10,1,34],[15,1,55],[20,1,80],[26,1,108],[36,2,68],[40,2,78],[48,2,97],[60,2,116],[72,2,68]];
const [ecPerBlock, numBlocks, dataPerBlock] = ecInfo[ver - 1];
const totalData = numBlocks * dataPerBlock;
const bits = [];
const push = (val, len) => { for (let i = len - 1; i >= 0; i--) bits.push((val >> i) & 1); };
push(0b0100, 4); // byte mode
push(data.length, ver <= 9 ? 8 : 16);
for (const b of data) push(b, 8);
push(0, Math.min(4, totalData * 8 - bits.length)); // terminator
while (bits.length % 8) bits.push(0);
while (bits.length < totalData * 8) {
push(0xEC, 8);
if (bits.length < totalData * 8) push(0x11, 8);
}
// Convert to bytes
const dataBytes = [];
for (let i = 0; i < bits.length; i += 8) {
dataBytes.push(bits.slice(i, i + 8).reduce((a, b) => (a << 1) | b, 0));
}
// RS error correction (simplified GF(256))
function gfMul(a, b) {
if (a === 0 || b === 0) return 0;
let r = 0;
for (let i = 0; i < 8; i++) {
if (b & 1) r ^= a;
b >>= 1;
const hi = a & 0x80;
a = (a << 1) & 0xFF;
if (hi) a ^= 0x11D;
}
return r;
}
function rsEncode(data, ecLen) {
// Generator polynomial
let gen = [1];
for (let i = 0; i < ecLen; i++) {
const ng = new Array(gen.length + 1).fill(0);
let a = 1;
for (let j = 0; j < i; j++) a = gfMul(a, 2);
for (let j = 0; j < gen.length; j++) {
ng[j] ^= gen[j];
ng[j + 1] ^= gfMul(gen[j], a);
}
gen = ng;
}
const msg = [...data, ...new Array(ecLen).fill(0)];
for (let i = 0; i < data.length; i++) {
const coef = msg[i];
if (coef === 0) continue;
for (let j = 0; j < gen.length; j++) {
msg[i + j] ^= gfMul(gen[j], coef);
}
}
return msg.slice(data.length);
}
// Split into blocks and compute EC
const blocks = [];
let offset = 0;
for (let b = 0; b < numBlocks; b++) {
const blockData = dataBytes.slice(offset, offset + dataPerBlock);
offset += dataPerBlock;
const ec = rsEncode(blockData, ecPerBlock);
blocks.push({ data: blockData, ec });
}
// Interleave
const final = [];
for (let i = 0; i < dataPerBlock; i++) for (const b of blocks) if (i < b.data.length) final.push(b.data[i]);
for (let i = 0; i < ecPerBlock; i++) for (const b of blocks) final.push(b.ec[i]);
// Place data on grid
const allBits = [];
for (const byte of final) for (let i = 7; i >= 0; i--) allBits.push((byte >> i) & 1);
let bitIdx = 0;
for (let right = size - 1; right >= 1; right -= 2) {
if (right === 6) right = 5;
for (let vert = 0; vert < size; vert++) {
for (const dx of [0, -1]) {
const x = right + dx;
const y = ((Math.floor((size - 1 - right) / 2)) % 2 === 0) ? vert : size - 1 - vert;
if (x < 0 || y < 0 || x >= size || y >= size) continue;
if (mask[y][x]) continue;
grid[y][x] = bitIdx < allBits.length ? allBits[bitIdx] : 0;
bitIdx++;
}
}
}
// Apply mask pattern 0 (checkerboard) and XOR
for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
if (!mask[y][x] && (y + x) % 2 === 0) grid[y][x] ^= 1;
}
// Write format info (mask 0, ECC L = 01, format bits = 0b111011111000100)
const fmtBits = [1,1,1,0,1,1,1,1,1,0,0,0,1,0,0];
const fmtPositions = [];
for (let i = 0; i < 6; i++) fmtPositions.push([8, i]);
fmtPositions.push([8, 7], [8, 8], [7, 8]);
for (let i = 9; i < 15; i++) fmtPositions.push([14 - i, 8]);
for (let i = 0; i < 8; i++) fmtPositions.push([8, size - 8 + i]);
for (let i = 0; i < 7; i++) fmtPositions.push([size - 1 - i, 8]);
// Write first 15 to first set, second 15 to second set
for (let i = 0; i < 15; i++) {
const [y, x] = fmtPositions[i];
grid[y][x] = fmtBits[i];
}
for (let i = 0; i < 15; i++) {
const [y, x] = fmtPositions[15 + i];
if (y >= 0 && y < size && x >= 0 && x < size) grid[y][x] = fmtBits[i];
}
return grid;
}
/**
* Crée un élément DOM avec une classe CSS
*/
function creerElement(tag, className) {
const el = document.createElement(tag);
if (className) el.className = className;
return el;
}
/**
* Affiche une notification toast en bas à droite
* @param {string} message
* @param {'info'|'success'|'error'} type
*/
function showToast(message, type = 'info') {
let container = document.getElementById('toast-container');
if (!container) {
container = document.createElement('div');
container.id = 'toast-container';
document.body.appendChild(container);
}
const toast = document.createElement('div');
toast.className = 'toast' + (type === 'success' ? ' is-success' : type === 'error' ? ' is-error' : '');
const text = document.createElement('span');
text.textContent = message;
const dismiss = document.createElement('button');
dismiss.className = 'toast-dismiss';
dismiss.setAttribute('aria-label', 'Fermer');
dismiss.textContent = '✕';
toast.append(text, dismiss);
container.appendChild(toast);
const remove = () => {
if (!toast.isConnected) return;
toast.classList.add('is-out');
toast.addEventListener('animationend', () => toast.remove(), { once: true });
};
dismiss.addEventListener('click', remove);
toast.addEventListener('click', remove);
const delay = type === 'error' ? 6000 : 4000;
setTimeout(remove, delay);
}
// ── Filtre local + recherche disque ─────────────────────────────────────────
let searchMode = false; // true = on affiche des résultats de recherche disque
function lancerRecherche() {
const q = document.getElementById('file-filter')?.value?.trim() ?? '';
if (!q) { quitterRecherche(); return; }
afficherResultatsRecherche(q);
}
function quitterRecherche() {
searchMode = false;
const label = document.getElementById('search-mode-label');
if (label) label.remove();
navigateTo(currentPath);
document.getElementById('file-filter').value = '';
}
async function afficherResultatsRecherche(q) {
const list = document.getElementById('file-list');
list.innerHTML = '<li class="file-item"><div class="empty-msg">Recherche en cours…</div></li>';
searchMode = true;
// Bandeau "mode recherche"
let label = document.getElementById('search-mode-label');
if (!label) {
label = creerElement('div', 'search-mode-label');