-
Notifications
You must be signed in to change notification settings - Fork 218
Upload zip to server #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f1712d9
5125509
38401a6
ae432c7
5f4d737
98df692
1e5239e
6a31c60
1ce4300
c0c123d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| FROM tiangolo/uwsgi-nginx-flask:python2.7 | ||
|
|
||
| COPY ./ ./ | ||
|
|
||
| EXPOSE 5000 | ||
|
|
||
| RUN pip install -r requirements.txt | ||
|
|
||
| CMD gunicorn --timeout 0 -c config.py wsgi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # config for gunicorn server | ||
|
|
||
| bind = '0.0.0.0:5000' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ click | |
| flask | ||
| markdown2 | ||
| emoji | ||
| gunicorn | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import os | ||
| import flask | ||
| from flask import request, redirect, url_for, flash | ||
| from slackviewer.archive import extract_archive | ||
| from slackviewer.reader import Reader | ||
|
|
||
|
|
||
| app = flask.Flask( | ||
|
|
@@ -7,14 +11,22 @@ | |
| static_folder="static" | ||
| ) | ||
|
|
||
| app.config["UPLOAD_FOLDER"] = "archives" | ||
| app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 * 1024 # 2 GB limit | ||
|
|
||
| reader = Reader() | ||
|
|
||
| # these functions only fire when the route is navigated to | ||
|
|
||
| @app.route("/channel/<name>/") | ||
| def channel_name(name): | ||
| messages = flask._app_ctx_stack.channels[name] | ||
| channels = list(flask._app_ctx_stack.channels.keys()) | ||
| groups = list(flask._app_ctx_stack.groups.keys()) | ||
| dm_users = list(flask._app_ctx_stack.dm_users) | ||
| mpim_users = list(flask._app_ctx_stack.mpim_users) | ||
| if not hasattr(reader, 'channels'): | ||
| return flask.render_template("404.html") | ||
| messages = reader.channels[name] | ||
| channels = list(reader.channels.keys()) | ||
| groups = list(reader.groups.keys()) | ||
| dm_users = list(reader.dm_users) | ||
| mpim_users = list(reader.mpim_users) | ||
|
|
||
| return flask.render_template("viewer.html", messages=messages, | ||
| name=name.format(name=name), | ||
|
|
@@ -26,11 +38,13 @@ def channel_name(name): | |
|
|
||
| @app.route("/group/<name>/") | ||
| def group_name(name): | ||
| messages = flask._app_ctx_stack.groups[name] | ||
| channels = list(flask._app_ctx_stack.channels.keys()) | ||
| groups = list(flask._app_ctx_stack.groups.keys()) | ||
| dm_users = list(flask._app_ctx_stack.dm_users) | ||
| mpim_users = list(flask._app_ctx_stack.mpim_users) | ||
| if not hasattr(reader, 'channels'): | ||
| return flask.render_template("404.html") | ||
| messages = reader.groups[name] | ||
| channels = list(reader.channels.keys()) | ||
| groups = list(reader.groups.keys()) | ||
| dm_users = list(reader.dm_users) | ||
| mpim_users = list(reader.mpim_users) | ||
|
|
||
| return flask.render_template("viewer.html", messages=messages, | ||
| name=name.format(name=name), | ||
|
|
@@ -42,11 +56,13 @@ def group_name(name): | |
|
|
||
| @app.route("/dm/<id>/") | ||
| def dm_id(id): | ||
| messages = flask._app_ctx_stack.dms[id] | ||
| channels = list(flask._app_ctx_stack.channels.keys()) | ||
| groups = list(flask._app_ctx_stack.groups.keys()) | ||
| dm_users = list(flask._app_ctx_stack.dm_users) | ||
| mpim_users = list(flask._app_ctx_stack.mpim_users) | ||
| if not hasattr(reader, 'channels'): | ||
| return flask.render_template("404.html") | ||
| messages = reader.dms[id] | ||
| channels = list(reader.channels.keys()) | ||
| groups = list(reader.groups.keys()) | ||
| dm_users = list(reader.dm_users) | ||
| mpim_users = list(reader.mpim_users) | ||
|
|
||
| return flask.render_template("viewer.html", messages=messages, | ||
| id=id.format(id=id), | ||
|
|
@@ -58,11 +74,13 @@ def dm_id(id): | |
|
|
||
| @app.route("/mpim/<name>/") | ||
| def mpim_name(name): | ||
| messages = flask._app_ctx_stack.mpims[name] | ||
| channels = list(flask._app_ctx_stack.channels.keys()) | ||
| groups = list(flask._app_ctx_stack.groups.keys()) | ||
| dm_users = list(flask._app_ctx_stack.dm_users) | ||
| mpim_users = list(flask._app_ctx_stack.mpim_users) | ||
| if reader.channels is None: | ||
| return flask.render_template("404.html") | ||
| messages = reader.mpims[name] | ||
| channels = list(reader.channels.keys()) | ||
| groups = list(reader.groups.keys()) | ||
| dm_users = list(reader.dm_users) | ||
| mpim_users = list(reader.mpim_users) | ||
|
|
||
| return flask.render_template("viewer.html", messages=messages, | ||
| name=name.format(name=name), | ||
|
|
@@ -72,9 +90,46 @@ def mpim_name(name): | |
| mpim_users=mpim_users) | ||
|
|
||
|
|
||
| @app.route("/") | ||
| ALLOWED_EXTENSIONS = set(["zip"]) | ||
|
|
||
| def allowed_file(filename): | ||
| return "." in filename and \ | ||
| filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two newlines between all module-level functions |
||
| @app.route("/", methods=["GET", "POST"]) | ||
| def upload(): | ||
| print('upload') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the request I have general for these print statements is, can you just clean them up a bit? |
||
| if request.method == "POST": | ||
| # check if the post request has the file part | ||
| print(request.files) | ||
| if "archive_file" not in request.files: | ||
| print('archive_file not in request.file') | ||
| flash("No file part") | ||
| return redirect(request.url) | ||
| file = request.files["archive_file"] | ||
| # if user does not select file, browser also | ||
| # submit a empty part without filename | ||
| if file.filename == "": | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| print("file name is empty") | ||
| flash("No selected file") | ||
| return redirect(request.url) | ||
| if file and allowed_file(file.filename): | ||
| print(os.path.abspath(file.filename)) | ||
| filename = os.path.abspath(file.filename) # change this to full file path for where it gets uploaded to | ||
| archive_path = extract_archive(filename) | ||
| reader.set_path(archive_path) | ||
| reader.get_all_messages() | ||
| return redirect(url_for("index")) | ||
|
|
||
| reader.reset() | ||
| return flask.render_template("upload.html", reader=reader) | ||
|
|
||
|
|
||
| @app.route("/channel/") | ||
| def index(): | ||
| channels = list(flask._app_ctx_stack.channels.keys()) | ||
| if not hasattr(reader, 'channels'): | ||
| return flask.render_template("404.html") | ||
| channels = list(reader.channels.keys()) | ||
| if "general" in channels: | ||
| return channel_name("general") | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,39 +10,72 @@ class Reader(object): | |
| Reader object will read all of the archives' data from the json files | ||
| """ | ||
|
|
||
| def __init__(self, PATH): | ||
| self._PATH = PATH | ||
| # TODO: Make sure this works | ||
| with io.open(os.path.join(self._PATH, "users.json"), encoding="utf8") as f: | ||
| self.__USER_DATA = {u["id"]: u for u in json.load(f)} | ||
| def __init__(self, PATH=''): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| if not PATH == '': | ||
| self._PATH = PATH | ||
| with io.open(os.path.join(self._PATH, "users.json"), encoding="utf8") as f: | ||
| self.__USER_DATA = {u["id"]: u for u in json.load(f)} | ||
|
|
||
|
|
||
| ################## | ||
| # Public Methods # | ||
| ################## | ||
|
|
||
| def reset(self): | ||
| self.channels = None | ||
| self.groups = None | ||
| self.dms = None | ||
| self.dm_users = None | ||
| self.mpims = None | ||
| self.mpim_users = None | ||
| self._PATH = '' | ||
| self.__USER_DATA = None | ||
|
|
||
| def get_all_messages(self): | ||
| """ | ||
| This method is used to call all of the compile methods at once. | ||
| """ | ||
| self.compile_channels() | ||
| self.compile_groups() | ||
| self.compile_dm_messages() | ||
| self.compile_dm_users() | ||
| self.compile_mpim_messages() | ||
| self.compile_mpim_users() | ||
|
|
||
| def set_path(self, path): | ||
| """ | ||
| Sets the _PATH and readers the users json file to get the user data | ||
| """ | ||
| self._PATH = path | ||
| with io.open(os.path.join(self._PATH, "users.json"), encoding="utf8") as f: | ||
| self.__USER_DATA = {u["id"]: u for u in json.load(f)} | ||
|
|
||
| def compile_channels(self): | ||
|
|
||
| print("getting channels...") | ||
| channel_data = self._read_from_json("channels.json") | ||
| channel_names = [c["name"] for c in channel_data.values()] | ||
|
|
||
| return self._create_messages(channel_names, channel_data) | ||
| self.channels = self._create_messages(channel_names, channel_data) | ||
|
|
||
| def compile_groups(self): | ||
|
|
||
| print("getting groups...") | ||
| group_data = self._read_from_json("groups.json") | ||
| group_names = [c["name"] for c in group_data.values()] | ||
|
|
||
| return self._create_messages(group_names, group_data) | ||
| self.groups = self._create_messages(group_names, group_data) | ||
|
|
||
| def compile_dm_messages(self): | ||
|
|
||
| print("getting dm messages...") | ||
| # Gets list of dm objects with dm ID and array of members ids | ||
| dm_data = self._read_from_json("dms.json") | ||
| dm_ids = [c["id"] for c in dm_data.values()] | ||
|
|
||
| # True is passed here to let the create messages function know that | ||
| # it is dm data being passed to it | ||
| return self._create_messages(dm_ids, dm_data, True) | ||
| self.dms = self._create_messages(dm_ids, dm_data, True) | ||
|
|
||
| def compile_dm_users(self): | ||
| """ | ||
|
|
@@ -58,6 +91,8 @@ def compile_dm_users(self): | |
|
|
||
| """ | ||
|
|
||
| print("getting dm users...") | ||
|
|
||
| dm_data = self._read_from_json("dms.json") | ||
| dms = dm_data.values() | ||
| all_dms_users = [] | ||
|
|
@@ -68,15 +103,17 @@ def compile_dm_users(self): | |
| dm_members = {"id": dm["id"], "users": [self.__USER_DATA[m] for m in dm["members"]]} | ||
| all_dms_users.append(dm_members) | ||
|
|
||
| return all_dms_users | ||
| self.dm_users = all_dms_users | ||
|
|
||
|
|
||
| def compile_mpim_messages(self): | ||
|
|
||
| print("getting mpim messages...") | ||
|
|
||
| mpim_data = self._read_from_json("mpims.json") | ||
| mpim_names = [c["name"] for c in mpim_data.values()] | ||
|
|
||
| return self._create_messages(mpim_names, mpim_data) | ||
| self.mpims = self._create_messages(mpim_names, mpim_data) | ||
|
|
||
| def compile_mpim_users(self): | ||
| """ | ||
|
|
@@ -92,6 +129,8 @@ def compile_mpim_users(self): | |
|
|
||
| """ | ||
|
|
||
| print("getting mpim users...") | ||
|
|
||
| mpim_data = self._read_from_json("mpims.json") | ||
| mpims = [c for c in mpim_data.values()] | ||
| all_mpim_users = [] | ||
|
|
@@ -100,7 +139,7 @@ def compile_mpim_users(self): | |
| mpim_members = {"name": mpim["name"], "users": [self.__USER_DATA[m] for m in mpim["members"]]} | ||
| all_mpim_users.append(mpim_members) | ||
|
|
||
| return all_mpim_users | ||
| self.mpim_users = all_mpim_users | ||
|
|
||
|
|
||
| ################### | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constants like this should be at the top of the file