aboutsummaryrefslogtreecommitdiff
path: root/cgi/img.py
blob: 3561672f5718e3031fbb71981415c2c9cc33f119 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# coding=utf-8
import struct
import math
#import random
import os
import subprocess
from StringIO import StringIO

from settings import Settings
from database import *
from framework import *

try: # Windows needs stdio set for binary mode.
  import msvcrt
  msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
  msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
  pass

def processImage(post, data, t, originalname, spoiler=False):
  """
  Take all post data from <post>, process uploaded file in <data>, and calculate
  file names using datetime <t>
  Returns updated <post> with file and thumb values
  """
  board = Settings._.BOARD

  used_filetype = None
  
  # get image information
  content_type, width, height, size, extra = getImageInfo(data)
  
  # check the size is fine
  if size > int(board["maxsize"])*1024:
    raise UserError, _("File too big. The maximum file size is: %s") % board['maxsize']
  
  # check if file is supported
  for filetype in board['filetypes']:
    if content_type == filetype['mime']:
      used_filetype = filetype
      break
    
  if not used_filetype:
    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>')
  
  # prepare file names
  if used_filetype['preserve_name'] == '1':
    file_base = os.path.splitext(originalname)[0] # use original filename
  else:
    file_base = '%d' % int(t * 1000) # generate timestamp name
  file_name = file_base + "." + used_filetype['ext']
  file_thumb_name = file_base + "s.jpg"
  
  # prepare paths
  file_path = Settings.IMAGES_DIR + board["dir"] + "/src/" + file_name
  file_thumb_path = Settings.IMAGES_DIR + board["dir"] + "/thumb/" + file_thumb_name
  file_mobile_path = Settings.IMAGES_DIR + board["dir"] + "/mobile/" + file_thumb_name
  file_cat_path = Settings.IMAGES_DIR + board["dir"] + "/cat/" + file_thumb_name
  
  # remove EXIF data if necessary for privacy
  if content_type == 'image/jpeg':
    data = removeExifData(data)
  
  # write file
  f = open(file_path, "wb")
  try:
    f.write(data)
  finally:
    f.close()
  
  # set maximum dimensions
  maxsize = int(board['thumb_px'])
  
  post["file"] = file_name
  post["image_width"] = width
  post["image_height"] = height
  
  # Do we need to thumbnail it?
  if not used_filetype['image']:
    # make thumbnail
    file_thumb_width, file_thumb_height = getThumbDimensions(width, height, maxsize)
    
    if used_filetype['ffmpeg_thumb'] == '1':
      # use ffmpeg to make thumbnail
      logTime("Generating thumbnail")
      
      if used_filetype['mime'][:5] == 'video':
        retcode = subprocess.call([
          Settings.FFMPEG_PATH, '-strict', '-2', '-ss', '0', '-i', file_path,
          '-v', 'quiet', '-an', '-vframes', '1', '-f', 'mjpeg', '-vf', 'scale=%d:%d' % (file_thumb_width, file_thumb_height),
          '-threads', '1', file_thumb_path])
        if spoiler:
          args = [Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", "1", "-background", "white", "-flatten", "-resize",  "%dx%d" % (file_thumb_width, file_thumb_height), "-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ((file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler", "-quality", str(Settings.THUMB_QUALITY), file_thumb_path]
          retcode = subprocess.call(args)
      elif used_filetype['mime'][:5] == 'audio':
        # we do an exception and use png for audio waveform thumbnails since they
        # 1. are smaller 2. allow for transparency
        file_thumb_name = file_thumb_name[:-3] + "png"
        file_thumb_path = file_thumb_path[:-3] + "png"
        file_mobile_path = file_mobile_path[:-3] + "png"
        file_cat_path = file_cat_path[:-3] + "png"
        
        if int(board['thumb_px']) > 149:
          file_thumb_width = board['thumb_px']
          file_thumb_height = float(int(board['thumb_px'])/2)
        else:
          file_thumb_width = 150
          file_thumb_height = 75

        retcode = subprocess.call([
          Settings.FFMPEG_PATH, '-t', '300', '-i', file_path,
          '-filter_complex', 'showwavespic=s=%dx%d:split_channels=1' % (int(file_thumb_width), int(file_thumb_height)),
          '-frames:v', '1', '-threads', '1', file_thumb_path])
#       elif used_filetype['mime'] == 'application/x-shockwave-flash' or used_filetype['mime'] == 'mime/x-shockwave-flash':
#         retcode = subprocess.call([
#           './ffmpeg', '-i', file_path, '-vcodec', 'mjpeg', '-vframes', '1', '-an', '-f', 'rawvideo',
#           '-vf', 'scale=%d:%d' % (file_thumb_width, file_thumb_height), '-threads', '1', file_thumb_path])

      if retcode != 0:
        os.remove(file_path)
        raise UserError, _("Thumbnail creation failure.") + ' ('+str(retcode)+')'
    else:
      # use imagemagick to make thumbnail
      args = [Settings.CONVERT_PATH, file_path, "-limit", "thread", "1", "-background", "white", "-flatten", "-resize",  "%dx%d" % (file_thumb_width, file_thumb_height)]
      if spoiler:
        args += ["-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ((file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler"]
      args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path]

      # generate thumbnails
      logTime("Generating thumbnail")
      retcode = subprocess.call(args)
      if retcode != 0:
        os.remove(file_path)
        raise UserError, _("Thumbnail creation failure.") + ' ('+str(retcode)+')'

    # check if thumbnail was truly created
    try:
      open(file_thumb_path)
    except:
      os.remove(file_path)
      raise UserError, _("Thumbnail creation failure.")

    # create extra thumbnails (catalog/mobile)
    subprocess.call([Settings.CONVERT_PATH, file_thumb_path, "-limit" , "thread", "1", "-resize",  "100x100", "-quality", "75", file_mobile_path])
    if not post["parentid"]:
      subprocess.call([Settings.CONVERT_PATH, file_thumb_path, "-limit" , "thread", "1", "-resize",  "150x150", "-quality", "60", file_cat_path])
    
    post["thumb"] = file_thumb_name
    post["thumb_width"] = file_thumb_width
    post["thumb_height"] = file_thumb_height
  else:
    # Don't thumbnail and use mime image
    if board["board_type"] == '0':
      post["thumb"] = used_filetype['image']
      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'
  
  # calculate size (bytes)
  post["file_size"] = len(data)
  
  # add additional metadata, if any
  post["message"] += extraInfo(content_type, file_name, file_path)
  
  # file md5
  post["file_hex"] = getMD5(data)

  return post

def extraInfo(mime, file_name, file_path):
  board = Settings._.BOARD
  
  if mime in ['audio/ogg', 'audio/opus', 'audio/mpeg', 'video/webm', 'video/mp4']:
    info = ffprobe_f(file_path)
    extra = {}
    credit_str = ""
    
    if mime == 'video/webm':
      for s in info['streams']:
        if 'width' in s:
          stream = s
    else:
      stream = info['streams'][0]
    
    extra['codec'] = stream.get('codec_name', '').encode('utf-8')
    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')
      extra['artist'] = format['tags'].get('ARTIST', format['tags'].get('artist', '')).encode('utf-8')
      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')
      if extra['title'] or extra['artist']:
        credit_str = ' - '.join((extra['artist'], extra['title'])) + ' '
    
    return '<hr /><small>%s(%s)</small>' % (credit_str, extra['codec'])
    
  elif mime in ['audio/mod', 'audio/xm', 'audio/s3m']:
    ext = mime.split('/')[1].upper()
    url = '/cgi/play/%s/%s' % (board['dir'], file_name)
    return '<hr /><small>Módulo tracker (%s) [<a href="%s" target="_blank">Click para escuchar</a>]</small>' % (ext, url)
    
  return ''
    
def getImageInfo(data):
  data = str(data)
  size = len(data)
  height = -1
  width = -1
  extra = {}
  content_type = ""

  # handle GIFs
  if (size >= 10) and data[:6] in ("GIF87a", "GIF89a"):
    # Check to see if content_type is correct
    content_type = "image/gif"
    w, h = struct.unpack("<HH", data[6:10])
    width = int(w)
    height = int(h)

  # 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")
        and (data[12:16] == "IHDR")):
    content_type = "image/png"
    w, h = struct.unpack(">LL", data[16:24])
    width = int(w)
    height = int(h)

  # Maybe this is for an older PNG version.
  elif (size >= 16) and data.startswith("\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])
    width = int(w)
    height = int(h)

  # handle JPEGs
  elif (size >= 2) and data.startswith("\377\330"):
    content_type = "image/jpeg"
    jpeg = StringIO(data)
    jpeg.read(2)
    b = jpeg.read(1)
    try:
      while (b and ord(b) != 0xDA):
        while (ord(b) != 0xFF): b = jpeg.read
        while (ord(b) == 0xFF): b = jpeg.read(1)
        if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
          jpeg.read(3)
          h, w = struct.unpack(">HH", jpeg.read(4))
          break
        else:
          jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
        b = jpeg.read(1)
      width = int(w)
      height = int(h)
    except struct.error:
      pass
    except ValueError:
      pass
  
  # handle WebM
  elif (size >= 4) and data.startswith("\x1A\x45\xDF\xA3"):
    content_type = "video/webm"
    info = ffprobe(data)
  
  # handle mp4
  elif (size >= 8) and data[4:12] in ["ftypmp42", "ftypisom"]:
    content_type = "video/mp4"

  # handle ogg formats (vorbis/opus)
  elif (size >= 64) and data[:4] == "OggS":
    if data[28:35] == "\x01vorbis":
      content_type = "audio/ogg"
    elif data[28:36] == "OpusHead":
      content_type = "audio/opus"

  # handle MP3
  elif (size >= 64) and (data[:3] == "ID3" or data[:3] == "\xFF\xFB"):
    content_type = "audio/mpeg"

  # handle MOD
  elif (size >= 64) and data[1080:1084] == "M.K.":
    content_type = "audio/mod"

  # handle XM
  elif (size >= 64) and data.startswith("Extended Module:"):
    content_type = "audio/xm"

  # handle S3M
  elif (size >= 64) and data[25:32] == "\x00\x00\x00\x1A\x10\x00\x00":
    content_type = "audio/s3m"

  # handle PDF
  elif (size >= 4) and data[:7] == "%PDF-1.":
    content_type = "application/pdf"

  # handle Shockwave Flash
  elif (size >= 3) and data[:3] in ["CWS", "FWS"]:
    content_type = "application/x-shockwave-flash"

  # handle torrent
  elif (size >= 11) and data[:11] == "d8:announce":
    content_type = "application/x-bittorrent"

  # handle PDF
  elif (size >= 2) and data[:2] == "PK":
    content_type = "application/epub+zip"

  if content_type.startswith("video"):
    info = ffprobe(data)
    for stream in info['streams']:
      if 'width' in stream:
        width = stream['width']
        height = stream['height']
        break


  return content_type, width, height, size, extra

def ffprobe(data):
  import json
  p = subprocess.Popen([Settings.FFPROBE_PATH, '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', '-'],
        stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  
  out = p.communicate(input=data)[0]
  return json.loads(out)
  
def ffprobe_f(filename):
  import json
  
  p = subprocess.Popen([Settings.FFPROBE_PATH, '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', filename],
        stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  
  out = p.communicate()[0]
  return json.loads(out)

def getThumbDimensions(width, height, maxsize):
  """
  Calculate dimensions to use for a thumbnail with maximum width/height of
  <maxsize>, keeping aspect ratio
  """
  wratio = (float(maxsize) / float(width))
  hratio = (float(maxsize) / float(height))
  
  if (width <= maxsize) and (height <= maxsize):
    return width, height
  else:
    if (wratio * height) < maxsize:
      thumb_height = math.ceil(wratio * height)
      thumb_width = maxsize
    else:
      thumb_width = math.ceil(hratio * width)
      thumb_height = maxsize
  
  return int(thumb_width), int(thumb_height)

def checkFileDuplicate(data):
  """
  Check that the file <data> does not already exist in a live post on the
  current board by calculating its hex and checking it against the database
  """
  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']))
  if post:
    if int(post["parentid"]) != 0:
      return True, post["parentid"], post["id"]
    else:
      return True, post["id"], post["id"]
  else:
    return False, 0, 0

def getJpegSegments(data):
  if data[0:2] != b"\xff\xd8":
    raise UserError("Given data isn't JPEG.")

  head = 2
  segments = [b"\xff\xd8"]
  while 1:
    if data[head: head + 2] == b"\xff\xda":
        yield data[head:]
        break
    else:
        length = struct.unpack(">H", data[head + 2: head + 4])[0]
        endPoint = head + length + 2
        seg = data[head: endPoint]
        yield seg
        head = endPoint

    if (head >= len(data)):
        raise UserDataError("Wrong JPEG data.")
  
def removeExifData(src_data):
  exif = None
  
  for seg in getJpegSegments(src_data):
    if seg[0:2] == b"\xff\xe1" and seg[4:10] == b"Exif\x00\x00":
      exif = seg
      break
  
  if exif:
    return src_data.replace(exif, b"")
  else:
    return src_data