def test_one_as_array(self): test = [''' 1. .1'''] res = image.from_string(test) exp = [[[1, 0], [0, 1]]] self.assertTrue(res == exp)
def test_compound_dict(self): test = {'A': ''' 1. 2. .1 .2 '''} res = image.from_string(test) exp = {'A': [[[1, 0], [0, 1]], [[2, 0], [0, 2]]]} self.assertTrue(res == exp)
def test_one_image(self): test = ''' 1. .1''' res = image.from_string(test) exp = [[1, 0], [0, 1]] self.assertTrue(res == exp)
def test_multi(self): test = ''' 1. 2. .1 .2 ''' res = image.from_string(test) exp = [[[1, 0], [0, 1]], [[2, 0], [0, 2]]] self.assertTrue(res == exp)
def reupload(userid, submitid, submitfile): submitsize = len(submitfile) # Select submission data query = d.execute("SELECT userid, subtype, settings, rating FROM submission WHERE submitid = %i AND settings !~ 'h'", [submitid], ["single"]) if not query: raise WeasylError("Unexpected") elif userid != query[0]: raise WeasylError("Unexpected") elif "v" in query[2] or "D" in query[2]: raise WeasylError("Unexpected") subcat = query[1] / 1000 * 1000 if subcat not in m.ALL_SUBMISSION_CATEGORIES: raise WeasylError("Unexpected") premium = d.get_premium(userid) # Check invalid file data if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") # Write temporary submission file submitextension = files.get_extension_for_category(submitfile, subcat) if submitextension is None: raise WeasylError("submitType") elif subcat == m.ART_SUBMISSION_CATEGORY and submitextension not in [".jpg", ".png", ".gif"]: raise WeasylError("submitType") elif subcat == m.MULTIMEDIA_SUBMISSION_CATEGORY and submitextension not in [".mp3", ".swf"]: raise WeasylError("submitType") elif _limit(submitsize, submitextension, premium): raise WeasylError("submitSizeExceedsLimit") submit_file_type = submitextension.lstrip('.') im = None if submit_file_type in {'jpg', 'png', 'gif'}: im = image.from_string(submitfile) submit_media_item = orm.fetch_or_create_media_item( submitfile, file_type=submit_file_type, im=im) check_for_duplicate_media(userid, submit_media_item.mediaid) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'submission', submit_media_item, rating=query[3]) if subcat == m.ART_SUBMISSION_CATEGORY: cover_media_item = submit_media_item.ensure_cover_image() orm.SubmissionMediaLink.make_or_replace_link( submitid, 'cover', cover_media_item, rating=query[3]) generated_thumb = images.make_thumbnail(im) generated_thumb_media_item = orm.fetch_or_create_media_item( generated_thumb.to_buffer(format=images.image_file_type(generated_thumb)), file_type=submit_file_type, im=generated_thumb) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'thumbnail-generated', generated_thumb_media_item, rating=query[3])
def test_complex_multi(self): test = [ ''' 1. 2. .1 .2 ''', ''' 3. .3 ''' ] res = image.from_string(test) exp = [[[1, 0], [0, 1]], [[2, 0], [0, 2]], [[3, 0], [0, 3]]] self.assertTrue(res == exp)
def test_simple_dict(self): test = { 'A': ''' 1. .1 ''', 'B': ''' 2. .2 ''' } res = image.from_string(test) exp = {'A': [[1, 0], [0, 1]], 'B': [[2, 0], [0, 2]]} self.assertTrue(res == exp)
def test_two_images(self): test = [ ''' 1. .1 ''', ''' 2. .2 ''' ] res = image.from_string(test) exp = [[[1, 0], [0, 1]], [[2, 0], [0, 2]]] self.assertTrue(res == exp)
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, submitid, submitfile): submitsize = len(submitfile) # Select submission data query = d.execute( "SELECT userid, subtype, settings, rating FROM submission WHERE submitid = %i AND settings !~ 'h'", [submitid], ["single"]) if not query: raise WeasylError("Unexpected") elif userid != query[0]: raise WeasylError("Unexpected") elif "v" in query[2] or "D" in query[2]: raise WeasylError("Unexpected") subcat = query[1] / 1000 * 1000 if subcat not in m.ALL_SUBMISSION_CATEGORIES: raise WeasylError("Unexpected") premium = d.get_premium(userid) # Check invalid file data if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") # Write temporary submission file submitextension = files.get_extension_for_category(submitfile, subcat) if submitextension is None: raise WeasylError("submitType") elif subcat == m.ART_SUBMISSION_CATEGORY and submitextension not in [ ".jpg", ".png", ".gif" ]: raise WeasylError("submitType") elif subcat == m.MULTIMEDIA_SUBMISSION_CATEGORY and submitextension not in [ ".mp3", ".swf" ]: raise WeasylError("submitType") elif _limit(submitsize, submitextension, premium): raise WeasylError("submitSizeExceedsLimit") submit_file_type = submitextension.lstrip('.') im = None if submit_file_type in {'jpg', 'png', 'gif'}: im = image.from_string(submitfile) submit_media_item = orm.fetch_or_create_media_item( submitfile, file_type=submit_file_type, im=im) check_for_duplicate_media(userid, submit_media_item.mediaid) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission', submit_media_item, rating=query[3]) if subcat == m.ART_SUBMISSION_CATEGORY: cover_media_item = submit_media_item.ensure_cover_image() orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover', cover_media_item, rating=query[3]) generated_thumb = images.make_thumbnail(im) generated_thumb_media_item = orm.fetch_or_create_media_item( generated_thumb.to_buffer( format=images.image_file_type(generated_thumb)), file_type=submit_file_type, im=generated_thumb) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'thumbnail-generated', generated_thumb_media_item, rating=query[3])
def create_multimedia(userid, submission, embedlink=None, friends_only=None, tags=None, coverfile=None, thumbfile=None, submitfile=None, critique=False, create_notifications=True, auto_thumb=False): premium = d.get_premium(userid) embedlink = embedlink.strip() # Determine filesizes coversize = len(coverfile) thumbsize = len(thumbfile) submitsize = len(submitfile) if not submitsize and not embedlink: raise WeasylError("submitSizeZero") elif embedlink and not embed.check_valid(embedlink): raise WeasylError("embedlinkInvalid") elif coversize > 10 * _MEGABYTE: raise WeasylError("coverSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: raise WeasylError("thumbSizeExceedsLimit") if submitsize: submitextension = files.get_extension_for_category( submitfile, m.MULTIMEDIA_SUBMISSION_CATEGORY) if submitextension is None: raise WeasylError("submitType") elif submitextension not in [".mp3", ".swf"] and not embedlink: raise WeasylError("submitType") elif _limit(submitsize, submitextension, premium): raise WeasylError("submitSizeExceedsLimit") submit_media_item = orm.fetch_or_create_media_item( submitfile, file_type=submitextension.lstrip('.')) check_for_duplicate_media(userid, submit_media_item.mediaid) else: submit_media_item = None thumb_media_item = media.make_cover_media_item(thumbfile) cover_media_item = media.make_cover_media_item(coverfile) if cover_media_item and not thumb_media_item: thumb_media_item = cover_media_item tempthumb_media_item = None im = None if auto_thumb: if thumbsize == 0 and coversize == 0: # Fetch default thumbnail from source if available thumb_url = embed.thumbnail(embedlink) if thumb_url: resp = d.http_get(thumb_url, timeout=5) im = image.from_string(resp.content) if not im and (thumbsize or coversize): im = image.from_string(thumbfile or coverfile) if im: tempthumb = images.make_thumbnail(im) tempthumb_type = images.image_file_type(tempthumb) tempthumb_media_item = orm.fetch_or_create_media_item( tempthumb.to_buffer(format=tempthumb_type), file_type=tempthumb_type, im=tempthumb) # Assign settings settings = [] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings.append("v" if embedlink else "") settings = "".join(settings) # Inject embedlink if embedlink: submission.content = "".join([embedlink, "\n", submission.content]) # Create submission db = d.connect() now = arrow.get() try: q = (d.meta.tables['submission'].insert().values([{ "folderid": submission.folderid, "userid": userid, "unixtime": now, "title": submission.title, "content": submission.content, "subtype": submission.subtype, "rating": submission.rating, "settings": settings, "sorttime": now, }]).returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) except PostgresError: files.clear_temporary(userid) raise # Assign search tags searchtag.associate(userid, tags, submitid=submitid) if submit_media_item: orm.SubmissionMediaLink.make_or_replace_link( submitid, 'submission', submit_media_item, rating=submission.rating.code) if cover_media_item: orm.SubmissionMediaLink.make_or_replace_link( submitid, 'cover', cover_media_item, rating=submission.rating.code) if thumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item) if tempthumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-custom', tempthumb_media_item) # Create notifications if create_notifications: _create_notifications(userid, submitid, submission.rating, settings, submission.title, tags) # Clear temporary files files.clear_temporary(userid) d.metric('increment', 'submissions') d.metric('increment', 'multimediasubmissions') return submitid, bool(thumb_media_item)
def create_visual(userid, submission, friends_only, tags, imageURL, thumbfile, submitfile, critique, create_notifications): premium = d.get_premium(userid) if imageURL: resp = d.http_get(imageURL, timeout=5) submitfile = resp.content # Determine filesizes thumbsize = len(thumbfile) submitsize = len(submitfile) if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") elif thumbsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("thumbSizeExceedsLimit") im = image.from_string(submitfile) submitextension = image.image_extension(im) if _limit(submitsize, submitextension, premium): raise WeasylError("submitSizeExceedsLimit") elif submitextension not in [".jpg", ".png", ".gif"]: raise WeasylError("submitType") submit_file_type = submitextension.lstrip('.') submit_media_item = orm.fetch_or_create_media_item( submitfile, file_type=submit_file_type, im=im) check_for_duplicate_media(userid, submit_media_item.mediaid) cover_media_item = submit_media_item.ensure_cover_image(im) # Thumbnail stuff. # Always create a 'generated' thumbnail from the source image. thumb_generated = images.make_thumbnail(im) if thumb_generated is im: thumb_generated_media_item = submit_media_item else: thumb_generated_media_item = orm.fetch_or_create_media_item( thumb_generated.to_buffer(format=submit_file_type), file_type=submit_file_type, im=thumb_generated) # If requested, also create a 'custom' thumbnail. thumb_media_item = media.make_cover_media_item(thumbfile) if thumb_media_item: thumb_custom = images.make_thumbnail(image.from_string(thumbfile)) thumb_custom_media_item = orm.fetch_or_create_media_item( thumb_custom.to_buffer(format=submit_file_type), file_type=submit_file_type, im=thumb_custom) # Assign settings settings = [] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings = "".join(settings) # TODO(kailys): maintain ORM object db = d.connect() now = arrow.get() q = (d.meta.tables['submission'].insert().values([{ "folderid": submission.folderid, "userid": userid, "unixtime": now, "title": submission.title, "content": submission.content, "subtype": submission.subtype, "rating": submission.rating.code, "settings": settings, "sorttime": now, }]).returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission', submit_media_item) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover', cover_media_item) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-generated', thumb_generated_media_item) if thumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-custom', thumb_custom_media_item) # Assign search tags searchtag.associate(userid, tags, submitid=submitid) # Create notifications if create_notifications: _create_notifications(userid, submitid, submission.rating, settings, submission.title, tags) d.metric('increment', 'submissions') d.metric('increment', 'visualsubmissions') return submitid
.....1 1..... ''' ], #LEFT [ ''' ..1..1 1..1.. .1..1. .1..1. .222.. .222.. .222.. .222.. .1..1. .1..1. 1..1.. ..1..1 ''' ] ] LITTLE_BUG = image.from_string(LITTLE_BUG,COLORS_BUG) BIG_BUG = { 'standing' : ''' ...2...........2. ....2.........2.. .....2.......2... ......2.....2.... .......2...2..... ......11.11...... .....1111111..... ....111311311.... .....1111111..... .....111..11..... ......11111......
def create_multimedia(userid, submission, embedlink=None, friends_only=None, tags=None, coverfile=None, thumbfile=None, submitfile=None, critique=False, create_notifications=True, auto_thumb=False): premium = d.get_premium(userid) embedlink = embedlink.strip() # Determine filesizes coversize = len(coverfile) thumbsize = len(thumbfile) submitsize = len(submitfile) if not submitsize and not embedlink: raise WeasylError("submitSizeZero") elif embedlink and not embed.check_valid(embedlink): raise WeasylError("embedlinkInvalid") elif coversize > 10 * _MEGABYTE: raise WeasylError("coverSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: raise WeasylError("thumbSizeExceedsLimit") if submitsize: submitextension = files.get_extension_for_category(submitfile, m.MULTIMEDIA_SUBMISSION_CATEGORY) if submitextension is None: raise WeasylError("submitType") elif submitextension not in [".mp3", ".swf"] and not embedlink: raise WeasylError("submitType") elif _limit(submitsize, submitextension, premium): raise WeasylError("submitSizeExceedsLimit") submit_media_item = orm.fetch_or_create_media_item( submitfile, file_type=submitextension.lstrip('.')) check_for_duplicate_media(userid, submit_media_item.mediaid) else: submit_media_item = None thumb_media_item = media.make_cover_media_item(thumbfile) cover_media_item = media.make_cover_media_item(coverfile) if cover_media_item and not thumb_media_item: thumb_media_item = cover_media_item tempthumb_media_item = None im = None if auto_thumb: if thumbsize == 0 and coversize == 0: # Fetch default thumbnail from source if available thumb_url = embed.thumbnail(embedlink) if thumb_url: resp = d.http_get(thumb_url, timeout=5) im = image.from_string(resp.content) if not im and (thumbsize or coversize): im = image.from_string(thumbfile or coverfile) if im: tempthumb = images.make_thumbnail(im) tempthumb_type = images.image_file_type(tempthumb) tempthumb_media_item = orm.fetch_or_create_media_item( tempthumb.to_buffer(format=tempthumb_type), file_type=tempthumb_type, im=tempthumb) # Assign settings settings = [] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings.append("v" if embedlink else "") settings = "".join(settings) # Inject embedlink if embedlink: submission.content = "".join([embedlink, "\n", submission.content]) # Create submission db = d.connect() now = arrow.get() try: q = ( d.meta.tables['submission'].insert().values([{ "folderid": submission.folderid, "userid": userid, "unixtime": now, "title": submission.title, "content": submission.content, "subtype": submission.subtype, "rating": submission.rating, "settings": settings, "sorttime": now, }]) .returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) except PostgresError: files.clear_temporary(userid) raise # Assign search tags searchtag.associate(userid, tags, submitid=submitid) if submit_media_item: orm.SubmissionMediaLink.make_or_replace_link( submitid, 'submission', submit_media_item, rating=submission.rating.code) if cover_media_item: orm.SubmissionMediaLink.make_or_replace_link( submitid, 'cover', cover_media_item, rating=submission.rating.code) if thumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item) if tempthumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-custom', tempthumb_media_item) # Create notifications if create_notifications: _create_notifications(userid, submitid, submission.rating, settings, submission.title, tags) # Clear temporary files files.clear_temporary(userid) d.metric('increment', 'submissions') d.metric('increment', 'multimediasubmissions') return submitid, bool(thumb_media_item)
def create_visual(userid, submission, friends_only, tags, imageURL, thumbfile, submitfile, critique, create_notifications): premium = d.get_premium(userid) if imageURL: resp = d.http_get(imageURL, timeout=5) submitfile = resp.content # Determine filesizes thumbsize = len(thumbfile) submitsize = len(submitfile) if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") elif thumbsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("thumbSizeExceedsLimit") im = image.from_string(submitfile) submitextension = image.image_extension(im) if _limit(submitsize, submitextension, premium): raise WeasylError("submitSizeExceedsLimit") elif submitextension not in [".jpg", ".png", ".gif"]: raise WeasylError("submitType") submit_file_type = submitextension.lstrip('.') submit_media_item = orm.fetch_or_create_media_item( submitfile, file_type=submit_file_type, im=im) check_for_duplicate_media(userid, submit_media_item.mediaid) cover_media_item = submit_media_item.ensure_cover_image(im) # Thumbnail stuff. # Always create a 'generated' thumbnail from the source image. thumb_generated = images.make_thumbnail(im) if thumb_generated is im: thumb_generated_media_item = submit_media_item else: thumb_generated_media_item = orm.fetch_or_create_media_item( thumb_generated.to_buffer(format=submit_file_type), file_type=submit_file_type, im=thumb_generated) # If requested, also create a 'custom' thumbnail. thumb_media_item = media.make_cover_media_item(thumbfile) if thumb_media_item: thumb_custom = images.make_thumbnail(image.from_string(thumbfile)) thumb_custom_media_item = orm.fetch_or_create_media_item( thumb_custom.to_buffer(format=submit_file_type), file_type=submit_file_type, im=thumb_custom) # Assign settings settings = [] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings = "".join(settings) # TODO(kailys): maintain ORM object db = d.connect() now = arrow.get() q = ( d.meta.tables['submission'].insert().values([{ "folderid": submission.folderid, "userid": userid, "unixtime": now, "title": submission.title, "content": submission.content, "subtype": submission.subtype, "rating": submission.rating.code, "settings": settings, "sorttime": now, }]).returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'submission', submit_media_item) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'cover', cover_media_item) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'thumbnail-generated', thumb_generated_media_item) if thumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'thumbnail-custom', thumb_custom_media_item) # Assign search tags searchtag.associate(userid, tags, submitid=submitid) # Create notifications if create_notifications: _create_notifications(userid, submitid, submission.rating, settings, submission.title, tags) d.metric('increment', 'submissions') d.metric('increment', 'visualsubmissions') return submitid