def save_playback_data(playback_data, comment=None, uuid=None):
    if comment is None and uuid is None:
        raise ValueError("Must supply either a comment or a UUID.")

    if uuid is not None:
        try:
            comment = QuestComment.objects.get(uuid=uuid_string_to_int(uuid))
        except QuestComment.DoesNotExist:
            filename = filename_for_uuid(uuid)

    if comment is not None:
        filename = comment.playback_filename

    gzipped = StringIO.StringIO()
    f = gzip.GzipFile(fileobj=gzipped, mode='w')
    f.write(playback_data)
    f.close()

    try:
        fs = get_fs(*settings.PLAYBACK_FS)
    except SSLError:
        return None

    if fs is not None:
        fs.save(comment.playback_filename, gzipped.getvalue())

    if comment is not None:
        save_stroke_count(comment, playback_data)
def save_playback_data(playback_data, comment=None, uuid=None):
    if comment is None and uuid is None:
        raise ValueError("Must supply either a comment or a UUID.")

    if uuid is not None:
        try:
            comment = QuestComment.objects.get(uuid=uuid_string_to_int(uuid))
        except QuestComment.DoesNotExist:
            filename = filename_for_uuid(uuid)

    if comment is not None:
        filename = comment.playback_filename

    gzipped = StringIO.StringIO()
    f = gzip.GzipFile(fileobj=gzipped, mode="w")
    f.write(playback_data)
    f.close()

    try:
        fs = get_fs(*settings.PLAYBACK_FS)
    except SSLError:
        return None

    if fs is not None:
        fs.save(comment.playback_filename, gzipped.getvalue())

    if comment is not None:
        save_stroke_count(comment, playback_data)
def _got_imagedata(filedata, request, url=''):
    remix_of = request.GET.get('remix_of')
    stamps_used = request.GET.getlist('used_stamps')
    text_used = request.GET.get('used_text', '')
    is_quest = str(request.GET.get('is_quest', 0)) == '1'

    fs = get_fs(*settings.IMAGE_FS)

    try:
        return create_content(request.META['REMOTE_ADDR'], fs, filedata, remix_of, stamps_used, is_quest=is_quest)
    except IOError, e:
        util.papertrail.debug('UPLOADS: unable to read image')
        client.captureException()
        raise ServiceError("Unable to read image.")
def get_playback_data(comment):
    try:
        fs = get_fs(*settings.PLAYBACK_FS)
    except SSLError:
        return None

    def read_file(filename):
        gzipped_data = fs.read(filename)
        f = gzip.GzipFile(fileobj=StringIO.StringIO(gzipped_data))
        data = f.read()
        f.close()
        return data

    try:
        data = read_file(comment.playback_filename)
    except (S3ResponseError, IOError):
        try:
            data = read_file(filename_for_uuid(comment.uuid))
        except (S3ResponseError, IOError, ValueError):
            data = None

    return data
def get_playback_data(comment):
    try:
        fs = get_fs(*settings.PLAYBACK_FS)
    except SSLError:
        return None

    def read_file(filename):
        gzipped_data = fs.read(filename)
        f = gzip.GzipFile(fileobj=StringIO.StringIO(gzipped_data))
        data = f.read()
        f.close()
        return data

    try:
        data = read_file(comment.playback_filename)
    except (S3ResponseError, IOError):
        try:
            data = read_file(filename_for_uuid(comment.uuid))
        except (S3ResponseError, IOError, ValueError):
            data = None

    return data
def combine_upload_chunks(request, chunks, metadata, is_quest=False):
    keys = ['chunk:{0}'.format(chunk) for chunk in chunks]

    raw_values = redis.mget(keys)

    if not all(raw_values):
        raise ServiceError("Missing uploaded chunk, please retry.")

    values = [b64decode(val) for val in raw_values]
    filedata = "".join(values)

    fs = get_fs(*settings.IMAGE_FS)
    remix_of = metadata.get('remix_of')
    stamps_used = metadata.get('used_stamps', []) or []
    text_used = metadata.get('used_text', '') or ''

    redis.delete(*keys)

    try:
        return create_content(request.META['REMOTE_ADDR'], fs, filedata, remix_of, stamps_used,
                              is_quest=is_quest)
    except IOError, e:
        raise ServiceError("Unable to read image.")