-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
119 lines (99 loc) · 3.88 KB
/
__init__.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
#!/usr/bin/env python
import os
import sqlite3
from flask import Flask, render_template, Response, request, redirect, url_for, send_from_directory, session, g, abort, flash
from contextlib import closing
from exposr.processr import Processr
from exposr.viewr import Viewr
from werkzeug import secure_filename
from flask_jsglue import JSGlue
# create our little application :)
app = Flask(__name__)
app.config.from_object('exposr.config')
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
DATABASE = os.path.join(APP_ROOT, 'static/uploads/exposr.db')
app.config['DATABASE'] = DATABASE
jsglue = JSGlue(app)
app.config['ALLOWED_EXTENSIONS'] = set(['mp4'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
@app.route('/')
def show_entries():
cur = g.db.execute('select id, filename, description from entries order by id desc')
entries = [dict(id=row[0], filename=row[1], description=row[2]) for row in cur.fetchall()]
return render_template('show_entries.html', entries=entries)
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
g.db.execute('insert into entries (filename, description) values (?, ?)',
[filename, request.form['description']])
g.db.commit()
flash('New entry was successfully posted')
return redirect(url_for('show_entries'))
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))
@app.route('/remove/<id>')
def rementry(id):
g.db.execute('delete from entries where id=' + id)
g.db.commit()
flash('Entry was successfully removed')
return redirect(url_for('show_entries'))
@app.route('/process/<filename>/<runningFlag>')
def processr(filename,runningFlag):
return Response(gen(Processr(app.config['UPLOAD_FOLDER']+filename),runningFlag),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/view/<filename>/<runningFlag>')
def viewr(filename,runningFlag):
return Response(gen(Viewr(app.config['UPLOAD_FOLDER']+filename),runningFlag),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/viewer/<filename>')
def viewer(filename):
return render_template('viewer.html',filename=filename)
def gen(video,runningFlag):
while True:
frame, isImage = video.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
if runningFlag=='1':
break
if __name__ == '__main__':
init_db()
app.run()