diff options
-rw-r--r-- | flask_app.py | 7 | ||||
-rw-r--r-- | storage.py | 23 |
2 files changed, 28 insertions, 2 deletions
diff --git a/flask_app.py b/flask_app.py index a69f4f8..87697e5 100644 --- a/flask_app.py +++ b/flask_app.py @@ -3,10 +3,12 @@ The web application built on Flask is contained within this file. When run as a script, the Flask development server is started. """ -import submission_pb2 +import os +import submission_pb2, storage from flask import Flask, request app = Flask(__name__) +store = storage.FilesystemStorage('logs/') @app.route('/') def index(): @@ -20,7 +22,8 @@ def submit(): """ submission = submission_pb2.Submission() submission.ParseFromString(request.data) - return str(submission) + store.save_file(request.remote_addr, submission.filename, submission.data) + return '' if __name__ == '__main__': app.run(host='::1', debug=True) diff --git a/storage.py b/storage.py new file mode 100644 index 0000000..847806f --- /dev/null +++ b/storage.py @@ -0,0 +1,23 @@ +""" +Implements storage of collected log files in the local filesystem. +""" + +import os, errno + +class FilesystemStorage: + def __init__(self, root): + self.root = root + try: + os.mkdir(root) + except OSError: + pass # TODO: proper handling + + def save_file(self, source, filename, data): + try: + os.mkdir(os.path.join(self.root, source)) + except OSError: + pass # TODO: proper handling + + path = os.path.join(self.root, source, filename) # TODO: consider adding in date at some point + with open(path, 'wb') as f: + f.write(data) |