diff options
Diffstat (limited to 'cgi/img.py')
-rw-r--r-- | cgi/img.py | 64 |
1 files changed, 34 insertions, 30 deletions
@@ -31,6 +31,7 @@ def processImage(post, data, t, originalname, spoiler=False): # get image information content_type, width, height, size, extra = getImageInfo(data) + #raise Exception(repr(getImageInfo(data))) logging.info("{} {} {}".format(content_type, width, height)) # check the size is fine @@ -52,7 +53,7 @@ def processImage(post, data, t, originalname, spoiler=False): 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': + if used_filetype['preserve_name']: file_base = os.path.splitext(originalname)[0] # use original filename else: file_base = '%d' % int(t * 1000) # generate timestamp name @@ -90,11 +91,10 @@ def processImage(post, data, t, originalname, spoiler=False): if not used_filetype['image']: # make thumbnail logging.debug("Generating thumbnail") - file_thumb_width, file_thumb_height = getThumbDimensions( - width, height, maxsize) + file_thumb_width, file_thumb_height = getThumbDimensions(width, height, maxsize) try: - if used_filetype['ffmpeg_thumb'] == '1': + if used_filetype['ffmpeg_thumb']: # use ffmpeg to make thumbnail if used_filetype['mime'][:5] == 'video': # Create preview for video AND spoiler it if necessary @@ -144,7 +144,7 @@ def processImage(post, data, t, originalname, spoiler=False): call_wrap(args) except subprocess.CalledProcessError as e: os.remove(file_path) - logging.error("Thumbnail creation failure: " + e.output) + logging.error("Thumbnail creation failure: " + str(e.output)) raise UserError(_("Thumbnail creation failure.") + ' ('+str(e.returncode)+')') # check if thumbnail was truly created @@ -166,14 +166,14 @@ def processImage(post, data, t, originalname, spoiler=False): post["thumb_height"] = file_thumb_height else: # Don't thumbnail and use mime image - if board["board_type"] == '0': + if board["board_type"] == 0: post["thumb"] = used_filetype['image'] - post["thumb_width"] = '120' - post["thumb_height"] = '120' + post["thumb_width"] = 120 + post["thumb_height"] = 120 else: post["thumb"] = used_filetype['image'].split(".")[0] + '_small.png' - post["thumb_width"] = '90' - post["thumb_height"] = '90' + post["thumb_width"] = 90 + post["thumb_height"] = 90 # calculate size (bytes) post["file_size"] = len(data) @@ -202,22 +202,22 @@ def extraInfo(mime, file_name, file_path): else: stream = info['streams'][0] - extra['codec'] = stream.get('codec_name', '').encode('utf-8') + extra['codec'] = stream.get('codec_name', '') format = info['format'] if 'bit_rate' in format: extra['codec'] += ' ~%d kbps' % int(int(format['bit_rate']) / 1000) if 'tags' in format: extra['title'] = format['tags'].get( - 'TITLE', format['tags'].get('title', '')).encode('utf-8') + 'TITLE', format['tags'].get('title', '')) extra['artist'] = format['tags'].get( - 'ARTIST', format['tags'].get('artist', '')).encode('utf-8') + 'ARTIST', format['tags'].get('artist', '')) if extra['title'] or extra['artist']: credit_str = ' - '.join((extra['artist'], extra['title'])) + ' ' if 'tags' in stream: - extra['title'] = stream['tags'].get('TITLE', '').encode('utf-8') - extra['artist'] = stream['tags'].get('ARTIST', '').encode('utf-8') + extra['title'] = stream['tags'].get('TITLE', '') + extra['artist'] = stream['tags'].get('ARTIST', '') if extra['title'] or extra['artist']: credit_str = ' - '.join((extra['artist'], extra['title'])) + ' ' @@ -234,13 +234,13 @@ def extraInfo(mime, file_name, file_path): def getImageInfo(data): size = len(data) - height = -1 - width = -1 + height = 0 + width = 0 extra = {} content_type = "" # handle GIFs - if (size >= 10) and data[:6] in ("GIF87a", "GIF89a"): + if (size >= 10) and data[:6] in (b"GIF87a", b"GIF89a"): # Check to see if content_type is correct content_type = "image/gif" w, h = struct.unpack("<HH", data[6:10]) @@ -251,7 +251,7 @@ def getImageInfo(data): # Bytes 0-7 are below, 4-byte chunk length, then 'IHDR' # and finally the 4-byte width, height elif ((size >= 24) and data.startswith(b"\211PNG\r\n\032\n") - and (data[12:16] == "IHDR")): + and (data[12:16] == b"IHDR")): content_type = "image/png" w, h = struct.unpack(">LL", data[16:24]) width = int(w) @@ -313,22 +313,22 @@ def getImageInfo(data): info = ffprobe(data) # handle mp4 - elif (size >= 8) and data[4:12] in ["ftypmp42", "ftypisom"]: + elif (size >= 8) and data[4:12] in [b"ftypmp42", b"ftypisom"]: content_type = "video/mp4" # handle ogg formats (vorbis/opus) - elif (size >= 64) and data[:4] == "OggS": - if data[28:35] == "\x01vorbis": + elif (size >= 64) and data[:4] == b"OggS": + if data[28:35] == b"\x01vorbis": content_type = "audio/ogg" - elif data[28:36] == "OpusHead": + elif data[28:36] == b"OpusHead": content_type = "audio/opus" # handle MP3 - elif (size >= 64) and (data[:3] == "ID3" or data[:3] == "\xFF\xFB"): + elif (size >= 64) and (data[:3] == b"ID3" or data[:3] == b"\xFF\xFB"): content_type = "audio/mpeg" # handle MOD - elif (size >= 64) and data[1080:1084] == "M.K.": + elif (size >= 64) and data[1080:1084] == b"M.K.": content_type = "audio/mod" # handle XM @@ -336,23 +336,23 @@ def getImageInfo(data): content_type = "audio/xm" # handle S3M - elif (size >= 64) and data[25:32] == "\x00\x00\x00\x1A\x10\x00\x00": + elif (size >= 64) and data[25:32] == b"\x00\x00\x00\x1A\x10\x00\x00": content_type = "audio/s3m" # handle PDF - elif (size >= 4) and data[:7] == "%PDF-1.": + elif (size >= 4) and data[:7] == b"%PDF-1.": content_type = "application/pdf" # handle Shockwave Flash - elif (size >= 3) and data[:3] in ["CWS", "FWS"]: + elif (size >= 3) and data[:3] in [b"CWS", b"FWS"]: content_type = "application/x-shockwave-flash" # handle torrent - elif (size >= 11) and data[:11] == "d8:announce": + elif (size >= 11) and data[:11] == b"d8:announce": content_type = "application/x-bittorrent" # handle PDF - elif (size >= 2) and data[:2] == "PK": + elif (size >= 2) and data[:2] == b"PK": content_type = "application/epub+zip" if content_type.startswith("video"): @@ -386,6 +386,7 @@ def ffprobe_f(filename): def call_wrap(args): + logging.info("Calling: " + repr(args)) subprocess.check_output(args, stderr=subprocess.STDOUT) @@ -394,6 +395,9 @@ def getThumbDimensions(width, height, maxsize): Calculate dimensions to use for a thumbnail with maximum width/height of <maxsize>, keeping aspect ratio """ + if not width or not height: + return 0, 0 + wratio = (float(maxsize) / float(width)) hratio = (float(maxsize) / float(height)) |