Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
agentmilindu authored Oct 6, 2018
2 parents d7db574 + 5dcfefd commit 865b7b2
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 127 deletions.
98 changes: 61 additions & 37 deletions components/core/Server.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
from flask import Flask
from flask.ext.cors import CORS
from initializer import server, socketio
from flask import send_file, send_from_directory
from flask import request, jsonify, abort, Response, g
from flask_socketio import SocketIO, join_room
from Auth import *
from Models import *
from DownloadManager import *
import json, urllib.request, urllib.error, urllib.parse, os, _thread
from multiprocessing import Process
from DownloadDaemon import starter
from EMail import send_mail
import sys
from gevent import monkey

monkey.patch_all(ssl=False)

server = Flask(__name__)
server.config['SECRET_KEY'] = "123456789"
socketio = SocketIO(server, debug=True, logger=True, engineio_logger=True, ping_timeout=600)
cors = CORS(server)
p = None
verbose = False

if len(sys.argv) == 2 and sys.argv[1] == '-v':
verbose = True

def token_validator(token):
user = verify_auth_token(token, server.config['SECRET_KEY'])
if user != None:
g.user = user
token = generate_auth_token(user, server.config['SECRET_KEY'])
return token
return None
from flask_socketio import join_room
import os
from file_sender import sender
from routes import User, Download


# socket connections
@socketio.on('join', namespace='/progress')
def on_join(data):
room = data['room']
if room != '':
join_room(room)


# endpoints

@server.route('/ui/<string:path>')
def serve_ui(path):
return send_from_directory(os.path.dirname(os.path.realpath(__file__))+"/ui", path)
return send_from_directory(os.path.dirname(os.path.realpath(__file__)) + "/ui", path)


@server.route('/ui/')
def serve_ui1():
return send_file(os.path.dirname(os.path.realpath(__file__))+"/ui/index.html")
return send_file(os.path.dirname(os.path.realpath(__file__)) + "/ui/index.html")


# download endpoints
server.add_url_rule(rule='/download/start', endpoint='start', view_func=Download.start)
server.add_url_rule(rule='/download/kill', endpoint='kill', view_func=Download.kill)
server.add_url_rule(rule='/api/download', endpoint='add_download_request', view_func=Download.add_download_request,
methods=['POST'])
server.add_url_rule(rule='/api/download/<int:id>', endpoint='remove_download_request',
view_func=Download.remove_download_request, methods=['DELETE'])
server.add_url_rule(rule='/api/download/rate/<int:id>', endpoint='rate_download_request',
view_func=Download.rate_download_request, methods=['POST'])
server.add_url_rule(rule='/api/downloads/<int:limit>', endpoint='get_downloads_request',
view_func=Download.get_downloads_request, methods=['GET'])
server.add_url_rule(rule='/api/download/<int:id>', endpoint='get_download', view_func=Download.get_download,
methods=['GET'])

# user action api endpoints
server.add_url_rule(rule='/api/login', endpoint='login', view_func=User.login, methods=['POST'])
server.add_url_rule(rule='/api/regularuser', endpoint='regular_user_request', view_func=User.regular_user_request,
methods=['POST'])
server.add_url_rule(rule='/api/user', endpoint='add_user_request', view_func=User.add_user_request, methods=['POST'])
server.add_url_rule(rule='/api/user/<string:username>', endpoint='remove_user_request',
view_func=User.remove_user_request, methods=['DELETE'])
server.add_url_rule(rule='/api/user/<string:username>', endpoint='update_user_request',
view_func=User.update_user_request, methods=['PUT'])
server.add_url_rule(rule='/api/user', endpoint='get_users_request', view_func=User.get_users_request, methods=['GET'])
server.add_url_rule(rule='/api/user/requests', endpoint='get_user_signup_requests',
view_func=User.get_user_signup_requests, methods=['GET'])
server.add_url_rule(rule='/api/user/approve/<string:username>', endpoint='approve_user_request',
view_func=User.approve_user_request, methods=['POST'])
server.add_url_rule(rule='/api/user/blocked', endpoint='get_blocked_users_request',
view_func=User.get_blocked_users_request, methods=['GET'])
server.add_url_rule(rule='/api/user/blocked/<string:username>', endpoint='block_user_request',
view_func=User.block_user_request, methods=['POST'])
server.add_url_rule(rule='/api/user/blocked/<string:username>', endpoint='unblock_user_request',
view_func=User.unblock_user_request, methods=['DELETE'])
server.add_url_rule(rule='/api/user/downloads/<int:limit>', endpoint='get_downloads_user_request',
view_func=User.get_downloads_user_request, methods=['GET'])
server.add_url_rule(rule='/api/user/heavy', endpoint='get_topten_heaviest_users',
view_func=User.get_topten_heaviest_users, methods=['GET'])

# Import routes
from routes import *
# file sending endpoints
server.add_url_rule(rule='/api/file', endpoint='send_file_from_path', view_func=sender.send_file_from_path,
methods=['GET'])
17 changes: 11 additions & 6 deletions components/core/file_sender/sender.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from flask import Response, request, send_file, jsonify
from DownloadManager import get_id_from_gid, get_download_path, get_download_name_from_id, get_compression_progress
import logging
from Server import token_validator
import subprocess
import os
import json
from utils.token_utils import token_validator


def send_file_from_path():
token = token_validator(request.args['token'])
token = None
if request.args.get('share') is not None:
token = 'sample-token-for-sharing-feature'
if token is None:
token = token_validator(request.headers['token'])
if token is not None:
try:
gid = request.args.get('gid')
Expand Down Expand Up @@ -56,7 +57,11 @@ def file_name_formatter(file_name):


def start_compression():
token = token_validator(request.headers['token'])
token = None
if request.args.get('share') is not None:
token = 'sample-token-for-sharing-feature'
if token is None:
token = token_validator(request.headers['token'])
if token is not None:
gid_list = request.get_json()['gid'] # list of file identifiers to compress
gid_list.sort()
Expand Down
19 changes: 19 additions & 0 deletions components/core/initializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
from flask import Flask
from flask_cors import CORS
from flask_socketio import SocketIO
from gevent import monkey

from utils.app_constants import SERVER_SECRET_KEY

monkey.patch_all(ssl=False)

server = Flask(__name__)
server.config['SECRET_KEY'] = SERVER_SECRET_KEY
socketio = SocketIO(server, debug=True, logger=True, engineio_logger=True, ping_timeout=600)
cors = CORS(server)
p = None
verbose = False

if len(sys.argv) == 2 and sys.argv[1] == '-v':
verbose = True
55 changes: 15 additions & 40 deletions components/core/routes/Download.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
from flask import Flask
from flask.ext.cors import CORS
from flask import send_file, send_from_directory
from flask import request, jsonify, abort, Response, g
from flask_socketio import SocketIO, join_room
from flask import send_file
from flask import request, Response, g
from Auth import *
from Models import *
from DownloadManager import *
import json, urllib.request, urllib.error, urllib.parse, _thread
import json, urllib.request, urllib.error, urllib.parse
from multiprocessing import Process
from DownloadDaemon import starter
from EMail import send_mail
from gevent import monkey
from Server import *
from file_sender import sender
from utils.app_constants import SERVER_SECRET_KEY
from utils.token_utils import token_validator
from initializer import socketio


@server.route('/download/start')
def start():
try:
token = request.headers['key']
if str(token) != server.config['SECRET_KEY']:
if str(token) != SERVER_SECRET_KEY:
return "{'error':'not authorized'}", 403
global p
p = Process(target=starter, args=(socketio,))
p.start()
return '{"status":"' + str(p.pid) + '"}'
except Exception as e:
return '{"error":"' + e.message + '"}', 400
return '{"error":"' + str(e) + '"}', 400


@socketio.on('join', namespace='/progress')
def on_join(data):
room = data['room']
if room != '':
join_room(room)


@server.route('/download/kill')
def kill():
try:
token = request.headers['key']
if str(token) != server.config['SECRET_KEY']:
if str(token) != SERVER_SECRET_KEY:
return "{'error':'not authorized'}", 403
if p is not None:
p.terminate()
Expand All @@ -55,10 +41,9 @@ def kill():
else:
return '{"error":"error"}'
except Exception as e:
return '{"error":"' + e.message + '"}', 400
return '{"error":"' + str(e) + '"}', 400


@server.route('/api/download', methods=['POST'])
def add_download_request():
token = token_validator(request.headers['token'])
if token is not None:
Expand All @@ -71,7 +56,7 @@ def add_download_request():
status = add_download(newDownload)
resp = Response(response='{"status":"' + status + '"}', status=200 if status == "success" else 400)
except Exception as e:
resp = Response(response='{"error":"' + e.message + '"}', status=400)
resp = Response(response='{"error":"' + str(e) + '"}', status=400)
resp.headers['token'] = token
resp.headers['Access-Control-Expose-Headers'] = 'token'
return resp
Expand All @@ -81,15 +66,14 @@ def add_download_request():
return '{"error":"token error"}', 403


@server.route('/api/download/<int:id>', methods=['DELETE'])
def remove_download_request(id):
token = token_validator(request.headers['token'])
if token is not None:
try:
status = remove_download(id, g.user.userName)
resp = Response(response='{"status":"' + status + '"}', status=(200 if status == "success" else 400))
except Exception as e:
resp = Response(response='{"error":"' + e.message + '"}', status=400)
resp = Response(response='{"error":"' + str(e) + '"}', status=400)
resp.headers['token'] = token
resp.headers['Access-Control-Expose-Headers'] = 'token'
return resp
Expand All @@ -99,7 +83,6 @@ def remove_download_request(id):
return '{"error":"token error"}', 403


@server.route('/api/download/rate/<int:id>', methods=['POST'])
def rate_download_request(id):
token = token_validator(request.headers['token'])
if token is not None:
Expand All @@ -108,7 +91,7 @@ def rate_download_request(id):
status = rate_download(id, g.user.userName, data['rate'])
resp = Response(response='{"status":"' + status + '"}', status=(200 if status == "success" else 400))
except Exception as e:
resp = Response(response='{"error":"' + e.message + '"}', status=400)
resp = Response(response='{"error":"' + str(e) + '"}', status=400)
resp.headers['token'] = token
resp.headers['Access-Control-Expose-Headers'] = 'token'
return resp
Expand All @@ -118,7 +101,6 @@ def rate_download_request(id):
return '{"error":"token error"}', 403


@server.route('/api/downloads/<int:limit>', methods=['GET'])
def get_downloads_request(limit):
token = token_validator(request.headers['token'])
if token is not None:
Expand All @@ -129,7 +111,7 @@ def get_downloads_request(limit):
else:
resp = Response('{"error":"' + status + '"}', status=400)
except Exception as e:
resp = Response(response='{"error":"' + e.message + '"}', status=400)
resp = Response(response='{"error":"' + str(e) + '"}', status=400)
resp.headers['token'] = token
resp.headers['Access-Control-Expose-Headers'] = 'token'
return resp
Expand All @@ -139,7 +121,6 @@ def get_downloads_request(limit):
return '{"error":"token error"}', 403


@server.route('/api/download/<int:id>', methods=['GET'])
def get_download(id):
token = token_validator(request.headers['token'])
if token is not None:
Expand All @@ -152,15 +133,9 @@ def get_download(id):
else:
return '{"error":"file not found"}', 404
except Exception as e:
resp = Response(response="{'error':'" + e.message + "'}", status=400)
resp = Response(response="{'error':'" + str(e) + "'}", status=400)
return resp
elif token is not None:
return '{"error":"not authorized"}', 403
else:
return '{"error":"token error"}', 403


# register endpoints
server.add_url_rule('/api/file', 'send_file_from_path', sender.send_file_from_path, methods=['GET'])
server.add_url_rule('/api/compress', 'start_compression', sender.start_compression, methods=['POST'])
server.add_url_rule('/api/compression-progress', 'get_compression_progress', sender.check_compression_progress, methods=['GET'])
Loading

0 comments on commit 865b7b2

Please sign in to comment.