aboutsummaryrefslogtreecommitdiff
path: root/cgi/img.py
diff options
context:
space:
mode:
Diffstat (limited to 'cgi/img.py')
-rw-r--r--cgi/img.py40
1 files changed, 19 insertions, 21 deletions
diff --git a/cgi/img.py b/cgi/img.py
index 1c57e37..7759b23 100644
--- a/cgi/img.py
+++ b/cgi/img.py
@@ -5,7 +5,7 @@ import math
import os
import subprocess
import logging
-from StringIO import StringIO
+from io import BytesIO
from settings import Settings
from database import *
@@ -35,7 +35,7 @@ def processImage(post, data, t, originalname, spoiler=False):
# check the size is fine
if size > int(board["maxsize"])*1024:
- raise UserError, _("File too big. The maximum file size is: %s") % board['maxsize']
+ raise UserError(_("File too big. The maximum file size is: %s") % board['maxsize'])
# check if file is supported
for filetype in board['filetypes']:
@@ -44,12 +44,12 @@ def processImage(post, data, t, originalname, spoiler=False):
break
if not used_filetype:
- raise UserError, _("File type not supported.")
+ raise UserError(_("File type not supported."))
# check if file is already posted
is_duplicate = checkFileDuplicate(data)
if checkFileDuplicate(data)[0]:
- raise UserError, _("This image has already been posted %s.") % ('<a href="' + Settings.BOARDS_URL + board['dir'] + '/res/' + str(is_duplicate[1]) + '.html#' + str(is_duplicate[2]) + '">' + _("here") + '</a>')
+ raise UserError(_("This image has already been posted %s.") % ('<a href="' + Settings.BOARDS_URL + board['dir'] + '/res/' + str(is_duplicate[1]) + '.html#' + str(is_duplicate[2]) + '">' + _("here") + '</a>'))
# prepare file names
if used_filetype['preserve_name'] == '1':
@@ -142,17 +142,17 @@ def processImage(post, data, t, originalname, spoiler=False):
# generate thumbnails
call_wrap(args)
- except subprocess.CalledProcessError, e:
+ except subprocess.CalledProcessError as e:
os.remove(file_path)
logging.error("Thumbnail creation failure: " + e.output)
- raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')'
+ raise UserError(_("Thumbnail creation failure.") + ' ('+str(e.returncode)+')')
# check if thumbnail was truly created
try:
open(file_thumb_path)
except:
os.remove(file_path)
- raise UserError, _("Thumbnail creation failure.")
+ raise UserError(_("Thumbnail creation failure."))
# create extra thumbnails (catalog/mobile)
subprocess.call([Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread",
@@ -182,7 +182,7 @@ def processImage(post, data, t, originalname, spoiler=False):
post["message"] += extraInfo(content_type, file_name, file_path)
# file md5
- post["file_hex"] = getMD5(data)
+ post["file_hex"] = getMD5b(data)
return post
@@ -233,7 +233,6 @@ def extraInfo(mime, file_name, file_path):
def getImageInfo(data):
- data = str(data)
size = len(data)
height = -1
width = -1
@@ -251,7 +250,7 @@ def getImageInfo(data):
# See PNG 2. Edition spec (http://www.w3.org/TR/PNG/)
# Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
# and finally the 4-byte width, height
- elif ((size >= 24) and data.startswith("\211PNG\r\n\032\n")
+ elif ((size >= 24) and data.startswith(b"\211PNG\r\n\032\n")
and (data[12:16] == "IHDR")):
content_type = "image/png"
w, h = struct.unpack(">LL", data[16:24])
@@ -259,7 +258,7 @@ def getImageInfo(data):
height = int(h)
# Maybe this is for an older PNG version.
- elif (size >= 16) and data.startswith("\211PNG\r\n\032\n"):
+ elif (size >= 16) and data.startswith(b"\211PNG\r\n\032\n"):
# Check to see if we have the right content type
content_type = "image/png"
w, h = struct.unpack(">LL", data[8:16])
@@ -267,9 +266,9 @@ def getImageInfo(data):
height = int(h)
# handle JPEGs
- elif (size >= 2) and data.startswith("\377\330"):
+ elif (size >= 2) and data.startswith(b"\377\330"):
content_type = "image/jpeg"
- jpeg = StringIO(data)
+ jpeg = BytesIO(data)
jpeg.read(2)
b = jpeg.read(1)
try:
@@ -288,9 +287,9 @@ def getImageInfo(data):
width = int(w)
height = int(h)
except struct.error:
- pass
+ raise
except ValueError:
- pass
+ raise
# handle WebP
if data[:4] == b'RIFF' and data[8:12] == b'WEBP':
@@ -309,7 +308,7 @@ def getImageInfo(data):
content_type = "image/webp"
# handle WebM
- elif (size >= 4) and data.startswith("\x1A\x45\xDF\xA3"):
+ elif (size >= 4) and data.startswith(b"\x1A\x45\xDF\xA3"):
content_type = "video/webm"
info = ffprobe(data)
@@ -333,7 +332,7 @@ def getImageInfo(data):
content_type = "audio/mod"
# handle XM
- elif (size >= 64) and data.startswith("Extended Module:"):
+ elif (size >= 64) and data.startswith(b"Extended Module:"):
content_type = "audio/xm"
# handle S3M
@@ -418,11 +417,10 @@ def checkFileDuplicate(data):
"""
board = Settings._.BOARD
- file_hex = getMD5(data)
- post = FetchOne("SELECT `id`, `parentid` FROM `posts` WHERE `file_hex` = '%s' AND `boardid` = %s AND IS_DELETED = 0 LIMIT 1" % (
- file_hex, board['id']))
+ file_hex = getMD5b(data)
+ post = FetchOne("SELECT `id`, `parentid` FROM `posts` WHERE `file_hex` = %s AND `boardid` = %s AND IS_DELETED = 0 LIMIT 1", (file_hex, board['id']))
if post:
- if int(post["parentid"]) != 0:
+ if post["parentid"]:
return True, post["parentid"], post["id"]
else:
return True, post["id"], post["id"]