Exemple #1
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])
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
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)
    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)
Exemple #5
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)
    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)
Exemple #6
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
Exemple #7
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)
Exemple #8
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)
Exemple #9
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