Browse Source

Initial commit

Samuel Čavoj 3 years ago
commit
868a467ef4
6 changed files with 77 additions and 0 deletions
  1. 5 0
      .gitignore
  2. 30 0
      fput/__init__.py
  3. 27 0
      fput/acl.py
  4. 4 0
      run_dev.py
  5. 10 0
      uwsgi_config.ini
  6. 1 0
      wsgi.py

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+/venv/
+.py[ocd]
+__pycache__/
+acl.ini
+.vscode/

+ 30 - 0
fput/__init__.py

@@ -0,0 +1,30 @@
+from flask import Flask, abort, request
+import os
+from fput.acl import Acl
+
+app = Flask(__name__)
+acl = Acl("acl.ini")
+
+
+@app.route("/", methods=["GET", "POST"])
+def index():
+    if request.method == "POST":
+        if "file" not in request.files:
+            abort(400)
+        if "path" not in request.form:
+            abort(400)
+        if "key" not in request.form:
+            abort(401)
+
+        key = request.form.get("key")
+        path = request.form.get("path")
+        file = request.files.get("file")
+
+        if not acl.get(key).check_path(path):
+            abort(403)
+
+        os.makedirs(os.path.join(acl.datadir, os.path.dirname(path)), exist_ok=True)
+        file.save(os.path.join(acl.datadir, path))
+        return "ok"
+    else:
+        return "This is fput!"

+ 27 - 0
fput/acl.py

@@ -0,0 +1,27 @@
+import configparser
+import os.path
+
+
+class Grant:
+    def __init__(self, path, **kwargs):
+        self.path = os.path.normpath(path)
+
+    def check_path(self, path):
+        return os.path.normpath(path).startswith(self.path)
+
+
+class NoGrant:
+    @staticmethod
+    def check_path(path):
+        return False
+
+
+class Acl:
+    def __init__(self, acl_filename):
+        cp = configparser.ConfigParser()
+        cp.read(acl_filename)
+        self.grants = {k: Grant(**v) for k, v in cp.items() if k != "DEFAULT"}
+        self.datadir = cp["DEFAULT"]["datadir"]
+
+    def get(self, key):
+        return self.grants.get(key, NoGrant)

+ 4 - 0
run_dev.py

@@ -0,0 +1,4 @@
+#!/bin/python
+from fput import app
+
+app.run(debug=True)

+ 10 - 0
uwsgi_config.ini

@@ -0,0 +1,10 @@
+[uwsgi]
+uid = http
+gid = http
+socket = /run/http/fput.sock
+workers = 4
+master = true
+chmod-socket = 664
+module = wsgi:app
+req-logger = syslog
+

+ 1 - 0
wsgi.py

@@ -0,0 +1 @@
+from fput import app