def _upload_char(filedata, charid): """ Creates a preview-size copy of an uploaded image file for a new thumbnail selection file. """ filename = d.url_make(charid, "char/.thumb", root=True) files.write(filename, filedata) if image.check_type(filename): image.make_cover(filename) else: os.remove(filename) raise WeasylError("FileType")
def reupload(userid, charid, submitdata): submitsize = len(submitdata) if not submitsize: raise WeasylError("submitSizeZero") elif submitsize > 10 * _MEGABYTE: raise WeasylError("submitSizeExceedsLimit") # Select character data query, = define.engine.execute(""" SELECT userid, settings FROM character WHERE charid = %(character)s AND settings !~ 'h' """, character=charid) if userid != query.userid: raise WeasylError("Unexpected") im = image.from_string(submitdata) submitextension = image.image_extension(im) # Check invalid file data if not submitextension: raise WeasylError("submitType") # Make submission file submitfile = files.make_resource(userid, charid, "char/submit", submitextension) files.ensure_file_directory(submitfile) im.write(submitfile) # Make cover file image.make_cover( submitfile, files.make_resource(userid, charid, "char/cover", submitextension)) # Update settings settings = re.sub(r'[~=].', '', query.settings) settings += files.typeflag("submit", submitextension) settings += files.typeflag("cover", submitextension) define.engine.execute(""" UPDATE character SET settings = %(settings)s WHERE charid = %(character)s """, settings=settings, character=charid)
def reupload(userid, charid, submitdata): submitsize = len(submitdata) if not submitsize: raise WeasylError("submitSizeZero") elif submitsize > 10 * _MEGABYTE: raise WeasylError("submitSizeExceedsLimit") # Select character data query, = define.engine.execute(""" SELECT userid, settings FROM character WHERE charid = %(character)s AND settings !~ 'h' """, character=charid) if userid != query.userid: raise WeasylError("Unexpected") im = image.from_string(submitdata) submitextension = image.image_extension(im) # Check invalid file data if not submitextension: raise WeasylError("submitType") # Make submission file submitfile = files.make_resource(userid, charid, "char/submit", submitextension) files.ensure_file_directory(submitfile) im.write(submitfile) # Make cover file image.make_cover( submitfile, files.make_resource(userid, charid, "char/cover", submitextension)) # Update settings settings = re.sub(r'[~=].', '', query.settings) settings += files.typeflag("submit", submitextension) settings += files.typeflag("cover", submitextension) define.engine.execute(""" UPDATE character SET settings = %(settings)s WHERE charid = %(character)s """, settings=settings, character=charid)
def _upload_char(userid, filedata, charid): """ Creates a preview-size copy of an uploaded image file for a new thumbnail selection file. """ query = d.execute( "SELECT userid, settings FROM character WHERE charid = %i", [charid], options="single") if not query: raise WeasylError("Unexpected") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") filename = d.url_make(charid, "char/.thumb", root=True) files.write(filename, filedata, encoding=None) if image.check_type(filename): image.make_cover(filename) else: files.remove(filename) raise WeasylError("FileType")
def _upload_char(userid, filedata, charid): """ Creates a preview-size copy of an uploaded image file for a new thumbnail selection file. """ query = d.execute( "SELECT userid, settings FROM character WHERE charid = %i", [charid], options="single") if not query: raise WeasylError("Unexpected") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") filename = d.url_make(charid, "char/.thumb", root=True) files.write(filename, filedata) if image.check_type(filename): image.make_cover(filename) else: os.remove(filename) raise WeasylError("FileType")
def create(userid, character, friends, tags, thumbfile, submitfile): # Make temporary filenames tempthumb = files.get_temporary(userid, "thumb") tempsubmit = files.get_temporary(userid, "char") # Determine filesizes thumbsize = len(thumbfile) submitsize = len(submitfile) # Check invalid title or rating if not character.char_name: raise WeasylError("characterNameInvalid") elif not character.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, character.rating) # Write temporary thumbnail file if thumbsize: files.write(tempthumb, thumbfile) thumbextension = files.get_extension_for_category( thumbfile, macro.ART_SUBMISSION_CATEGORY) else: thumbextension = None # Write temporary submission file if submitsize: files.write(tempsubmit, submitfile) submitextension = files.get_extension_for_category( submitfile, macro.ART_SUBMISSION_CATEGORY) else: submitextension = None # Check invalid file data if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") elif submitsize > _MAIN_IMAGE_SIZE_LIMIT: files.clear_temporary(userid) raise WeasylError("submitSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("thumbSizeExceedsLimit") elif submitextension not in [".jpg", ".png", ".gif"]: files.clear_temporary(userid) raise WeasylError("submitType") elif thumbsize and thumbextension not in [".jpg", ".png", ".gif"]: files.clear_temporary(userid) raise WeasylError("thumbType") # Assign settings settings = [] settings.append("f" if friends else "") settings.append(files.typeflag("submit", submitextension)) settings.append(files.typeflag("cover", submitextension)) settings = "".join(settings) # Insert submission data ch = define.meta.tables["character"] try: charid = define.engine.scalar(ch.insert().returning(ch.c.charid), { "userid": userid, "unixtime": arrow.now(), "char_name": character.char_name, "age": character.age, "gender": character.gender, "height": character.height, "weight": character.weight, "species": character.species, "content": character.content, "rating": character.rating.code, "settings": settings, }) except PostgresError: files.clear_temporary(userid) raise # Assign search tags searchtag.associate(userid, tags, charid=charid) # Make submission file files.make_character_directory(charid) files.copy(tempsubmit, files.make_resource(userid, charid, "char/submit", submitextension)) # Make cover file image.make_cover(tempsubmit, files.make_resource(userid, charid, "char/cover", submitextension)) # Make thumbnail selection file if thumbsize: image.make_cover( tempthumb, files.make_resource(userid, charid, "char/.thumb")) thumbnail.create(0, 0, 0, 0, charid=charid, remove=False) # Create notifications welcome.character_insert(userid, charid, rating=character.rating.code, settings=settings) # Clear temporary files files.clear_temporary(userid) define.metric('increment', 'characters') return charid
def create(userid, character, friends, tags, thumbfile, submitfile): # Make temporary filenames tempthumb = files.get_temporary(userid, "thumb") tempsubmit = files.get_temporary(userid, "char") # Determine filesizes thumbsize = len(thumbfile) submitsize = len(submitfile) # Check invalid title or rating if not character.char_name: raise WeasylError("characterNameInvalid") elif not character.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, character.rating) # Write temporary thumbnail file if thumbsize: files.easyupload(tempthumb, thumbfile, "image") thumbextension = files.get_extension_for_category( thumbfile, macro.ART_SUBMISSION_CATEGORY) else: thumbextension = None # Write temporary submission file if submitsize: files.easyupload(tempsubmit, submitfile, "image") submitextension = files.get_extension_for_category( submitfile, macro.ART_SUBMISSION_CATEGORY) else: submitextension = None # Check invalid file data if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") elif submitsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("submitSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("thumbSizeExceedsLimit") elif submitextension not in [".jpg", ".png", ".gif"]: files.clear_temporary(userid) raise WeasylError("submitType") elif thumbsize and thumbextension not in [".jpg", ".png", ".gif"]: files.clear_temporary(userid) raise WeasylError("thumbType") # Assign settings settings = [] settings.append("f" if friends else "") settings.append(files.typeflag("submit", submitextension)) settings.append(files.typeflag("cover", submitextension)) settings = "".join(settings) # Insert submission data ch = define.meta.tables["character"] try: charid = define.engine.scalar(ch.insert().returning(ch.c.charid), { "userid": userid, "unixtime": arrow.now(), "char_name": character.char_name, "age": character.age, "gender": character.gender, "height": character.height, "weight": character.weight, "species": character.species, "content": character.content, "rating": character.rating.code, "settings": settings, }) except PostgresError: files.clear_temporary(userid) raise # Assign search tags searchtag.associate(userid, tags, charid=charid) # Make submission file files.make_path(charid, "char") files.copy(tempsubmit, files.make_resource(userid, charid, "char/submit", submitextension)) # Make cover file image.make_cover(tempsubmit, files.make_resource(userid, charid, "char/cover", submitextension)) # Make thumbnail selection file if thumbsize: image.make_cover( tempthumb, files.make_resource(userid, charid, "char/.thumb")) thumbnail.create(userid, 0, 0, 0, 0, charid=charid, remove=False) # Create notifications welcome.character_insert(userid, charid, rating=character.rating.code, settings=settings) # Clear temporary files files.clear_temporary(userid) define.metric('increment', 'characters') return charid