-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
144 lines (102 loc) · 3.41 KB
/
main.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
import json
import os
import sqlite3
import mimetypes
from flask import Flask, request, flash, make_response, g
from flask_cors import CORS
from file_domain import File
from settings import SETTINGS
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = SETTINGS.get('UPLOAD_FOLDER', '/tmp')
CORS(app)
DATABASE = SETTINGS.get('DATABASE')
mimetypes.init()
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
# helper to close
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
class FileDAO(object):
def create(self, file_object):
# type: (File) -> File
conn = get_db()
cur = conn.cursor()
cur.execute('INSERT INTO files (file_id, file_name, type) VALUES (?, ?, ?)',
(file_object.file_id, file_object.file_name, file_object.type))
conn.commit()
return file_object
def get(self, file_id):
# type: (str) -> File or None
conn = get_db()
cur = conn.cursor()
cur.execute('SELECT * FROM files WHERE file_id = ?', (file_id,))
file_data = cur.fetchone()
if not file_data:
return None
return File(*file_data)
def get_all(self):
# type: () -> [File]
conn = get_db()
cur = conn.cursor()
cur.execute('SELECT * FROM files')
files_data = cur.fetchall()
if not files_data:
return []
return [File(*file_data) for file_data in files_data]
def delete(self, file_id):
# type: (str) -> None
conn = get_db()
cur = conn.cursor()
cur.execute('DELETE FROM files WHERE file_id == ?', (file_id,))
conn.commit()
file_dao = FileDAO()
@app.route('/file_info/<file_id>', methods=['GET'])
def get_file_info(file_id):
# type: (str) -> {}
file_obj = file_dao.get(file_id)
if file_obj:
return json.dumps(file_obj.serialize())
return {}
@app.route('/file_info', methods=['GET'])
def get_all_file_info():
# type: () -> []
file_objects = file_dao.get_all()
return json.dumps([file_obj.serialize() for file_obj in file_objects if file_obj])
@app.route('/upload_file', methods=['POST'])
def upload_file():
# type: () -> {}
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return {}
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if not file or file.filename == '':
flash('No selected file')
return {}
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
_, ext = file.filename.rsplit('.')
file_obj = File(
file_id=None,
file_name=file.filename,
type=mimetypes.types_map['.' + ext]
)
file_obj = file_dao.create(file_obj)
data = file_obj.serialize()
return json.dumps(data)
@app.route('/file/<file_name>', methods=['GET'])
def serve_files(file_name):
# type: (str) -> {}
with open(os.path.join(app.config['UPLOAD_FOLDER'], file_name), 'rb') as fd:
response = make_response(fd.read())
_, ext = file_name.rsplit('.')
response.headers.set('Content-Type', mimetypes.types_map['.' + ext])
return response
app.run(host='localhost', port=9000)