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): 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): raise WeasylError("submitSizeExceedsLimit") submit_media_item = orm.MediaItem.fetch_or_create( submitfile, file_type=submitextension.lstrip('.')) check_for_duplicate_media(userid, submit_media_item.mediaid) else: submit_media_item = None # Check if the submission is spam is_spam = _check_for_spam(submission=submission, userid=userid) 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.MediaItem.fetch_or_create( 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() 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, "favorites": 0, "is_spam": is_spam, "submitter_ip_address": submission.submitter_ip_address, "submitter_user_agent_id": submission.submitter_user_agent_id, }]).returning(d.meta.tables['submission'].c.submitid)) submitid = db.scalar(q) # 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) d.metric('increment', 'submissions') d.metric('increment', 'multimediasubmissions') return submitid, bool(thumb_media_item)
def edit(userid, submission, embedlink=None, friends_only=False, critique=False): query = d.execute( "SELECT userid, subtype, settings FROM submission WHERE submitid = %i", [submission.submitid], ["single"]) if not query or "h" in query[2]: raise WeasylError("Unexpected") elif "a" in query[2] and userid not in staff.MODS: raise WeasylError("AdminLocked") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") elif not submission.title: raise WeasylError("titleInvalid") elif not submission.rating: raise WeasylError("Unexpected") elif not folder.check(query[0], submission.folderid): raise WeasylError("Unexpected") elif submission.subtype / 1000 != query[1] / 1000: raise WeasylError("Unexpected") elif 'v' in query[2] and not embed.check_valid(embedlink): raise WeasylError("embedlinkInvalid") elif 'D' in query[2]: check_google_doc_embed_data(embedlink) profile.check_user_rating_allowed(userid, submission.rating) # Assign settings settings = [query[2].replace("f", "").replace("q", "")] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings = "".join(settings) if "v" in settings: submission.content = "%s\n%s" % (embedlink, submission.content) if "f" in settings: welcome.submission_became_friends_only(submission.submitid, userid) # TODO(kailys): maintain ORM object db = d.connect() su = d.meta.tables['submission'] q = ( su.update() .values( folderid=submission.folderid, title=submission.title, content=submission.content, subtype=submission.subtype, rating=submission.rating, settings=settings, ) .where(su.c.submitid == submission.submitid)) db.execute(q) if 'D' in settings: db = d.connect() gde = d.meta.tables['google_doc_embeds'] q = (gde.update() .values(embed_url=embedlink) .where(gde.c.submitid == submission.submitid)) db.execute(q) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following submission was edited:', '- ' + text.markdown_link(submission.title, '/submission/%s?anyway=true' % (submission.submitid,)))
def edit(userid, submission, embedlink=None, friends_only=False, critique=False): query = d.engine.execute( "SELECT userid, subtype, settings FROM submission WHERE submitid = %(id)s", id=submission.submitid).first() if not query or "h" in query[2]: raise WeasylError("Unexpected") elif "a" in query[2] and userid not in staff.MODS: raise WeasylError("AdminLocked") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") elif not submission.title: raise WeasylError("titleInvalid") elif not submission.rating: raise WeasylError("Unexpected") elif not folder.check(query[0], submission.folderid): raise WeasylError("Unexpected") elif submission.subtype // 1000 != query[1] // 1000: raise WeasylError("Unexpected") elif 'v' in query[2] and not embed.check_valid(embedlink): raise WeasylError("embedlinkInvalid") elif 'D' in query[2]: check_google_doc_embed_data(embedlink) profile.check_user_rating_allowed(userid, submission.rating) # Assign settings settings = [query[2].replace("f", "").replace("q", "")] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings = "".join(settings) if "v" in settings: submission.content = "%s\n%s" % (embedlink, submission.content) if "f" in settings: welcome.submission_became_friends_only(submission.submitid, userid) # TODO(kailys): maintain ORM object db = d.connect() su = d.meta.tables['submission'] q = (su.update().values( folderid=submission.folderid, title=submission.title, content=submission.content, subtype=submission.subtype, rating=submission.rating, settings=settings, ).where(su.c.submitid == submission.submitid)) db.execute(q) if 'D' in settings: db = d.connect() gde = d.meta.tables['google_doc_embeds'] q = (gde.update().values(embed_url=embedlink).where( gde.c.submitid == submission.submitid)) db.execute(q) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following submission was edited:', '- ' + text.markdown_link( submission.title, '/submission/%s?anyway=true' % (submission.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)