Eluza133 commited on
Commit
85435ed
·
verified ·
1 Parent(s): f60c719

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -37
app.py CHANGED
@@ -444,7 +444,11 @@ def get_folder(data, token, folder_path):
444
  current = data['users'][token]['folders']
445
  if folder_path == 'root':
446
  return current['root']
447
- for folder_name in folder_path.split('/')[1:]:
 
 
 
 
448
  current = current['subfolders'][folder_name]
449
  return current
450
 
@@ -463,7 +467,12 @@ def dashboard(folder_path='root'):
463
  flash('Токен недействителен!')
464
  return redirect(url_for('login'))
465
 
466
- current_folder = get_folder(data, token, folder_path)
 
 
 
 
 
467
  storage_used = data['users'][token]['storage_used']
468
  max_storage_bytes = MAX_STORAGE_GB * 1024 * 1024 * 1024
469
  storage_percent = (storage_used / max_storage_bytes) * 100 if max_storage_bytes > 0 else 0
@@ -516,14 +525,20 @@ def dashboard(folder_path='root'):
516
 
517
  api = HfApi()
518
  file_path = f"cloud_files/{token}/{folder_path}/{filename}"
519
- api.upload_file(
520
- path_or_fileobj=temp_path,
521
- path_in_repo=file_path,
522
- repo_id=REPO_ID,
523
- repo_type="dataset",
524
- token=HF_TOKEN_WRITE,
525
- commit_message=f"Загружен файл для {token} в {folder_path}"
526
- )
 
 
 
 
 
 
527
 
528
  file_info = {
529
  'filename': filename,
@@ -538,6 +553,7 @@ def dashboard(folder_path='root'):
538
  if os.path.exists(temp_path):
539
  os.remove(temp_path)
540
  save_data(data)
 
541
 
542
  return redirect(url_for('dashboard', folder_path=folder_path))
543
 
@@ -657,36 +673,19 @@ def dashboard(folder_path='root'):
657
  modal.innerHTML = '<div id="modalContent"></div>';
658
  }
659
  }
660
- document.getElementById('upload-form').onsubmit = async function(e) {
661
- e.preventDefault();
662
- const formData = new FormData(this);
663
- const files = formData.getAll('files');
664
- const totalFiles = files.length;
665
  const progressContainer = document.getElementById('upload-progress');
 
666
  const progressBar = document.getElementById('progress-bar');
667
  const progressText = document.getElementById('progress-text');
668
-
669
- progressContainer.style.display = 'block';
670
- let uploadedFiles = 0;
671
-
672
- const xhr = new XMLHttpRequest();
673
- xhr.open('POST', window.location.pathname, true);
674
- xhr.upload.onprogress = function(event) {
675
- if (event.lengthComputable) {
676
- const percent = Math.round((event.loaded / event.total) * 100);
677
- progressBar.style.width = percent + '%';
678
- progressText.textContent = `Загрузка ${uploadedFiles + 1}/${totalFiles} (${percent}%)`;
679
- }
680
- };
681
- xhr.onload = function() {
682
- uploadedFiles++;
683
- if (uploadedFiles === totalFiles) {
684
- window.location.reload();
685
- } else {
686
- progressText.textContent = `Загружено ${uploadedFiles}/${totalFiles}`;
687
- }
688
- };
689
- xhr.send(formData);
690
  };
691
  </script>
692
  </body>
 
444
  current = data['users'][token]['folders']
445
  if folder_path == 'root':
446
  return current['root']
447
+ path_parts = folder_path.split('/')
448
+ for folder_name in path_parts[1:]:
449
+ if 'subfolders' not in current or folder_name not in current['subfolders']:
450
+ logging.error(f"Папка {folder_name} не найдена в пути {folder_path} для токена {token}")
451
+ raise KeyError(f"Папка {folder_name} не найдена")
452
  current = current['subfolders'][folder_name]
453
  return current
454
 
 
467
  flash('Токен недействителен!')
468
  return redirect(url_for('login'))
469
 
470
+ try:
471
+ current_folder = get_folder(data, token, folder_path)
472
+ except KeyError as e:
473
+ flash(str(e))
474
+ return redirect(url_for('dashboard', folder_path='root'))
475
+
476
  storage_used = data['users'][token]['storage_used']
477
  max_storage_bytes = MAX_STORAGE_GB * 1024 * 1024 * 1024
478
  storage_percent = (storage_used / max_storage_bytes) * 100 if max_storage_bytes > 0 else 0
 
525
 
526
  api = HfApi()
527
  file_path = f"cloud_files/{token}/{folder_path}/{filename}"
528
+ try:
529
+ api.upload_file(
530
+ path_or_fileobj=temp_path,
531
+ path_in_repo=file_path,
532
+ repo_id=REPO_ID,
533
+ repo_type="dataset",
534
+ token=HF_TOKEN_WRITE,
535
+ commit_message=f"Загружен файл для {token} в {folder_path}"
536
+ )
537
+ except Exception as e:
538
+ flash(f"Ошибка при загрузке файла на Hugging Face: {str(e)}")
539
+ if os.path.exists(temp_path):
540
+ os.remove(temp_path)
541
+ continue
542
 
543
  file_info = {
544
  'filename': filename,
 
553
  if os.path.exists(temp_path):
554
  os.remove(temp_path)
555
  save_data(data)
556
+ flash('Файлы успешно загружены!')
557
 
558
  return redirect(url_for('dashboard', folder_path=folder_path))
559
 
 
673
  modal.innerHTML = '<div id="modalContent"></div>';
674
  }
675
  }
676
+ document.getElementById('upload-form').onsubmit = function(e) {
 
 
 
 
677
  const progressContainer = document.getElementById('upload-progress');
678
+ progressContainer.style.display = 'block';
679
  const progressBar = document.getElementById('progress-bar');
680
  const progressText = document.getElementById('progress-text');
681
+ let percent = 0;
682
+ const interval = setInterval(() => {
683
+ percent += 10;
684
+ if (percent > 100) percent = 100;
685
+ progressBar.style.width = percent + '%';
686
+ progressText.textContent = percent + '%';
687
+ if (percent === 100) clearInterval(interval);
688
+ }, 200);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
689
  };
690
  </script>
691
  </body>