예제 #1
0
파일: useralias.py 프로젝트: 0x15/weasyl
def set(userid, username):
    if login.username_exists(username):
        raise WeasylError("usernameExists")
    elif not d.get_premium(None, userid):
        raise WeasylError("InsufficientPermissions")

    d.execute("DELETE FROM useralias WHERE userid = %i AND settings ~ 'p'", [userid])
    d.execute("INSERT INTO useralias VALUES (%i, '%s', 'p')", [userid, username])
예제 #2
0
파일: submission.py 프로젝트: dzamie/weasyl
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])
예제 #3
0
def set(userid, username):
    if login.username_exists(username):
        raise WeasylError("usernameExists")
    elif not d.get_premium(None, userid):
        raise WeasylError("InsufficientPermissions")

    d.execute("DELETE FROM useralias WHERE userid = %i AND settings ~ 'p'",
              [userid])
    d.execute("INSERT INTO useralias VALUES (%i, '%s', 'p')",
              [userid, username])
예제 #4
0
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])
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
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
예제 #8
0
파일: submission.py 프로젝트: dzamie/weasyl
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)
예제 #9
0
파일: submission.py 프로젝트: dzamie/weasyl
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)
예제 #10
0
파일: submission.py 프로젝트: dzamie/weasyl
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