mirror of
https://github.com/Team4388/RidgeScout.git
synced 2026-06-08 16:28:00 -06:00
Add upload and download to python server
This commit is contained in:
+95
-10
@@ -1,6 +1,10 @@
|
|||||||
|
from ast import mod
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from bottle import Bottle, run, get, static_file, response
|
import hashlib
|
||||||
|
from datetime import datetime
|
||||||
|
from bottle import Bottle, run, get, put, static_file, response, request,HTTPResponse
|
||||||
|
from random import SystemRandom
|
||||||
|
|
||||||
from utils import *
|
from utils import *
|
||||||
|
|
||||||
@@ -10,18 +14,52 @@ file_metadata = {}
|
|||||||
|
|
||||||
def save_metadata():
|
def save_metadata():
|
||||||
global file_metadata
|
global file_metadata
|
||||||
write(METADATA_FILE, json.dumps(file_metadata))
|
write(METADATA_PATH4, json.dumps(file_metadata))
|
||||||
|
|
||||||
def load_metadata():
|
def load_metadata():
|
||||||
global file_metadata
|
global file_metadata
|
||||||
data = read(METADATA_FILE)
|
data = read(METADATA_PATH4)
|
||||||
if data is not None:
|
if data is not None:
|
||||||
file_metadata = json.loads(data)
|
file_metadata = json.loads(data)
|
||||||
|
|
||||||
# @app.route('/')
|
api_key = None
|
||||||
# def list():
|
cryptogen = SystemRandom()
|
||||||
# response.content_type = 'application/json'
|
|
||||||
# return json.dumps(ls(DATA_ROOT))
|
def aquire_key():
|
||||||
|
global api_key
|
||||||
|
global cryptogen
|
||||||
|
|
||||||
|
if api_key is None:
|
||||||
|
try:
|
||||||
|
api_key = read(API_KEY_PATH).decode("utf-8").strip()
|
||||||
|
except:
|
||||||
|
ran = cryptogen.randrange(10**80)
|
||||||
|
api_key = "%064x" % ran
|
||||||
|
write(API_KEY_PATH, api_key)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def list_html():
|
||||||
|
global file_metadata
|
||||||
|
load_metadata()
|
||||||
|
|
||||||
|
content = '<html><body><table><tr>'
|
||||||
|
for heading in ['File', 'Size', 'Modified', 'Sha256']:
|
||||||
|
content += f'<th>{heading}</th>'
|
||||||
|
content += "</tr>"
|
||||||
|
|
||||||
|
print(file_metadata)
|
||||||
|
|
||||||
|
for filename in file_metadata.keys():
|
||||||
|
content += "<tr>"
|
||||||
|
content += f'<td><a href="/api/{filename}">{filename}</a></td>'
|
||||||
|
content += f'<td>{file_metadata[filename]["size"]}B</td>'
|
||||||
|
content += f'<td>{file_metadata[filename]["modified"]}</td>'
|
||||||
|
content += f'<td>{file_metadata[filename]["sha256"]}</td>'
|
||||||
|
content += "</tr>"
|
||||||
|
|
||||||
|
content += '</table></body></html>'
|
||||||
|
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/metadata')
|
@app.route('/api/metadata')
|
||||||
@@ -32,11 +70,58 @@ def metadata():
|
|||||||
return json.dumps(file_metadata)
|
return json.dumps(file_metadata)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/<filename>', method='PUT')
|
||||||
|
def upload(filename):
|
||||||
|
global api_key
|
||||||
|
try:
|
||||||
|
sentkey = request.headers[API_KEY_HEADER]
|
||||||
|
if sentkey != api_key:
|
||||||
|
return HTTPResponse(status=403, body=f"Invalid Key {sentkey}, {api_key}")
|
||||||
|
except:
|
||||||
|
return HTTPResponse(status=403, body="You must specify an 'api_key' header")
|
||||||
|
|
||||||
# @app.route('/<filename>')
|
|
||||||
# def hello(filename):
|
global file_metadata
|
||||||
# return static_file(DATA_ROOT, filename)
|
load_metadata()
|
||||||
|
|
||||||
|
data = request.body.read()
|
||||||
|
|
||||||
|
try:
|
||||||
|
modified = request.headers[MODIFIED_HEADER]
|
||||||
|
except:
|
||||||
|
modified = (datetime.now() - datetime(1970, 1, 1)).total_seconds()
|
||||||
|
|
||||||
|
sha256_hash = hashlib.sha256()
|
||||||
|
sha256_hash.update(data)
|
||||||
|
|
||||||
|
file_metadata[filename] = {
|
||||||
|
'size': len(data),
|
||||||
|
'modified': modified,
|
||||||
|
'sha256': sha256_hash.hexdigest()
|
||||||
|
}
|
||||||
|
|
||||||
|
save_metadata()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
write(os.path.join(DATA_ROOT, filename), data)
|
||||||
|
# save_metadata()
|
||||||
|
# response.content_type = 'application/json'
|
||||||
|
# return json.dumps(file_metadata)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/<filename>')
|
||||||
|
def download(filename):
|
||||||
|
|
||||||
|
data = read(os.path.join(DATA_ROOT, filename))
|
||||||
|
|
||||||
|
if data is not None:
|
||||||
|
response.content_type = 'application/octet-stream'
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
HTTPResponse(status=404, body="File not found")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
mkdir(DATA_ROOT)
|
mkdir(DATA_ROOT)
|
||||||
|
aquire_key()
|
||||||
app.run(host='localhost', port=8080)
|
app.run(host='localhost', port=8080)
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
bottle
|
bottle
|
||||||
|
random
|
||||||
|
|||||||
+15
-5
@@ -1,9 +1,14 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
ROOT = os.path.dirname(__file__)
|
ROOT = os.path.dirname(__file__)
|
||||||
METADATA_FILE = os.path.join(ROOT, 'metadata.json')
|
|
||||||
DATA_ROOT = os.path.join(os.path.dirname(__file__), 'data')
|
DATA_ROOT = os.path.join(os.path.dirname(__file__), 'data')
|
||||||
|
|
||||||
|
METADATA_PATH4 = os.path.join(ROOT, 'metadata.json')
|
||||||
|
API_KEY_PATH = os.path.join(ROOT, 'api_key.txt')
|
||||||
|
|
||||||
|
MODIFIED_HEADER = 'modified'
|
||||||
|
API_KEY_HEADER = 'api_key'
|
||||||
|
|
||||||
def mkdir(path):
|
def mkdir(path):
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
@@ -18,13 +23,18 @@ def read(path):
|
|||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
with open(path) as f:
|
with open(path, 'rb') as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(f"Error reading file {path}: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def write(path, data):
|
def write(path, data):
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
with open(path, mode='a'): pass
|
with open(path, mode='ab'): pass
|
||||||
with open(path, 'w') as f:
|
|
||||||
|
if isinstance(data, str):
|
||||||
|
data = str.encode(data)
|
||||||
|
|
||||||
|
with open(path, 'wb') as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user