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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -133
app.py CHANGED
@@ -36,17 +36,10 @@ def load_data():
36
  data.setdefault('users', {})
37
  data.setdefault('files', {})
38
  for token, user_data in data['users'].items():
39
- if 'folders' not in user_data:
40
- files = user_data.get('files', [])
41
- user_data['folders'] = {
42
- 'root': {
43
- 'name': 'root',
44
- 'files': files,
45
- 'subfolders': {}
46
- }
47
- }
48
- if 'files' in user_data:
49
- del user_data['files']
50
  if 'storage_used' not in user_data:
51
  user_data['storage_used'] = 0
52
  logging.info("Данные успешно загружены")
@@ -207,22 +200,6 @@ input:focus, textarea:focus, select:focus {
207
  .download-btn:hover {
208
  background: #00b8c5;
209
  }
210
- .edit-btn {
211
- background: var(--accent);
212
- padding: 8px 16px;
213
- font-size: 0.9em;
214
- }
215
- .edit-btn:hover {
216
- background: #7a4de3;
217
- }
218
- .delete-btn {
219
- background: #ff3b30;
220
- padding: 8px 16px;
221
- font-size: 0.9em;
222
- }
223
- .delete-btn:hover {
224
- background: #e02a20;
225
- }
226
  .flash {
227
  color: var(--secondary);
228
  text-align: center;
@@ -234,7 +211,7 @@ input:focus, textarea:focus, select:focus {
234
  gap: 20px;
235
  margin-top: 20px;
236
  }
237
- .folder-item, .file-item {
238
  background: var(--card-bg);
239
  padding: 15px;
240
  border-radius: 16px;
@@ -242,18 +219,12 @@ input:focus, textarea:focus, select:focus {
242
  text-align: center;
243
  transition: var(--transition);
244
  }
245
- body.dark .folder-item, body.dark .file-item {
246
  background: var(--card-bg-dark);
247
  }
248
- .folder-item:hover, .file-item:hover {
249
  transform: translateY(-5px);
250
  }
251
- .folder-item {
252
- background: rgba(139, 92, 246, 0.1);
253
- }
254
- body.dark .folder-item {
255
- background: rgba(139, 92, 246, 0.2);
256
- }
257
  .file-preview {
258
  max-width: 100%;
259
  max-height: 200px;
@@ -262,15 +233,15 @@ body.dark .folder-item {
262
  margin-bottom: 10px;
263
  loading: lazy;
264
  }
265
- .folder-item p, .file-item p {
266
  font-size: 0.9em;
267
  margin: 5px 0;
268
  }
269
- .folder-item a, .file-item a {
270
  color: var(--primary);
271
  text-decoration: none;
272
  }
273
- .folder-item a:hover, .file-item a:hover {
274
  color: var(--accent);
275
  }
276
  .modal {
@@ -351,7 +322,7 @@ def register():
351
  data = load_data()
352
  data['users'][token] = {
353
  'created_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
354
- 'folders': {'root': {'name': 'root', 'files': [], 'subfolders': {}}},
355
  'storage_used': 0
356
  }
357
  save_data(data)
@@ -439,23 +410,9 @@ def login():
439
  '''
440
  return render_template_string(html)
441
 
442
- # Функция для получения папки по пути
443
- def get_folder(data, token, folder_path):
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
-
455
  # Личный dashboard
456
  @app.route('/dashboard', methods=['GET', 'POST'])
457
- @app.route('/dashboard/<path:folder_path>', methods=['GET', 'POST'])
458
- def dashboard(folder_path='root'):
459
  if 'token' not in session:
460
  flash('Пожалуйста, войдите!')
461
  return redirect(url_for('login'))
@@ -467,12 +424,7 @@ def dashboard(folder_path='root'):
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
@@ -480,36 +432,7 @@ def dashboard(folder_path='root'):
480
  storage_remaining_gb = MAX_STORAGE_GB - storage_used_gb
481
 
482
  if request.method == 'POST':
483
- if 'create_folder' in request.form:
484
- folder_name = request.form.get('folder_name')
485
- if folder_name and folder_name not in current_folder['subfolders']:
486
- current_folder['subfolders'][folder_name] = {'name': folder_name, 'files': [], 'subfolders': {}}
487
- save_data(data)
488
-
489
- elif 'edit_folder' in request.form:
490
- old_name = request.form.get('old_name')
491
- new_name = request.form.get('new_name')
492
- if old_name in current_folder['subfolders'] and new_name and new_name not in current_folder['subfolders']:
493
- current_folder['subfolders'][new_name] = current_folder['subfolders'].pop(old_name)
494
- current_folder['subfolders'][new_name]['name'] = new_name
495
- save_data(data)
496
-
497
- elif 'delete_folder' in request.form:
498
- folder_name = request.form.get('folder_name')
499
- if folder_name in current_folder['subfolders']:
500
- del current_folder['subfolders'][folder_name]
501
- save_data(data)
502
-
503
- elif 'move_file' in request.form:
504
- file_index = int(request.form.get('file_index'))
505
- target_folder = request.form.get('target_folder')
506
- file = current_folder['files'][file_index]
507
- current_folder['files'].pop(file_index)
508
- target = get_folder(data, token, target_folder)
509
- target['files'].append(file)
510
- save_data(data)
511
-
512
- elif 'upload_files' in request.files:
513
  files = request.files.getlist('files')
514
  total_size = sum(f.content_length for f in files if f)
515
  if storage_used + total_size > max_storage_bytes:
@@ -524,18 +447,22 @@ def dashboard(folder_path='root'):
524
  file_size = os.path.getsize(temp_path)
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
@@ -547,7 +474,7 @@ def dashboard(folder_path='root'):
547
  'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
548
  'size': file_size
549
  }
550
- current_folder['files'].append(file_info)
551
  data['users'][token]['storage_used'] += file_size
552
 
553
  if os.path.exists(temp_path):
@@ -555,7 +482,7 @@ def dashboard(folder_path='root'):
555
  save_data(data)
556
  flash('Файлы успешно загружены!')
557
 
558
- return redirect(url_for('dashboard', folder_path=folder_path))
559
 
560
  html = '''
561
  <!DOCTYPE html>
@@ -571,7 +498,6 @@ def dashboard(folder_path='root'):
571
  <div class="container">
572
  <h1>Zues Cloud Dashboard</h1>
573
  <p>Токен: {{ token }}</p>
574
- <p>Текущая папка: {{ folder_path }}</p>
575
  <div class="storage-indicator">
576
  <p>Использовано места: {{ "%.2f" % storage_used_gb }} ГБ из {{ MAX_STORAGE_GB }} ГБ (Осталось: {{ "%.2f" % storage_remaining_gb }} ГБ)</p>
577
  <div class="storage-bar-container">
@@ -579,16 +505,7 @@ def dashboard(folder_path='root'):
579
  <span class="storage-text">{{ "%.1f" % storage_percent }}%</span>
580
  </div>
581
  </div>
582
- {% if folder_path != 'root' %}
583
- <a href="{{ url_for('dashboard', folder_path='/'.join(folder_path.split('/')[:-1]) or 'root') }}" class="btn">Назад</a>
584
- {% endif %}
585
 
586
- <h2 style="margin-top: 20px;">Создать папку</h2>
587
- <form method="POST">
588
- <input type="text" name="folder_name" placeholder="Название папки" required>
589
- <button type="submit" name="create_folder" class="btn">Создать</button>
590
- </form>
591
-
592
  <h2 style="margin-top: 20px;">Загрузить файлы</h2>
593
  {% with messages = get_flashed_messages() %}
594
  {% if messages %}
@@ -608,20 +525,9 @@ def dashboard(folder_path='root'):
608
  </div>
609
  </div>
610
 
611
- <h2 style="margin-top: 30px;">Папки и файлы</h2>
612
  <div class="file-grid">
613
- {% for folder_name, folder in current_folder['subfolders'].items() %}
614
- <div class="folder-item">
615
- <p><a href="{{ url_for('dashboard', folder_path=folder_path + '/' + folder_name) }}">{{ folder_name }}</a></p>
616
- <form method="POST" style="margin-top: 10px;">
617
- <input type="text" name="new_name" placeholder="Новое имя" value="{{ folder_name }}">
618
- <input type="hidden" name="old_name" value="{{ folder_name }}">
619
- <button type="submit" name="edit_folder" class="btn edit-btn">Переименовать</button>
620
- <button type="submit" name="delete_folder" class="btn delete-btn" onclick="return confirm('Удалить папку?');">Удалить</button>
621
- </form>
622
- </div>
623
- {% endfor %}
624
- {% for file in current_folder['files'] %}
625
  <div class="file-item">
626
  {% if file['type'] == 'video' %}
627
  <video class="file-preview" preload="metadata" muted loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}', true)">
@@ -634,20 +540,10 @@ def dashboard(folder_path='root'):
634
  {% endif %}
635
  <p>{{ file['upload_date'] }} ({{ "%.2f" % (file['size'] / (1024 * 1024)) }} МБ)</p>
636
  <a href="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" class="btn download-btn" download="{{ file['filename'] }}">Скачать</a>
637
- <form method="POST" style="margin-top: 10px;">
638
- <input type="hidden" name="file_index" value="{{ loop.index0 }}">
639
- <select name="target_folder">
640
- <option value="">Переместить в...</option>
641
- {% for f_name in current_folder['subfolders'] %}
642
- <option value="{{ folder_path + '/' + f_name }}">{{ f_name }}</option>
643
- {% endfor %}
644
- </select>
645
- <button type="submit" name="move_file" class="btn edit-btn">Переместить</button>
646
- </form>
647
  </div>
648
  {% endfor %}
649
- {% if not current_folder['files'] and not current_folder['subfolders'] %}
650
- <p>Папка пуста.</p>
651
  {% endif %}
652
  </div>
653
  <a href="{{ url_for('logout') }}" class="btn" style="margin-top: 20px;">Выйти</a>
@@ -694,8 +590,7 @@ def dashboard(folder_path='root'):
694
  return render_template_string(
695
  html,
696
  token=token,
697
- current_folder=current_folder,
698
- folder_path=folder_path,
699
  repo_id=REPO_ID,
700
  storage_used_gb=storage_used_gb,
701
  storage_remaining_gb=storage_remaining_gb,
 
36
  data.setdefault('users', {})
37
  data.setdefault('files', {})
38
  for token, user_data in data['users'].items():
39
+ # Удаляем структуру папок, храним файлы напрямую
40
+ if 'folders' in user_data:
41
+ user_data['files'] = user_data['folders'].get('root', {}).get('files', [])
42
+ del user_data['folders']
 
 
 
 
 
 
 
43
  if 'storage_used' not in user_data:
44
  user_data['storage_used'] = 0
45
  logging.info("Данные успешно загружены")
 
200
  .download-btn:hover {
201
  background: #00b8c5;
202
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  .flash {
204
  color: var(--secondary);
205
  text-align: center;
 
211
  gap: 20px;
212
  margin-top: 20px;
213
  }
214
+ .file-item {
215
  background: var(--card-bg);
216
  padding: 15px;
217
  border-radius: 16px;
 
219
  text-align: center;
220
  transition: var(--transition);
221
  }
222
+ body.dark .file-item {
223
  background: var(--card-bg-dark);
224
  }
225
+ .file-item:hover {
226
  transform: translateY(-5px);
227
  }
 
 
 
 
 
 
228
  .file-preview {
229
  max-width: 100%;
230
  max-height: 200px;
 
233
  margin-bottom: 10px;
234
  loading: lazy;
235
  }
236
+ .file-item p {
237
  font-size: 0.9em;
238
  margin: 5px 0;
239
  }
240
+ .file-item a {
241
  color: var(--primary);
242
  text-decoration: none;
243
  }
244
+ .file-item a:hover {
245
  color: var(--accent);
246
  }
247
  .modal {
 
322
  data = load_data()
323
  data['users'][token] = {
324
  'created_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
325
+ 'files': [],
326
  'storage_used': 0
327
  }
328
  save_data(data)
 
410
  '''
411
  return render_template_string(html)
412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  # Личный dashboard
414
  @app.route('/dashboard', methods=['GET', 'POST'])
415
+ def dashboard():
 
416
  if 'token' not in session:
417
  flash('Пожалуйста, войдите!')
418
  return redirect(url_for('login'))
 
424
  flash('Токен недействителен!')
425
  return redirect(url_for('login'))
426
 
427
+ user_files = data['users'][token]['files']
 
 
 
 
 
428
  storage_used = data['users'][token]['storage_used']
429
  max_storage_bytes = MAX_STORAGE_GB * 1024 * 1024 * 1024
430
  storage_percent = (storage_used / max_storage_bytes) * 100 if max_storage_bytes > 0 else 0
 
432
  storage_remaining_gb = MAX_STORAGE_GB - storage_used_gb
433
 
434
  if request.method == 'POST':
435
+ if 'upload_files' in request.files:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
  files = request.files.getlist('files')
437
  total_size = sum(f.content_length for f in files if f)
438
  if storage_used + total_size > max_storage_bytes:
 
447
  file_size = os.path.getsize(temp_path)
448
 
449
  api = HfApi()
450
+ file_path = f"cloud_files/{token}/{filename}"
451
  try:
452
+ logging.info(f"Попытка загрузки файла: {file_path} в репозиторий {REPO_ID}")
453
+ logging.info(f"Используемый токен: {HF_TOKEN_WRITE[:5]}... (скрыт для безопасности)")
454
  api.upload_file(
455
  path_or_fileobj=temp_path,
456
  path_in_repo=file_path,
457
  repo_id=REPO_ID,
458
  repo_type="dataset",
459
  token=HF_TOKEN_WRITE,
460
+ commit_message=f"Загружен файл для {token}"
461
  )
462
+ logging.info(f"Файл {filename} успешно загружен в Hugging Face")
463
  except Exception as e:
464
+ logging.error(f"Ошибка при загрузке файла на Hugging Face: {str(e)}")
465
+ flash(f"Ошибка при загрузке файла {filename}: {str(e)}")
466
  if os.path.exists(temp_path):
467
  os.remove(temp_path)
468
  continue
 
474
  'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
475
  'size': file_size
476
  }
477
+ user_files.append(file_info)
478
  data['users'][token]['storage_used'] += file_size
479
 
480
  if os.path.exists(temp_path):
 
482
  save_data(data)
483
  flash('Файлы успешно загружены!')
484
 
485
+ return redirect(url_for('dashboard'))
486
 
487
  html = '''
488
  <!DOCTYPE html>
 
498
  <div class="container">
499
  <h1>Zues Cloud Dashboard</h1>
500
  <p>Токен: {{ token }}</p>
 
501
  <div class="storage-indicator">
502
  <p>Использовано места: {{ "%.2f" % storage_used_gb }} ГБ из {{ MAX_STORAGE_GB }} ГБ (Осталось: {{ "%.2f" % storage_remaining_gb }} ГБ)</p>
503
  <div class="storage-bar-container">
 
505
  <span class="storage-text">{{ "%.1f" % storage_percent }}%</span>
506
  </div>
507
  </div>
 
 
 
508
 
 
 
 
 
 
 
509
  <h2 style="margin-top: 20px;">Загрузить файлы</h2>
510
  {% with messages = get_flashed_messages() %}
511
  {% if messages %}
 
525
  </div>
526
  </div>
527
 
528
+ <h2 style="margin-top: 30px;">Файлы</h2>
529
  <div class="file-grid">
530
+ {% for file in user_files %}
 
 
 
 
 
 
 
 
 
 
 
531
  <div class="file-item">
532
  {% if file['type'] == 'video' %}
533
  <video class="file-preview" preload="metadata" muted loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}', true)">
 
540
  {% endif %}
541
  <p>{{ file['upload_date'] }} ({{ "%.2f" % (file['size'] / (1024 * 1024)) }} МБ)</p>
542
  <a href="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" class="btn download-btn" download="{{ file['filename'] }}">Скачать</a>
 
 
 
 
 
 
 
 
 
 
543
  </div>
544
  {% endfor %}
545
+ {% if not user_files %}
546
+ <p>Файлов нет.</p>
547
  {% endif %}
548
  </div>
549
  <a href="{{ url_for('logout') }}" class="btn" style="margin-top: 20px;">Выйти</a>
 
590
  return render_template_string(
591
  html,
592
  token=token,
593
+ user_files=user_files,
 
594
  repo_id=REPO_ID,
595
  storage_used_gb=storage_used_gb,
596
  storage_remaining_gb=storage_remaining_gb,