Use pathlib

This commit is contained in:
Mia Herkt 2020-12-29 12:40:11 +01:00
parent b5ce94edeb
commit cd083a7f83
No known key found for this signature in database
GPG key ID: 72E154B8622EC191

View file

@ -30,6 +30,7 @@ import os, sys
import requests import requests
from short_url import UrlEncoder from short_url import UrlEncoder
from validators import url as url_valid from validators import url as url_valid
from pathlib import Path
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
app.config.update( app.config.update(
@ -81,8 +82,8 @@ except:
Please install python-magic.""") Please install python-magic.""")
sys.exit(1) sys.exit(1)
if not os.path.exists(app.config["FHOST_STORAGE_PATH"]): storage = Path(app.config["FHOST_STORAGE_PATH"])
os.mkdir(app.config["FHOST_STORAGE_PATH"]) storage.mkdir(parents=True, exist_ok=True)
db = SQLAlchemy(app) db = SQLAlchemy(app)
migrate = Migrate(app, db) migrate = Migrate(app, db)
@ -129,9 +130,6 @@ class File(db.Model):
else: else:
return url_for("get", path=n, _external=True) + "\n" return url_for("get", path=n, _external=True) + "\n"
def getpath(fn):
return os.path.join(app.config["FHOST_STORAGE_PATH"], fn)
def fhost_url(scheme=None): def fhost_url(scheme=None):
if not scheme: if not scheme:
return url_for(".fhost", _external=True).rstrip("/") return url_for(".fhost", _external=True).rstrip("/")
@ -182,18 +180,18 @@ def store_file(f, addr):
if existing.removed: if existing.removed:
abort(451) abort(451)
epath = getpath(existing.sha256) epath = storage / existing.sha256
if not os.path.exists(epath): if not epath.is_file():
with open(epath, "wb") as of: f.save(epath)
of.write(data)
if existing.nsfw_score == None: if existing.nsfw_score == None:
if app.config["NSFW_DETECT"]: if app.config["NSFW_DETECT"]:
existing.nsfw_score = nsfw.detect(epath) existing.nsfw_score = nsfw.detect(epath)
os.utime(epath, None) epath.touch()
existing.addr = addr existing.addr = addr
db.session.commit() db.session.commit()
return existing.geturl() return existing.geturl()
@ -226,10 +224,8 @@ def store_file(f, addr):
if not ext: if not ext:
ext = ".bin" ext = ".bin"
spath = getpath(digest) spath = storage / digest
f.save(spath)
with open(spath, "wb") as of:
of.write(data)
if app.config["NSFW_DETECT"]: if app.config["NSFW_DETECT"]:
nsfw_score = nsfw.detect(spath) nsfw_score = nsfw.detect(spath)
@ -281,17 +277,15 @@ def get(path):
if f.removed: if f.removed:
abort(451) abort(451)
fpath = getpath(f.sha256) fpath = storage / f.sha256
if not os.path.exists(fpath): if not fpath.is_file():
abort(404) abort(404)
fsize = os.path.getsize(fpath)
if app.config["FHOST_USE_X_ACCEL_REDIRECT"]: if app.config["FHOST_USE_X_ACCEL_REDIRECT"]:
response = make_response() response = make_response()
response.headers["Content-Type"] = f.mime response.headers["Content-Type"] = f.mime
response.headers["Content-Length"] = fsize response.headers["Content-Length"] = fpath.stat().st_size
response.headers["X-Accel-Redirect"] = "/" + fpath response.headers["X-Accel-Redirect"] = "/" + fpath
return response return response
else: else: