forked from islandarkproject-org/islandarkproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
249 lines (210 loc) · 8.32 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import sys
import os
from functools import wraps
from flask import *
import json
from werkzeug import secure_filename
from PIL import Image
import glob
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
rootFolder = os.path.dirname(os.path.realpath(__file__))
app.template_folder= rootFolder + '/Files/'
filesPath = rootFolder + '/Files'
UPLOAD_FOLDER = rootFolder + '/Files/userFiles'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
sys.path.insert(0, rootFolder + '/ServerCode')
sys.path.insert(0, filesPath)
from ServerFacade import *
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def check_auth(user, password):
#value will acutaly be a random identifing token created when logged in and stored in DB. it will only be good
#for 24 hours then it wont be good.
if session.has_key('value'): #add check that user is value in session
return True
else:
return False
#def authenticate():
# """Sends a 401 response that enables basic auth"""
# return Response(
# 'Could not verify your access level for that URL.\n'
# 'You have to login with proper credentials', 401,
# {'WWW-Authenticate': 'Basic realm="Login Required"'})
# if session.has_key('value'):
# return True
# else:
# as return False
#def requires_auth(f):
# @wraps(f)
# def decorated(*args, **kwargs):
# auth = request.authorization
# if not auth or not check_auth(auth.username, auth.password):
# return authenticate()
# return f(*args, **kwargs)
# return decorated
@app.route("/")
def index():
return render_template('main.html')
#return send_from_directory(filesPath,'main.html')
@app.route("/retrieveFileDetails/<fileIdIn>")
def getFileDetails(fileIdIn):
serverFacade = ServerFacade()
result = serverFacade.retrieveFileDetails(session['value'],fileIdIn)
return json.dumps(result)
@app.route("/retrieveThumb/<fileIdIn>")
def getThumb(fileIdIn):
serverFacade = ServerFacade()
result, fileName, owner, thumb = serverFacade.checkFileAuth(session['value'],fileIdIn)
if(result == True):
#if file not exist deal with it correctly
return send_from_directory(filesPath + "/userFiles/"+owner,thumb)
else:
return "Not Authorized"
@app.route("/retrieveFile/<fileIdIn>")
def getFile(fileIdIn):
serverFacade = ServerFacade()
result, fileName, owner,thumb = serverFacade.checkFileAuth(session['value'],fileIdIn)
if(result == True):
#if file not exist deal with it correctly
return send_from_directory(filesPath + "/userFiles/"+owner,fileName)
else:
return "Not Authorized"
@app.route("/uploadFile", methods=['GET','POST'])
def upload():
if(check_auth("NONE", "GOG") == True):
#return json.dumps({'status':'here'})
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = filename.rstrip()
if not os.path.exists(app.config['UPLOAD_FOLDER'] + '/' + session['value']):
os.makedirs(app.config['UPLOAD_FOLDER'] + '/' + session['value'])
#find if this filename already exists and rename it if needs be.
i = 1
savedFileName = filename
while os.path.exists(app.config['UPLOAD_FOLDER'] + '/' + session['value'] + '/' + filename):
filename = savedFileName.rsplit('.',1)
extension = filename[1]
filename = filename[0]
filename = filename + "(" + str(i) + ")" + '.' + extension
i = i + 1
file.save(os.path.join(app.config['UPLOAD_FOLDER'] + '/' + session['value'], filename))
#create the thumb name for the thing now
size = 128, 128
infile = app.config['UPLOAD_FOLDER'] + '/' + session['value'] + '/' + filename
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.thumbnail(size)
thumbFilePath = file + ".thumbnail" + ext
extAr = ext.split('.')
im.save(thumbFilePath)
thumbFile = thumbFilePath.rsplit('/',1)[1]
name = request.form['name']
date = request.form['date']
description = request.form['description']
location = request.form['location']
privacy = request.form['privacy']
#save the meta data to the database
serverFacade = ServerFacade()
serverFacade.saveFile(session['value'],filename, app.config['UPLOAD_FOLDER'] + '/' + session['value'], name, date, description, location,privacy, thumbFile)
return json.dumps({'status':'success', 'other':privacy, 'session':session['value']})
else:
return json.dumps({'status':"notAuth"})
def errorPage(error):
return Response("An internal Error has occurred", 500)
@app.route("/getData", methods=['GET'])
def getData():
serverFacade = ServerFacade()
data = serverFacade.getData(session['value'])
return Response(json.dumps({'status':'success', 'data':data}), mimetype='application/json')
@app.route('/deletePic/<hashIn>', methods=['GET'])
def deletePic(hashIn):
serverFacade = ServerFacade()
return json.dumps({'status':serverFacade.deletePic(hashIn, session['value'])})
@app.route("/login", methods=['POST'])
def login():
data = request.get_json()
userName = data['userName'].lower()
password = data['password']
serverFacade = ServerFacade()
if(serverFacade.login(userName, password)):
session['value'] = userName.lower()
return json.dumps({'status':"success"})
else:
return json.dumps({'status':"notAuth"})
@app.route("/register", methods=['POST'])
def register():
data = request.get_json()
serverFacade = ServerFacade()
#check is pass and passValidate are the same, double check
if(serverFacade.register(data["userName"], data["password"], data["email"], data["fName"])):
#create folder in userFiles named the usernames
session['value'] = data["userName"].lower()
return json.dumps({'status':'success'})
else:
return json.dumps({'status':'error', 'data':'User name or email already registered'})
#status = registerUser(data)
#if(status == "success"):
# session['value'] = data['email'].lower()
# return json.dumps({'status':"success"})
#else:
# return authenticate1(status)
@app.route("/verify", methods=['GET'])
def verify():
temp = check_auth("NONCE", "GUFL")
return json.dumps({'status':temp})
@app.route("/logout")
def logout():
session.pop('value', None)
session["__invalidate__"] = True
return "true"#return send_from_directory(rootFolder,"logout.html")
@app.after_request
def removeCookie(response):
if"__invalidate__" in session:
response.delete_cookie(app.session_cookie_name)
return response
@app.route("/getBios")
def getBios():
from theTeam import *
temp = theTeam
#return jsonify(temp)
return json.dumps(temp)
@app.route("/getSession", methods=['GET','POST'])
def getSession():
if not session.has_key('value'):
return "Session value not set."
else:
return "Session value set to: " + str(session['value'])
@app.route("/getPersonDetails")
def getPersonDetails():
if(not session.has_key('value')):
return json.dumps({'status':'Not logged in'})
serverFacade = ServerFacade()
return json.dumps(serverFacade.getPersonDetails(session['value']))
@app.route("/updatePersonDetails", methods=['POST'])
def updatePersonDetails():
if(not session.has_key('value')):
return json.dumps({'status':'Not logged in'})
data = request.get_json()
#return json.dumps({'status':'updatePersonDetails', 'data':data})
serverFacade = ServerFacade()
return json.dumps({'status':serverFacade.updatePersonDetails(session['value'], data)})
@app.route("/updatePassword", methods=['POST'])
def updatePassword():
if(not session.has_key('value')):
return json.dumps({'status':'Not logged in'})
data = request.get_json()
#return json.dumps({'status':'updatePassword', 'data':data})
serverFacade = ServerFacade()
return json.dumps({'status':serverFacade.updatePassword(session['value'], data)})
@app.route("/test")
def test():
#return 'hey'
#myServerFacade = ServerFacade()
return json.dumps(testDB())
if __name__ == "__main__":
first = rootFolder + 'server.crt'
second = rootFolder + 'server.key'
app.run('0.0.0.0',debug=True)#, ssl_context=(first,second))