From eff059b289a72a610d40373bcb80ced1635b1a71 Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 15:18:31 +0530 Subject: [PATCH 1/7] Still under building stage --- Downloads/__init__.py | 1 + Downloads/down.ini | 2 ++ Downloads/download.py | 28 +++++++++++++++++++ __init__.py | 14 ++++++++++ routes/__init__.py | 6 ++++ routes/baseroutes.py | 18 ++++++++++++ templates/download_form.html | 11 ++++++++ {uploads => templates}/index.html | 0 .../upload_form.html | 0 uploads/__init__.py | 4 +++ upload.py => uploads/upload.py | 14 ++-------- 11 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 Downloads/__init__.py create mode 100644 Downloads/down.ini create mode 100644 Downloads/download.py create mode 100644 __init__.py create mode 100644 routes/__init__.py create mode 100644 routes/baseroutes.py create mode 100644 templates/download_form.html rename {uploads => templates}/index.html (100%) rename upload_form.html => templates/upload_form.html (100%) create mode 100644 uploads/__init__.py rename upload.py => uploads/upload.py (64%) diff --git a/Downloads/__init__.py b/Downloads/__init__.py new file mode 100644 index 0000000..ef39ffc --- /dev/null +++ b/Downloads/__init__.py @@ -0,0 +1 @@ +__author__ = 'karnikamit' diff --git a/Downloads/down.ini b/Downloads/down.ini new file mode 100644 index 0000000..9f779b1 --- /dev/null +++ b/Downloads/down.ini @@ -0,0 +1,2 @@ +[down_path] +path = "/home/karnikamit/Documents/keygen_ssh.txt" \ No newline at end of file diff --git a/Downloads/download.py b/Downloads/download.py new file mode 100644 index 0000000..cf3d7fc --- /dev/null +++ b/Downloads/download.py @@ -0,0 +1,28 @@ +__author__ = 'karnikamit' +from simpleconfigparser import simpleconfigparser +import os +import StringIO +from flask import send_file +configfile = simpleconfigparser() + + +class Download: + def __init__(self, path): + app_route = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0] + configfile.read(app_route+'/Downloads/down.ini') + self.path = path + self.f_name = (self.path.split('.')[0]).split('/')[-1] + self.extn = self.path.split('.')[-1] + + def download_file(self): + file_open = open(self.path, 'rb') + f = file_open.read() + + # Downloading the file + strIO = StringIO.StringIO() + strIO.write(f) + strIO.seek(0) + file_open.close() + return send_file(strIO, + attachment_filename=self.f_name+'.'+self.extn, + as_attachment=True) diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..3feb01e --- /dev/null +++ b/__init__.py @@ -0,0 +1,14 @@ +__author__ = 'karnikamit' +from tornado.wsgi import WSGIContainer +from tornado.httpserver import HTTPServer +from tornado.ioloop import IOLoop +from routes import app +import sys + + +if __author__ == 'karnikamit': + sys.dont_write_bytecode = True + http_server = HTTPServer(WSGIContainer(app)) + http_server.listen(7000) + print 'Running on:', 7000 + IOLoop.instance().start() diff --git a/routes/__init__.py b/routes/__init__.py new file mode 100644 index 0000000..10d9d7f --- /dev/null +++ b/routes/__init__.py @@ -0,0 +1,6 @@ +__author__ = 'karnikamit' +from flask import Flask + +app = Flask(__name__) +from routes import baseroutes + diff --git a/routes/baseroutes.py b/routes/baseroutes.py new file mode 100644 index 0000000..82837ab --- /dev/null +++ b/routes/baseroutes.py @@ -0,0 +1,18 @@ +__author__ = 'karnikamit' +from flask import jsonify, request, render_template +from routes import app +from Downloads.download import Download + +@app.route('/try', methods=["GET"]) +def app_try(): + return jsonify({"response": "success"}) + +@app.route('/Download', methods=["POST", "GET"]) +def download_file(): + path = request.json + do = Download(path) + try: + do.download_file() + return jsonify({"response": "success"}) + except Exception, e: + return jsonify({"response": "failure", "msg": str(e)}) \ No newline at end of file diff --git a/templates/download_form.html b/templates/download_form.html new file mode 100644 index 0000000..2578616 --- /dev/null +++ b/templates/download_form.html @@ -0,0 +1,11 @@ + + + + + Download File + + + +

Download File

+ + \ No newline at end of file diff --git a/uploads/index.html b/templates/index.html similarity index 100% rename from uploads/index.html rename to templates/index.html diff --git a/upload_form.html b/templates/upload_form.html similarity index 100% rename from upload_form.html rename to templates/upload_form.html diff --git a/uploads/__init__.py b/uploads/__init__.py new file mode 100644 index 0000000..e199491 --- /dev/null +++ b/uploads/__init__.py @@ -0,0 +1,4 @@ +__author__ = 'karnikamit' + + + diff --git a/upload.py b/uploads/upload.py similarity index 64% rename from upload.py rename to uploads/upload.py index 5dacd13..bfef123 100644 --- a/upload.py +++ b/uploads/upload.py @@ -1,13 +1,10 @@ -import tornado.httpserver, tornado.ioloop, tornado.options, tornado.web, os.path, random, string -from tornado.options import define, options -define("port", default=8888, help="run on the given port", type=int) class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", IndexHandler), - (r"/upload", UploadHandler) + (r"/templates", UploadHandler) ] tornado.web.Application.__init__(self, handlers) @@ -25,11 +22,4 @@ def post(self): output_file = open("uploads/" + final_filename, 'w') output_file.write(file1['body']) self.finish("file" + final_filename + " is uploaded") - -def main(): - http_server = tornado.httpserver.HTTPServer(Application()) - http_server.listen(options.port) - tornado.ioloop.IOLoop.instance().start() - -if __name__ == "__main__": - main() + From 93c64e7731e6a1552d4f0e33b345b1c6ec4bec2f Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 15:20:26 +0530 Subject: [PATCH 2/7] Still under building stage --- Downloads/download.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Downloads/download.py b/Downloads/download.py index cf3d7fc..576df69 100644 --- a/Downloads/download.py +++ b/Downloads/download.py @@ -1,15 +1,10 @@ __author__ = 'karnikamit' -from simpleconfigparser import simpleconfigparser -import os import StringIO from flask import send_file -configfile = simpleconfigparser() class Download: def __init__(self, path): - app_route = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0] - configfile.read(app_route+'/Downloads/down.ini') self.path = path self.f_name = (self.path.split('.')[0]).split('/')[-1] self.extn = self.path.split('.')[-1] From ac49f6e52289a53bf8065e4148b384a26b1c6215 Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 15:27:48 +0530 Subject: [PATCH 3/7] Still under building stage --- routes/baseroutes.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/routes/baseroutes.py b/routes/baseroutes.py index 82837ab..d20cbbb 100644 --- a/routes/baseroutes.py +++ b/routes/baseroutes.py @@ -3,11 +3,13 @@ from routes import app from Downloads.download import Download + @app.route('/try', methods=["GET"]) -def app_try(): +def api_try(): return jsonify({"response": "success"}) -@app.route('/Download', methods=["POST", "GET"]) + +@app.route('/get_file', methods=["POST", "GET"]) def download_file(): path = request.json do = Download(path) @@ -15,4 +17,14 @@ def download_file(): do.download_file() return jsonify({"response": "success"}) except Exception, e: - return jsonify({"response": "failure", "msg": str(e)}) \ No newline at end of file + return jsonify({"response": "failure", "msg": str(e)}) + + +@app.route("/Download", methods=["GET"]) +def download_api(): + return render_template('download_form.html') + + +@app.route("/Upload", methods=["GET"]) +def upload_api(): + return render_template('upload_form.html') \ No newline at end of file From d5272f8ca2338d4f24a498f1e3b269aa4cf26034 Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 15:49:55 +0530 Subject: [PATCH 4/7] Still under building stage --- routes/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/routes/__init__.py b/routes/__init__.py index 10d9d7f..3f1d9db 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -1,6 +1,7 @@ __author__ = 'karnikamit' from flask import Flask - -app = Flask(__name__) +import os +app_route = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0] +app = Flask(__name__, template_folder=app_route+'/templates') from routes import baseroutes From b8303c0f6a607d49f437b3cffbbcf98a5971c1af Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 15:56:15 +0530 Subject: [PATCH 5/7] Still under building stage --- routes/baseroutes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/baseroutes.py b/routes/baseroutes.py index d20cbbb..0d67250 100644 --- a/routes/baseroutes.py +++ b/routes/baseroutes.py @@ -15,7 +15,7 @@ def download_file(): do = Download(path) try: do.download_file() - return jsonify({"response": "success"}) + return jsonify({"response": "success", "msg": "file successfully downloaded."}) except Exception, e: return jsonify({"response": "failure", "msg": str(e)}) From aa17a4a2d1c929b1d9d95d4b99dbd0d6fbb00cd4 Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 22:41:52 +0530 Subject: [PATCH 6/7] update for downloading --- routes/baseroutes.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/routes/baseroutes.py b/routes/baseroutes.py index 0d67250..cc34c04 100644 --- a/routes/baseroutes.py +++ b/routes/baseroutes.py @@ -4,14 +4,15 @@ from Downloads.download import Download -@app.route('/try', methods=["GET"]) +@app.route('/try', methods=["POST"]) def api_try(): - return jsonify({"response": "success"}) + data = request.json + return jsonify(data) @app.route('/get_file', methods=["POST", "GET"]) def download_file(): - path = request.json + path = request.form['path'] do = Download(path) try: do.download_file() @@ -27,4 +28,4 @@ def download_api(): @app.route("/Upload", methods=["GET"]) def upload_api(): - return render_template('upload_form.html') \ No newline at end of file + return render_template('upload_form.html') From d044ebbe5950d2641c71a93c9cbf711b9ceb7e4c Mon Sep 17 00:00:00 2001 From: karnikamit Date: Mon, 27 Jul 2015 22:43:58 +0530 Subject: [PATCH 7/7] basic download html --- templates/download_form.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/templates/download_form.html b/templates/download_form.html index 2578616..8a73eaf 100644 --- a/templates/download_form.html +++ b/templates/download_form.html @@ -7,5 +7,8 @@

Download File

- - \ No newline at end of file +
+ file path: +
+ + \ No newline at end of file