From: Michael Tremer Date: Mon, 22 Oct 2018 13:18:29 +0000 (+0100) Subject: nopaste: Drop guessing mime types X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bed82f3e80196c04c8db468256a54b0d73a39d93;p=ipfire.org.git nopaste: Drop guessing mime types This doesn't work and we can use what the browser sends to us Signed-off-by: Michael Tremer --- diff --git a/requirements.txt b/requirements.txt index a755e131..30d61b2c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ cffi==1.11.5 cryptography==2.3.1 ecdsa==0.13 feedparser==5.2.1 -file-magic==0.4.0 html5lib==1.0.1 idna==2.7 iso3166==0.9 diff --git a/src/backend/nopaste.py b/src/backend/nopaste.py index 71d0d77e..bab855ba 100644 --- a/src/backend/nopaste.py +++ b/src/backend/nopaste.py @@ -1,23 +1,15 @@ #!/usr/bin/python -import magic - from .misc import Object class Nopaste(Object): - def create(self, subject, content, type="text", expires=None, account=None, address=None): + def create(self, subject, content, mimetype="text", expires=None, account=None, address=None): self._cleanup_database() uid = None if account: uid = account.uid - if type == "text": - mimetype = "text/plain" - - elif type == "file": - mimetype = self._guess_mimetype(content) - # http://blog.00null.net/easily-generating-random-strings-in-postgresql/ res = self.db.get("INSERT INTO nopaste(uuid, subject, content, time_expires, address, \ uid, mimetype, size) VALUES(random_slug(), %s, %s, \ @@ -28,18 +20,6 @@ class Nopaste(Object): if res: return res.uuid - def _guess_mimetype(self, buf): - ms = magic.open(magic.NONE) - ms.load() - - # Return the mime type - ms.setflags(magic.MAGIC_MIME_TYPE) - - try: - return ms.buffer(buf) - finally: - ms.close() - def get(self, uuid): res = self.db.get("SELECT uuid, subject, time_created, time_expires, address, uid, \ mimetype, views, size FROM nopaste WHERE uuid = %s AND (CASE WHEN time_expires \ diff --git a/src/web/nopaste.py b/src/web/nopaste.py index 125c4630..34a5d488 100644 --- a/src/web/nopaste.py +++ b/src/web/nopaste.py @@ -20,17 +20,17 @@ class CreateHandler(base.BaseHandler): if not mode in self.MODES: raise tornado.web.HTTPError(400) + mimetype = "text/plain" + if mode == "paste": subject = self.get_argument("subject", None) content = self.get_argument("content") - type = "text" elif mode == "upload": - type = "file" - for f in self.request.files.get("file"): - subject = f.filename - content = f.body + subject = f.filename + content = f.body + mimetype = f.content_type break # Check maximum size @@ -44,9 +44,8 @@ class CreateHandler(base.BaseHandler): except (TypeError, ValueError): expires = None - uid = self.backend.nopaste.create(subject, content, type=type, - expires=expires, account=self.current_user, - address=self.get_remote_ip()) + uid = self.backend.nopaste.create(subject, content, mimetype=mimetype, + expires=expires, account=self.current_user, address=self.get_remote_ip()) if uid: return self.redirect("/view/%s" % uid)