Skip to content

Commit

Permalink
Add files dropbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Sep 18, 2023
1 parent 0dac170 commit fdbf276
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
66 changes: 66 additions & 0 deletions dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import json
import os.path
import stat

from astropy.time import Time
import astropy.units as u
Expand All @@ -26,12 +27,17 @@
from flask import abort
from flask import Flask
from flask import jsonify
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import url_for

from werkzeug.security import safe_join
from werkzeug.utils import secure_filename
# pylint: disable=missing-docstring

FILES_ROOT = '/home/observer/Public'

GENERATED_DATA_DIR = '/var/www/dashboard/generated'
GENERATED_DATA = {
Expand Down Expand Up @@ -70,6 +76,66 @@ def vnc():
return render_template('vnc.html')


@app.route('/files/', methods=['GET', 'POST'])
@app.route('/files/<path:directory>', methods=['GET', 'POST'])
def files(directory=''):
path = safe_join(FILES_ROOT, directory)
if path is None:
return abort(403)

if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return redirect(request.url)

f = request.files['file']
if f and f.filename:
filename = secure_filename(f.filename)
f.save(os.path.join(path, filename))
return redirect(request.url)

table = []
if directory:
parent = safe_join(FILES_ROOT, os.path.join(directory, '..'))
if parent:
info = os.stat(parent)
modified = Time(info.st_mtime, format='unix').strftime('%Y-%m-%d&nbsp;%H:%M:%S')
table.append((url_for('files', directory=os.path.join(directory, '..')), 'bi-folder2', '..', modified, '-'))

units = ['B', 'KiB', 'MiB', 'GiB']
for filename in os.listdir(path):
if filename.startswith('.'):
continue
try:
info = os.stat(os.path.join(path, filename))
if stat.S_ISDIR(info.st_mode):
url = url_for('files', directory=os.path.join(directory, filename))
icon = 'bi-folder2'
size = '-'
else:
url = url_for('file', path=os.path.join(directory, filename))
icon = 'bi-file-earmark'
size = info.st_size
i = 0
while size > 1024 and i < len(units):
size /= 1024
i += 1

size = f'{size:.2f}&nbsp;{units[i]}'

modified = Time(info.st_mtime, format='unix').strftime('%Y-%m-%d&nbsp;%H:%M:%S')
table.append((url, icon, filename, modified, size))
except:
pass

return render_template('files.html', table=table, directory=directory)


@app.route('/file/<path:path>')
def file(path):
return send_from_directory(FILES_ROOT, path)


@app.route('/camera/<path:camera>')
def camera_image(camera):
if camera in ['eumetsat', 'allsky', 'dome', 'telescope']:
Expand Down
38 changes: 38 additions & 0 deletions dashboard/templates/files.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends "layout.html" %}
{% set title = 'TCS Control' -%}
{% set active_page = 'files' -%}
{% block body %}
<div class="container">
<div class="d-flex flex-row-reverse">
<div class="mt-3 mb-3" style="width: 400px">
<form method="post" enctype="multipart/form-data">
<div class="input-group input-group-sm">
<input type="file" name="file" class="form-control" aria-label="Upload">
<button class="btn btn-secondary" type="submit">Upload</button>
</div>
</form>
</div>
</div>
<div class="row">
<div class="rounded-0">
<table class="table table-bordered">
<tr>
<td class="fw-bold dashboard-stats w-75">Filename</td>
<td class="text-center fw-bold dashboard-stats w-auto">Modified</td>
<td class="text-end fw-bold dashboard-stats w-auto">Size</td>
</tr>
<tbody>
{% for row in table %}
<tr>
<td class="font-monospace dashboard-stats"><a href="{{ row[0] }}"><i class="{{ row[1] }}" style="margin-right:5px"></i>{{ row[2] }}</a></td>
<td class="text-center text-muted dashboard-stats">{{ row[3]|safe }}</td>
<td class="text-end text-muted dashboard-stats">{{ row[4]|safe }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>

{% endblock %}
3 changes: 3 additions & 0 deletions dashboard/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ <h1 class="navbar-brand">Windmill Hill Observatory</h1>
<li class="nav-item">
<a class="nav-link fw-normal text-nowrap{% if active_page == 'vnc' %} active{% endif %}" href="{{ url_for('vnc') }}"><i class="bi-pc-display-horizontal" style="margin-right:5px"></i>Control</a>
</li>
<li class="nav-item">
<a class="nav-link fw-normal text-nowrap{% if active_page == 'files' %} active{% endif %}" href="{{ url_for('files') }}"><i class="bi-file-earmark" style="margin-right:5px"></i>Files</a>
</li>
</ul>
</div>
</div>
Expand Down

0 comments on commit fdbf276

Please sign in to comment.