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 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(userid, x1, y1, x2, y2, submitid=None, charid=None, config=None, remove=True): if charid: return _create_char(userid, x1, y1, x2, y2, charid, config, remove) db = d.connect() x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) source = thumbnail_source(submitid) im = db.query(orm.MediaItem).get(source['mediaid']).as_image() size = im.size.width, im.size.height bounds = None if image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = images.make_thumbnail(im, bounds) file_type = images.image_file_type(im) media_item = orm.fetch_or_create_media_item( thumb.to_buffer(format=file_type), file_type=file_type, im=thumb) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-custom', media_item)
def make_resized_media_item(filedata, size, error_type='FileType'): if not filedata: return None im = image.from_string(filedata) file_type = image.image_file_type(im) if file_type not in ["jpg", "png", "gif"]: raise WeasylError(error_type) resized = image.resize_image(im, *size) if resized is not im: filedata = resized.to_buffer(format=file_type) return orm.fetch_or_create_media_item(filedata, file_type=file_type, im=resized)
def make_resized_media_item(filedata, size, error_type='FileType'): if not filedata: return None im = image.from_string(filedata) file_type = image.image_file_type(im) if file_type not in ["jpg", "png", "gif"]: raise WeasylError(error_type) resized = image.resize_image(im, *size) if resized is not im: filedata = resized.to_buffer(format=file_type) return orm.fetch_or_create_media_item(filedata, file_type=file_type, im=resized)
def create(userid, x1, y1, x2, y2, auto=False, config=None): x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) db = d.connect() im = db.query(orm.MediaItem).get(avatar_source(userid)['mediaid']).as_image() file_type = image.image_file_type(im) bounds = None size = im.size.width, im.size.height if not auto and image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = image.shrinkcrop(im, geometry.Size(100, 100), bounds) media_item = orm.fetch_or_create_media_item( thumb.to_buffer(format=file_type), file_type=file_type, im=thumb) orm.UserMediaLink.make_or_replace_link(userid, 'avatar', media_item) orm.UserMediaLink.clear_link(userid, 'avatar-source')
def create(userid, x1, y1, x2, y2, auto=False, config=None): x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) db = d.connect() im = db.query(orm.MediaItem).get( avatar_source(userid)['mediaid']).as_image() file_type = image.image_file_type(im) bounds = None size = im.size.width, im.size.height if not auto and image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = image.shrinkcrop(im, geometry.Size(100, 100), bounds) media_item = orm.fetch_or_create_media_item( thumb.to_buffer(format=file_type), file_type=file_type, im=thumb) orm.UserMediaLink.make_or_replace_link(userid, 'avatar', media_item) orm.UserMediaLink.clear_link(userid, 'avatar-source')
def create(userid, x1, y1, x2, y2, submitid=None, charid=None, config=None, remove=True): if charid: return _create_char(userid, x1, y1, x2, y2, charid, config, remove) db = d.connect() x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) source = thumbnail_source(submitid) im = db.query(orm.MediaItem).get(source['mediaid']).as_image() size = im.size.width, im.size.height bounds = None if image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = images.make_thumbnail(im, bounds) file_type = images.image_file_type(im) media_item = orm.fetch_or_create_media_item( thumb.to_buffer(format=file_type), file_type=file_type, im=thumb) orm.SubmissionMediaLink.make_or_replace_link( submitid, 'thumbnail-custom', media_item)
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_literary(userid, submission, embedlink=None, friends_only=False, tags=None, coverfile=None, thumbfile=None, submitfile=None, critique=False, create_notifications=True): premium = d.get_premium(userid) if embedlink: check_google_doc_embed_data(embedlink) # Determine filesizes coversize = len(coverfile) thumbsize = len(thumbfile) submitsize = len(submitfile) if not submitsize and not embedlink: raise WeasylError("submitSizeZero") elif coversize > 10 * _MEGABYTE: raise WeasylError("coverSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: raise WeasylError("thumbSizeExceedsLimit") if submitsize: submitextension = files.get_extension_for_category( submitfile, m.TEXT_SUBMISSION_CATEGORY) if submitextension is None: raise WeasylError("submitType") if _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 # Assign settings settings = [] settings.append("f" if friends_only else "") settings.append("q" if critique else "") if embedlink: settings.append('D') settings = "".join(settings) # Create submission # TODO(kailys): use ORM object 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.code, "settings": settings, "sorttime": now, }]).returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) if embedlink: q = (d.meta.tables['google_doc_embeds'].insert().values( submitid=submitid, embed_url=embedlink)) db.execute(q) except: 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) # 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', 'literarysubmissions') 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
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) if cover_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover', cover_media_item) 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_literary(userid, submission, embedlink=None, friends_only=False, tags=None, coverfile=None, thumbfile=None, submitfile=None, critique=False, create_notifications=True): premium = d.get_premium(userid) if embedlink: check_google_doc_embed_data(embedlink) # Determine filesizes coversize = len(coverfile) thumbsize = len(thumbfile) submitsize = len(submitfile) if not submitsize and not embedlink: raise WeasylError("submitSizeZero") elif coversize > 10 * _MEGABYTE: raise WeasylError("coverSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: raise WeasylError("thumbSizeExceedsLimit") if submitsize: submitextension = files.get_extension_for_category(submitfile, m.TEXT_SUBMISSION_CATEGORY) if submitextension is None: raise WeasylError("submitType") if _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 # Assign settings settings = [] settings.append("f" if friends_only else "") settings.append("q" if critique else "") if embedlink: settings.append('D') settings = "".join(settings) # Create submission # TODO(kailys): use ORM object 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.code, "settings": settings, "sorttime": now, }]) .returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) if embedlink: q = (d.meta.tables['google_doc_embeds'].insert() .values(submitid=submitid, embed_url=embedlink)) db.execute(q) except: 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) if cover_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover', cover_media_item) if thumb_media_item: orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_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', 'literarysubmissions') 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