Eluza133 commited on
Commit
6bf2429
·
verified ·
1 Parent(s): d16c245

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -48
app.py CHANGED
@@ -297,13 +297,19 @@ body.dark .file-item {
297
  justify-content: center;
298
  align-items: center;
299
  }
300
- .modal img, .modal video, .modal iframe, .modal pre {
301
  max-width: 95%;
302
  max-height: 95%;
303
  object-fit: contain;
304
  border-radius: 20px;
305
  box-shadow: var(--shadow);
306
  }
 
 
 
 
 
 
307
  #progress-container {
308
  width: 100%;
309
  background: var(--glass-bg);
@@ -496,7 +502,7 @@ def dashboard():
496
  for file in files:
497
  if file and file.filename:
498
  original_filename = secure_filename(file.filename)
499
- unique_filename = f"{uuid.uuid4()}{os.path.splitext(original_filename)[1]}"
500
  temp_path = os.path.join('uploads', unique_filename)
501
  file.save(temp_path)
502
  temp_files.append((temp_path, unique_filename, original_filename))
@@ -516,7 +522,7 @@ def dashboard():
516
  'filename': original_filename,
517
  'unique_filename': unique_filename,
518
  'path': file_path,
519
- 'type': get_file_type(unique_filename),
520
  'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
521
  }
522
  data['users'][username]['files'].append(file_info)
@@ -562,6 +568,7 @@ def dashboard():
562
  <div class="file-grid">
563
  {% for file in user_files %}
564
  <div class="file-item">
 
565
  {% if file['type'] == 'video' %}
566
  <video class="file-preview" preload="metadata" muted loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}', true)">
567
  <source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" type="video/mp4">
@@ -569,15 +576,14 @@ def dashboard():
569
  {% elif file['type'] == 'image' %}
570
  <img class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" alt="{{ file['filename'] }}" loading="lazy" onclick="openModal(this.src, false)">
571
  {% elif file['type'] == 'pdf' %}
572
- <iframe class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}#toolbar=0" loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}', 'pdf')"></iframe>
573
  {% elif file['type'] == 'text' %}
574
- <p class="file-preview" style="overflow: auto; white-space: pre-wrap;" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}', 'text')">Text file</p>
575
  {% else %}
576
- <p>{{ file['filename'] }}</p>
577
  {% endif %}
578
- <p>{{ file['filename'] }}</p>
579
  <p>{{ file['upload_date'] }}</p>
580
- <a href="{{ url_for('download_file', file_path=file['path'], filename=file['filename']) }}" class="btn download-btn">Скачать</a>
581
  <a href="{{ url_for('delete_file', file_path=file['path']) }}" class="btn delete-btn" onclick="return confirm('Вы уверены, что хотите удалить этот файл?');">Удалить</a>
582
  </div>
583
  {% endfor %}
@@ -611,21 +617,11 @@ def dashboard():
611
  <div id="modalContent"></div>
612
  </div>
613
  <script>
614
- async function openModal(src, type) {
615
  const modal = document.getElementById('mediaModal');
616
  const modalContent = document.getElementById('modalContent');
617
- if (type === true) {
618
  modalContent.innerHTML = `<video controls autoplay><source src="${src}" type="video/mp4"></video>`;
619
- } else if (type === 'pdf') {
620
- modalContent.innerHTML = `<iframe src="${src}#toolbar=0" style="width: 95%; height: 95vh;"></iframe>`;
621
- } else if (type === 'text') {
622
- try {
623
- const response = await fetch(src);
624
- const text = await response.text();
625
- modalContent.innerHTML = `<pre style="white-space: pre-wrap; overflow: auto;">${text}</pre>`;
626
- } catch (e) {
627
- modalContent.innerHTML = `<p>Ошибка загрузки текстового файла</p>`;
628
- }
629
  } else {
630
  modalContent.innerHTML = `<img src="${src}">`;
631
  }
@@ -707,8 +703,8 @@ def dashboard():
707
  '''
708
  return render_template_string(html, username=username, user_files=user_files, repo_id=REPO_ID)
709
 
710
- @app.route('/download/<path:file_path>/<filename>')
711
- def download_file(file_path, filename):
712
  if 'username' not in session:
713
  flash('Пожалуйста, войдите в систему!')
714
  return redirect(url_for('login'))
@@ -741,7 +737,7 @@ def download_file(file_path, filename):
741
  return send_file(
742
  file_content,
743
  as_attachment=True,
744
- download_name=filename,
745
  mimetype='application/octet-stream'
746
  )
747
  except requests.exceptions.RequestException as e:
@@ -883,6 +879,7 @@ def admin_user_files(username):
883
  <div class="file-grid">
884
  {% for file in user_files %}
885
  <div class="file-item">
 
886
  {% if file.get('type') == 'video' %}
887
  <video class="file-preview" preload="metadata" muted loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true', true)">
888
  <source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true" type="video/mp4">
@@ -890,15 +887,14 @@ def admin_user_files(username):
890
  {% elif file.get('type') == 'image' %}
891
  <img class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true" alt="{{ file['filename'] }}" loading="lazy" onclick="openModal(this.src, false)">
892
  {% elif file.get('type') == 'pdf' %}
893
- <iframe class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true#toolbar=0" loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true', 'pdf')"></iframe>
894
  {% elif file.get('type') == 'text' %}
895
- <p class="file-preview" style="overflow: auto; white-space: pre-wrap;" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true', 'text')">Text file</p>
896
  {% else %}
897
- <p>{{ file.get('filename', 'N/A') }}</p>
898
  {% endif %}
899
- <p>{{ file.get('filename', 'N/A') }}</p>
900
  <p>{{ file.get('upload_date', 'N/A') }}</p>
901
- <a href="{{ url_for('download_file', file_path=file['path'], filename=file['filename']) }}" class="btn download-btn">Скачать</a>
902
  <form method="POST" action="{{ url_for('admin_delete_file', username=username, file_path=file['path']) }}" style="display: inline; margin-left: 5px;" onsubmit="return confirm('Вы уверены, что хотите удалить этот файл?');">
903
  <button type="submit" class="btn delete-btn" style="padding: 5px 10px; font-size: 0.9em;">Удалить</button>
904
  </form>
@@ -914,29 +910,18 @@ def admin_user_files(username):
914
  <div id="modalContent"></div>
915
  </div>
916
  <script>
917
- async function openModal(src, type) {
918
  const modal = document.getElementById('mediaModal');
919
  const modalContent = document.getElementById('modalContent');
920
  const tokenParam = HF_TOKEN_READ ? `?token=${HF_TOKEN_READ}` : "";
921
- let finalSrc = src;
922
- if (!finalSrc.includes('?download=true')) {
923
- finalSrc = finalSrc.includes('?') ? finalSrc.replace('?', '?download=true&') : finalSrc + '?download=true';
924
- }
925
- finalSrc += tokenParam;
926
- if (type === true) {
927
- modalContent.innerHTML = `<video controls autoplay style='max-width: 95%; max-height: 95vh;'><source src="${finalSrc}" type="video/mp4"></video>`;
928
- } else if (type === 'pdf') {
929
- modalContent.innerHTML = `<iframe src="${finalSrc}#toolbar=0" style="width: 95%; height: 95vh;"></iframe>`;
930
- } else if (type === 'text') {
931
- try {
932
- const response = await fetch(finalSrc);
933
- const text = await response.text();
934
- modalContent.innerHTML = `<pre style="white-space: pre-wrap; overflow: auto;">${text}</pre>`;
935
- } catch (e) {
936
- modalContent.innerHTML = `<p>Ошибка загрузки текстового файла</p>`;
937
- }
938
- } else {
939
- modalContent.innerHTML = `<img src="${finalSrc}" style='max-width: 95%; max-height: 95vh;'>`;
940
  }
941
  modal.style.display = 'flex';
942
  }
 
297
  justify-content: center;
298
  align-items: center;
299
  }
300
+ .modal img, .modal video {
301
  max-width: 95%;
302
  max-height: 95%;
303
  object-fit: contain;
304
  border-radius: 20px;
305
  box-shadow: var(--shadow);
306
  }
307
+ .modal iframe {
308
+ border: none;
309
+ width: 95%;
310
+ max-width: 100%;
311
+ height: 95%;
312
+ }
313
  #progress-container {
314
  width: 100%;
315
  background: var(--glass-bg);
 
502
  for file in files:
503
  if file and file.filename:
504
  original_filename = secure_filename(file.filename)
505
+ unique_filename = f"{uuid.uuid4().hex}_{original_filename}"
506
  temp_path = os.path.join('uploads', unique_filename)
507
  file.save(temp_path)
508
  temp_files.append((temp_path, unique_filename, original_filename))
 
522
  'filename': original_filename,
523
  'unique_filename': unique_filename,
524
  'path': file_path,
525
+ 'type': get_file_type(original_filename),
526
  'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
527
  }
528
  data['users'][username]['files'].append(file_info)
 
568
  <div class="file-grid">
569
  {% for file in user_files %}
570
  <div class="file-item">
571
+ <p style="font-size: 1.1em; font-weight: bold;">{{ file['filename'] }}</p>
572
  {% if file['type'] == 'video' %}
573
  <video class="file-preview" preload="metadata" muted loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}', true)">
574
  <source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" type="video/mp4">
 
576
  {% elif file['type'] == 'image' %}
577
  <img class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" alt="{{ file['filename'] }}" loading="lazy" onclick="openModal(this.src, false)">
578
  {% elif file['type'] == 'pdf' %}
579
+ <iframe class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" loading="lazy"></iframe>
580
  {% elif file['type'] == 'text' %}
581
+ <textarea class="file-preview" readonly style="font-family: monospace; padding: 10px; border: 1px solid #ddd; border-radius: 5px; overflow: auto; width: 100%; height: 200px;">Предпросмотр текстовых файлов пока не поддерживается. Скачайте файл для просмотра.</textarea>
582
  {% else %}
583
+ <p>Тип файла не поддерживается для предпросмотра</p>
584
  {% endif %}
 
585
  <p>{{ file['upload_date'] }}</p>
586
+ <a href="{{ url_for('download_file', file_path=file['path'], filename=file['unique_filename'], original_filename=file['filename']) }}" class="btn download-btn">Скачать</a>
587
  <a href="{{ url_for('delete_file', file_path=file['path']) }}" class="btn delete-btn" onclick="return confirm('Вы уверены, что хотите удалить этот файл?');">Удалить</a>
588
  </div>
589
  {% endfor %}
 
617
  <div id="modalContent"></div>
618
  </div>
619
  <script>
620
+ function openModal(src, isVideo) {
621
  const modal = document.getElementById('mediaModal');
622
  const modalContent = document.getElementById('modalContent');
623
+ if (isVideo) {
624
  modalContent.innerHTML = `<video controls autoplay><source src="${src}" type="video/mp4"></video>`;
 
 
 
 
 
 
 
 
 
 
625
  } else {
626
  modalContent.innerHTML = `<img src="${src}">`;
627
  }
 
703
  '''
704
  return render_template_string(html, username=username, user_files=user_files, repo_id=REPO_ID)
705
 
706
+ @app.route('/download/<path:file_path>/<filename>/<original_filename>')
707
+ def download_file(file_path, filename, original_filename):
708
  if 'username' not in session:
709
  flash('Пожалуйста, войдите в систему!')
710
  return redirect(url_for('login'))
 
737
  return send_file(
738
  file_content,
739
  as_attachment=True,
740
+ download_name=original_filename,
741
  mimetype='application/octet-stream'
742
  )
743
  except requests.exceptions.RequestException as e:
 
879
  <div class="file-grid">
880
  {% for file in user_files %}
881
  <div class="file-item">
882
+ <p style="font-size: 1.1em; font-weight: bold;">{{ file.get('filename', 'N/A') }}</p>
883
  {% if file.get('type') == 'video' %}
884
  <video class="file-preview" preload="metadata" muted loading="lazy" onclick="openModal('https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true', true)">
885
  <source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true" type="video/mp4">
 
887
  {% elif file.get('type') == 'image' %}
888
  <img class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}?download=true" alt="{{ file['filename'] }}" loading="lazy" onclick="openModal(this.src, false)">
889
  {% elif file.get('type') == 'pdf' %}
890
+ <iframe class="file-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ file['path'] }}" loading="lazy"></iframe>
891
  {% elif file.get('type') == 'text' %}
892
+ <textarea class="file-preview" readonly style="font-family: monospace; padding: 10px; border: 1px solid #ddd; border-radius: 5px; overflow: auto; width: 100%; height: 200px;">Предпросмотр текстовых файлов пока не поддерживается. Скачайте файл для просмотра.</textarea>
893
  {% else %}
894
+ <p>Тип файла не поддерживается для предпросмотра</p>
895
  {% endif %}
 
896
  <p>{{ file.get('upload_date', 'N/A') }}</p>
897
+ <a href="{{ url_for('download_file', file_path=file['path'], filename=file['unique_filename'], original_filename=file['filename']) }}" class="btn download-btn">Скачать</a>
898
  <form method="POST" action="{{ url_for('admin_delete_file', username=username, file_path=file['path']) }}" style="display: inline; margin-left: 5px;" onsubmit="return confirm('Вы уверены, что хотите удалить этот файл?');">
899
  <button type="submit" class="btn delete-btn" style="padding: 5px 10px; font-size: 0.9em;">Удалить</button>
900
  </form>
 
910
  <div id="modalContent"></div>
911
  </div>
912
  <script>
913
+ function openModal(src, isVideo) {
914
  const modal = document.getElementById('mediaModal');
915
  const modalContent = document.getElementById('modalContent');
916
  const tokenParam = HF_TOKEN_READ ? `?token=${HF_TOKEN_READ}` : "";
917
+ const finalSrc = src + tokenParam;
918
+
919
+ if (isVideo) {
920
+ const videoSrc = finalSrc.includes('?download=true') ? finalSrc : finalSrc.replace('?', '?download=true&');
921
+ modalContent.innerHTML = `<video controls autoplay style='max-width: 95%; max-height: 95vh;'><source src="${videoSrc}" type="video/mp4"></video>`;
922
+ } else {
923
+ const img_src = finalSrc.includes('?download=true') ? finalSrc : finalSrc.replace('?', '?download=true&');
924
+ modalContent.innerHTML = `<img src="${img_src}" style='max-width: 95%; max-height: 95vh;'>`;
 
 
 
 
 
 
 
 
 
 
 
925
  }
926
  modal.style.display = 'flex';
927
  }