-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
145 lines (116 loc) · 4.06 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 01:04:38 2023
@author: shree
"""
# redirect if files not loaded
from flask import Flask, render_template, request, Response, send_file, redirect
import server
import client
import file_manager
import json
import os
import hasher
import shutil
from zipfile import ZipFile, ZIP_DEFLATED
app = Flask(__name__)
app.secret_key = "WoC"
glob_vars = {"host_ip":"", "port":"", "message":"upload .jpg"}
# is the hashtable generated?
loaded = len(os.listdir('data')) == 3
@app.route("/")
def home():
glob_vars["message"] = "upload .jpg"
return render_template("index.html")
@app.route("/upload_convert", methods=["POST"])
def upload_convert():
file_manager.init()
glob_vars["message"] = ""
uploaded_files = request.files.getlist("file")
file_manager.refresh('uploaded/')
file_manager.refresh('converted/')
for i, file in enumerate(uploaded_files):
name = file.filename.lower()
if name.endswith('.jpg') or name.endswith('.mp4') or name.endswith('.jpeg'):
with open(f'uploaded/{file.filename}', 'wb') as f:
f.write(file.read())
uploaded_files = [f"uploaded/{file}" for file in os.listdir('uploaded')]
for file in uploaded_files:
glob_vars["message"] = file.split("/")[-1]
if file.endswith('.mp4'):
file_manager.create_video(file)
else:
file_manager.create_photo(file)
zipf = ZipFile('converted files.zip','w', ZIP_DEFLATED)
for file in os.listdir('converted/'):
zipf.write(f'converted/{file}')
zipf.close()
return send_file('converted files.zip',
mimetype='zip',
as_attachment=True)
@app.route("/upload", methods=["POST"])
def upload():
global loaded
loaded = False
tile_size = 20
glob_vars["message"] = "uploading!"
uploaded_files = request.files.getlist("file")
shutil.rmtree('subimages/')
os.makedirs('subimages/')
for i, file in enumerate(uploaded_files):
name = file.filename.lower()
if name.endswith('.jpg') or name.endswith('.jpeg'):
with open(f'subimages/{file.filename}', 'wb') as f:
f.write(file.read())
im_list = hasher.create_imarray(tile_size, 'subimages')
bgr_avg, hashbin = hasher.main(im_list)
hasher.save_data(hashbin, im_list, bgr_avg)
loaded = True
glob_vars["message"]="upload .jpg"
return redirect("/")
@app.route('/feed_server')
def video_server():
return Response(server.feed_generator(glob_vars),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/feed_client')
def video_client():
return Response(client.feed_generator(glob_vars),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/status', methods=['GET'])
def server_status():
return json.dumps(glob_vars)
@app.route("/convert")
def convert():
if loaded:
glob_vars["message"] = ""
return render_template("convert.html")
else:
return redirect("/")
@app.route("/server")
def server_page():
global glob_vars
glob_vars = {"host_ip":"", "port":"", "message":""}
if loaded:
return render_template("server.html")
else:
return redirect("/")
@app.route("/client", methods=["POST", "GET"])
def client_page():
global glob_vars
glob_vars = {"host_ip":"", "port":"", "message":" ", "pressed":False}
if request.method == "POST":
glob_vars["message"] = "Connecting..."
glob_vars["host_ip"] = str(request.form['ip'])
if request.form['port'] == "":
glob_vars["port"] = 0
else:
glob_vars["port"] = int(request.form['port'])
glob_vars["pressed"] = True
return render_template("client.html", video_url="/feed_client")
if loaded:
return render_template("client.html", video_url="")
else:
return redirect("/")
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, port=4020)