# coding=utf-8 import os import cgi import random from database import * from settings import Settings from framework import * from formatting import * from template import * from post import * import netaddr def oekaki(self, path_split): """ Este script hace todo lo que tiene que hacer con los archivos de Oekaki. """ page = '' skiptemplate = False if len(path_split) > 2: # Inicia el applet. Lo envia luego a este mismo script, a "Finish". if path_split[2] == 'paint': # Veamos que applet usar applet = self.formdata['oek_applet'].split('|') applet_name = applet[0] if len(applet) > 1 and applet[1] == 'y': applet_str = 'pro' else: applet_str = '' if len(applet) > 2 and applet[2] == 'y': use_selfy = True else: use_selfy = False # Obtenemos el board board = setBoard(self.formdata['board']) if not board['allow_oekaki']: raise UserError('Esta sección no soporta oekaki.') # Veamos a quien le estamos respondiendo try: parentid = int(self.formdata['parent']) except: parentid = 0 # Vemos si el usuario quiere una animacion if 'oek_animation' in self.formdata: animation = True animation_str = 'animation' else: animation = False animation_str = '' # Nos aseguramos que la entrada es numerica try: width = int(self.formdata['oek_x']) height = int(self.formdata['oek_y']) except: raise UserError('Valores de tamaño inválidos (%s)' % repr( self.formdata)) params = { 'dir_resource': Settings.BOARDS_URL + 'oek_temp/', 'tt.zip': 'tt_def.zip', 'res.zip': 'res.zip', 'MAYSCRIPT': 'true', 'scriptable': 'true', 'tools': applet_str, 'layer_count': '5', 'undo': '90', 'undo_in_mg': '15', 'url_save': Settings.BOARDS_URL + 'oek_temp/save.py?applet=shi'+applet_str, 'poo': 'false', 'send_advance': 'true', 'send_language': 'utf8', 'send_header': '', 'send_header_image_type': 'false', 'thumbnail_type': animation_str, 'image_jpeg': 'false', 'image_size': '92', 'compress_level': '4' } if 'oek_edit' in self.formdata: # Si hay que editar, cargar la imagen correspondiente en el canvas pid = int(self.formdata['oek_edit']) post = FetchOne( 'SELECT id, file, image_width, image_height FROM posts WHERE id = %d AND boardid = %s' % (pid, board['id'])) editfile = Settings.BOARDS_URL + \ board['dir'] + '/src/' + post['file'] params['image_canvas'] = edit params['image_width'] = file['image_width'] params['image_height'] = file['image_height'] width = int(file['image_width']) height = int(file['image_height']) else: editfile = None params['image_width'] = str(width) params['image_height'] = str(height) if 'canvas' in self.formdata: editfile = self.formdata['canvas'] # Darle las dimensiones al exit script params['url_exit'] = Settings.CGI_URL + \ 'oekaki/finish/' + board['dir'] + '/' + str(parentid) page += renderTemplate("paint.html", {'applet': applet_name, 'edit': editfile, 'replythread': parentid, 'width': width, 'height': height, 'params': params, 'selfy': use_selfy}) elif path_split[2] == 'save': # path splits: # 3: Board # 4: Data format board = setBoard(path_split[3]) ip = int(netaddr.IPAddress(self.ip)) fname = os.path.join(Settings.IMAGES_DIR, board['dir'], "temp", str(ip) + ".png") if path_split[4] == 'b64': page = write_from_base64(fname, self.formdata['image']) elif path_split[4] == 'paintbbs': page = write_from_shi(fname, self.environ["wsgi.input"]) elif path_split[2] == 'finish': # path splits: # 3: Board # 4: Parentid if len(path_split) >= 5: # Al terminar de dibujar, llegamos aqui. Damos la opcion de postearlo. board = setBoard(path_split[3]) try: parentid = int(path_split[4]) except: parentid = None if not board['allow_oekaki']: raise UserError('Esta sección no soporta oekaki.') ts = int(time.time()) ip = int(netaddr.IPAddress(self.ip)) fname = os.path.join(Settings.IMAGES_DIR, board['dir'], "temp", str(ip) + ".png") oek = 'no' if 'filebase' in self.formdata: write_from_base64(fname, self.formdata['filebase']) if os.path.isfile(fname): oek = ip try: timetaken = timestamp() - int(path_split[5][:-2]) except: timetaken = 0 page += renderTemplate("board.html", { "threads": None, "oek_finish": oek, "replythread": parentid, "ts": ts}) elif path_split[2] == 'animation': try: board = setBoard(path_split[3]) file = int(path_split[4]) except: raise UserError('Board o archivo de animación inválido.') params = { 'pch_file': Settings.BOARDS_URL + board['dir'] + '/src/' + str(file) + '.pch', 'run': 'true', 'buffer_progress': 'false', 'buffer_canvas': 'true', 'speed': '2', 'res.zip': Settings.BOARDS_URL + 'oek_temp/res/' + 'res.zip', 'tt.zip': Settings.BOARDS_URL + 'oek_temp/res/' + 'tt.zip', 'tt_size': '31' } page += '' + \ '\n\nBienvenido a Internet | Oekaki\n\n' + \ '\n' + \ '
\n' page += '' for key in params: page += '' + "\n" page += '
Java must be installed and enabled to use this applet. Please refer to our Java setup tutorial for more information.
' page += '
\n
\n\n' if not skiptemplate: self.output = page def write_from_base64(fname, data): # Skip header if data.startswith("data:image/png;base64,"): data = data[22:] data = data.replace(' ', '+') data = decodeb64(data) with open(fname, 'wb') as f: f.write(data) return "OK" def write_from_shi(fname, fp): # Check data type type = fp.read(1) if type != b'P': return "UNSUPPORTED" # Read header headerLength = int(fp.read(8)) header = fp.read(headerLength) # Read image data imgLength = int(fp.read(8)) fp.read(2) # TODO: seek() isn't working for some reason. Debug. img = fp.read(imgLength) # Write image with open(fname, 'wb') as f: f.write(img) return "OK"