-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
178 lines (124 loc) · 5.25 KB
/
server.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import time
import shutil
from flask import Flask, request, render_template
from werkzeug.utils import secure_filename
import mysql_conn
# final upload directory
UPLOAD_FOLDER = './uploads'
# temporary upload directory
TEMP_FOLDER = './temp'
# allowed extensions
ALLOWED_EXTENSIONS = ['mp4', 'mkv']
# initializing the flask app
app = Flask(__name__)
# setting super key for the app
app.secret_key = 'super secret key'
# connecting to the database
mysql = mysql_conn.config_app(app)
# return true if the file type is allowed
def allowed_file(filename):
return '.' in filename and filename.split('.')[-1].lower() in ALLOWED_EXTENSIONS
# returns length and filename of the video
def get_video_length(file):
import cv2
filename = secure_filename(file.filename) # securing the file name
# filename = filename.split('.')
# adding the timestamp to the file name to avoid overwriting
filename = f"{int(time.time())}-{filename}"
# saving the file to the temp folder
file.save(os.path.join(TEMP_FOLDER, filename))
# opening the video file in cv2
video = cv2.VideoCapture(os.path.join(TEMP_FOLDER, filename))
# getting the fps of the video
fps = video.get(cv2.CAP_PROP_FPS)
# getting the total number of frames in the video
frame = video.get(cv2.CAP_PROP_FRAME_COUNT)
# calculating the length of the video in seconds
length = int(frame / fps)
return length, filename
# returns the cost of the video
def get_cost(length, size):
cost = 5 if size <= (500*1024*1024) else 12.5
cost += 12.5 if length <= (6*60 + 18) else 20
return cost
# checks if the file is valid
def validation(file):
if not allowed_file (file.filename):
return {"response":False, "message": "Invalid file type"} # invalid file type
file.size = request.content_length # getting the size of the file
if file.size > 1024**3: # if the file is greater than 1GB
return {"response":False, "message": "File size is too large", "size": file.size}
# getting the length of the video
length, filename = get_video_length(file)
if length > (10*60):
os.remove(os.path.join(TEMP_FOLDER, filename))
return {"response":False, "message": "Video is too long", "length": length}
return {"response":True, "message": "File is valid", "length": length, "filename": filename, "size": file.size}
# endpoint to upload the video
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['file']
# checking if the file is selected or not
if file.filename == '':
return {"response":False, "message": "No file selected"} # no file selected
# checking if the file is valid
validation_result = validation(file)
if validation_result["response"]==False: # if the file is invalid
return validation_result
# moves the valid file to the upload folder
shutil.move(os.path.join(TEMP_FOLDER, validation_result["filename"]),
os.path.join(UPLOAD_FOLDER, validation_result["filename"]))
# computes the cost of the video
cost = get_cost(validation_result["length"], validation_result["size"])
# inserting the video into the database
mysql_conn.insert_video(mysql, validation_result["filename"], validation_result["length"], validation_result["size"], cost)
return {"response" : True,
"message":"sucess uploading video",
"name" : validation_result["filename"],
"size" : validation_result["size"],
"length" : validation_result["length"],
"cost" : cost}
# endpoint to get the video cost
@app.route('/getCost', methods=['POST'])
def get_cost_of_video():
file = request.files['file']
# checking if the file is selected or not
if file.filename == '':
return {"response":False, "message": "No file selected"}
# checking if the file is valid
validation_result = validation(file)
if validation_result["response"]==False: # if the file is invalid
return validation_result
os.remove(os.path.join(TEMP_FOLDER, validation_result["filename"]))
# computes the cost of the video
cost = get_cost(validation_result["length"], validation_result["size"])
return {"response" : True,
"message":"Cost of video",
"cost" : cost}
# endpoint to get the videos
@app.route('/getVideos', methods=["GET", "POST"])
def get_videos():
# if get request then return the all videos
if request.method == "GET":
data = mysql_conn.query_videos(mysql)
return data
# if post request then return the videos based on the filter
elif request.method == "POST":
# getting the filter from the form
if request.form != {}:
data = request.form
# getting the filter from the json
elif request.json != {}:
data = request.json
else:
return {"response":False, "message": "No data received"}
# getting the videos info based on the filter
data = mysql_conn.query_videos_filter(mysql, data)
return data
# endpoint to home page
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)