]> git.ipfire.org Git - ipfire.org.git/commitdiff
nopaste: Drop guessing mime types
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Oct 2018 13:18:29 +0000 (14:18 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Oct 2018 13:18:29 +0000 (14:18 +0100)
This doesn't work and we can use what the browser sends to us

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
requirements.txt
src/backend/nopaste.py
src/web/nopaste.py

index a755e131068e1db487400128328e48bb7b779fd8..30d61b2c3c64079648e44eacdfef3fbb4014afcd 100644 (file)
@@ -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
index 71d0d77ea00bbaffc28a8a1f8447afe081700bab..bab855bab6ffac114669c3fbdbdb780d2107a2ff 100644 (file)
@@ -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 \
index 125c46306f40e15d2fea379c1d54e8f04a2b04f3..34a5d4887378dc8e080ba060f932ba94a7c23c02 100644 (file)
@@ -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)