From ff6bf1b045fa810989a3efc78624123d3fc97f19 Mon Sep 17 00:00:00 2001 From: z411 Date: Mon, 1 Apr 2019 04:28:52 -0300 Subject: Reimplementado oekaki en Python y otros cambios - Portados algoritmos de guardado de imagen a Python e incorporados en weabot - Ahora los dibujos temporales se guardan en el directorio del board (adiĆ³s oek_temp) - (Temporal) Los dibujos no se borran si no que se guardan como .bak - Arreglado bug donde se pierde el dibujo si hubo cualquier problema al subir --- cgi/framework.py | 24 +++++++++++---------- cgi/oekaki.py | 56 ++++++++++++++++++++++++++++++++++++++++-------- cgi/templates/board.html | 2 +- cgi/templates/paint.html | 4 ++-- cgi/weabot.py | 18 ++++++++++++---- static/js/wpaint/bai.js | 5 +++-- 6 files changed, 80 insertions(+), 29 deletions(-) diff --git a/cgi/framework.py b/cgi/framework.py index e827f67..053bef0 100644 --- a/cgi/framework.py +++ b/cgi/framework.py @@ -235,17 +235,19 @@ def getFormData(self): self.environ["wsgi.post_form"] = post_form self.environ["wsgi.input"] = new_input - formdata = {} - for key in dict(fs): - try: - formdata.update({key: fs[key].value}) - if key == "file": - formdata.update({"file_original": secure_filename(fs[key].filename)}) - except AttributeError: - formdata.update({key: fs[key]}) - - return formdata - + try: + formdata = {} + for key in dict(fs): + try: + formdata.update({key: fs[key].value}) + if key == "file": + formdata.update({"file_original": secure_filename(fs[key].filename)}) + except AttributeError: + formdata.update({key: fs[key]}) + + return formdata + except TypeError: + return fs class InputProcessed(object): def read(self): raise EOFError("El stream de wsgi.input ya se ha consumido.") diff --git a/cgi/oekaki.py b/cgi/oekaki.py index 14446d7..14c3a1d 100644 --- a/cgi/oekaki.py +++ b/cgi/oekaki.py @@ -74,7 +74,7 @@ def oekaki(self, path_split): 'layer_count': '5', 'undo': '90', 'undo_in_mg': '15', - 'url_save': Settings.BOARDS_URL + 'oek_temp/save.php?applet=shi'+applet_str, + 'url_save': Settings.BOARDS_URL + 'oek_temp/save.py?applet=shi'+applet_str, 'poo': 'false', 'send_advance': 'true', 'send_language': 'utf8', @@ -109,6 +109,19 @@ def oekaki(self, path_split): 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 = inet_aton(self.environ["REMOTE_ADDR"]) + fname = "%s/%s/temp/%d.png" % (Settings.IMAGES_DIR, board['dir'], ip) + + 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 @@ -126,17 +139,11 @@ def oekaki(self, path_split): ts = int(time.time()) ip = inet_aton(self.environ["REMOTE_ADDR"]) - fname = "%s/oek_temp/%d.png" % (Settings.HOME_DIR, ip) + fname = "%s/%s/temp/%d.png" % (Settings.IMAGES_DIR, board['dir'], ip) oek = 'no' if 'filebase' in self.formdata: - img = self.formdata['filebase'] - if img.startswith("data:image/png;base64,"): - img = img[22:] - img = img.replace(' ', '+') - img = img.decode('base64') - with open(fname, 'wb') as f: - f.write(img) + write_from_base64(fname, self.formdata['filebase']) if os.path.isfile(fname): oek = ip @@ -176,3 +183,34 @@ def oekaki(self, path_split): 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 = data.decode('base64') + 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 != '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" \ No newline at end of file diff --git a/cgi/templates/board.html b/cgi/templates/board.html index 6ded4c5..09ed67d 100644 --- a/cgi/templates/board.html +++ b/cgi/templates/board.html @@ -31,7 +31,7 @@ No hay dibujo - + diff --git a/cgi/templates/paint.html b/cgi/templates/paint.html index 476babe..945a8d2 100644 --- a/cgi/templates/paint.html +++ b/cgi/templates/paint.html @@ -27,7 +27,7 @@ - + @@ -57,7 +57,7 @@ -
+
diff --git a/cgi/weabot.py b/cgi/weabot.py index c38a972..a85c2e7 100755 --- a/cgi/weabot.py +++ b/cgi/weabot.py @@ -38,7 +38,9 @@ class weabot(object): self.environ["PATH_INFO"] = self.environ["PATH_INFO"][11:] self.start = start_response - self.formdata = getFormData(self) + + if self.environ["QUERY_STRING"] != "rawpost": + self.formdata = getFormData(self) self.output = "" @@ -597,15 +599,23 @@ class weabot(object): # process files if oek_file: try: - fname = "%s/oek_temp/%s.png" % (Settings.HOME_DIR, oek_file) - with open(fname) as f: + fname = "%s/%s/temp/%s.png" % (Settings.IMAGES_DIR, board['dir'], oek_file) + with open(fname, 'rb') as f: file = f.read() - os.remove(fname) except: raise UserError, "Imposible leer la imagen oekaki." if file and not noimage: post = processImage(post, file, t, file_original, (spoil and board['allow_spoilers'] == '1')) + + if oek_file: + # Remove temporary oekaki file if everything went right + #os.remove(fname) + # TODO: We will rename the file for now. We don't want lost work. + try: + os.rename(fname, fname + ".bak") + except: + pass # Just keep it if anything went wrong # slip if board["slip"] != '0': diff --git a/static/js/wpaint/bai.js b/static/js/wpaint/bai.js index 6b436c1..ef93e02 100644 --- a/static/js/wpaint/bai.js +++ b/static/js/wpaint/bai.js @@ -1,10 +1,11 @@ function saveImg(image) { var _this = this; var url = document.getElementById('finish').href; + var save_url = document.getElementById('wPaint').dataset.save; $.ajax({ type: 'POST', - url: '/oek_temp/upload.php', + url: save_url, data: {image: image}, success: function (resp) { _this._displayStatus('Image saved successfully'); @@ -20,4 +21,4 @@ $('#wPaint').wPaint({ menuOffsetTop: -50, menuOrientation: 'horizontal', saveImg: saveImg -}); \ No newline at end of file +}); -- cgit v1.2.1-18-gbd029