Ejemplo n.º 1
0
async def upload_image(request):
    data = await request.post()
    try:
        file_field = data['file']
        type_field = data['type']
        if type_field == 'cover':
            resolution = (320, 200)
            dst = _COVER_PATH
        elif type_field == 'userphoto':
            resolution = (256, 256)
            dst = _USERPHOTO_PATH
        else:
            return web.json_response(
                makeResponseFailed('INCORRECT_UPLOAD_TYPE'), dumps=dumps)
        content = file_field.file.read()
    except:
        return web.json_response(makeResponseFailed('INCORRECT_REQUEST'),
                                 dumps=dumps)
    if len(content) > UploadConfig.MAX_UPLOAD_SIZE:
        return web.json_response(makeResponseFailed('FILE_TOO_LARGE'),
                                 dumps=dumps)
    try:
        img = Image.open(io.BytesIO(content))
        if img is None:
            raise Exception()
    except:
        return web.json_response(makeResponseFailed('UNRECOGNIZED_IMAGE_FILE'),
                                 dumps=dumps)
    if isinstance(img, PIL.GifImagePlugin.GifImageFile):
        filename = random_bytes_str(24) + ".gif"
        frames = ImageSequence.Iterator(img)
        frames = _gif_thumbnails(frames, resolution)
        om = next(frames)  # Handle first frame separately
        om.info = img.info  # Copy sequence info
        om.save(dst + filename,
                save_all=True,
                append_images=list(frames),
                loop=0)
    else:
        filename = random_bytes_str(24) + ".png"
        img.thumbnail(resolution, Image.ANTIALIAS)
        img.save(dst + filename)
    file_key = "upload-image-" + random_bytes_str(16)
    rdb.set(file_key, filename)
    log('fe_upload_image',
        obj={
            'filename': filename,
            'file_key': file_key,
            'size': len(content)
        })
    return web.json_response(makeResponseSuccess({'file_key': file_key}),
                             dumps=dumps)
Ejemplo n.º 2
0
async def upload_image_url(request):
    data = await request.json()
    try:
        url = data['url']
        if data['type'] == 'cover':
            resolution = (320, 200)
            dst = _COVER_PATH
        elif data['type'] == 'userphoto':
            resolution = (256, 256)
            dst = _USERPHOTO_PATH
        else:
            return web.json_response(
                makeResponseFailed('INCORRECT_UPLOAD_TYPE'), dumps=dumps)
    except:
        return web.json_response(makeResponseFailed('INCORRECT_REQUEST'),
                                 dumps=dumps)
    async with ClientSession() as session:
        async with session.get(url) as resp:
            if resp.status == 200:
                try:
                    img = Image.open(io.BytesIO(await resp.read()))
                    if isinstance(img, PIL.GifImagePlugin.GifImageFile):
                        filename = random_bytes_str(24) + ".gif"
                        frames = ImageSequence.Iterator(img)
                        frames = _gif_thumbnails(frames, resolution)
                        om = next(frames)  # Handle first frame separately
                        om.info = img.info  # Copy sequence info
                        om.save(dst + filename,
                                save_all=True,
                                append_images=list(frames),
                                loop=0)
                    else:
                        filename = random_bytes_str(24) + ".png"
                        img.thumbnail(resolution, Image.ANTIALIAS)
                        img.save(dst + filename)
                except:
                    return web.json_response(
                        makeResponseFailed('INCORRECT_UPLOAD_TYPE'),
                        dumps=dumps)
            else:
                return web.json_response(
                    makeResponseFailed('INCORRECT_UPLOAD_TYPE'), dumps=dumps)
    file_key = "upload-image-" + random_bytes_str(16)
    rdb.set(file_key, filename)
    log('fe_upload_image_url',
        obj={
            'filename': filename,
            'file_key': file_key
        })
    return web.json_response(makeResponseSuccess({'file_key': file_key}),
                             dumps=dumps)
Ejemplo n.º 3
0
async def _download_thumbnail(url, user, event_id):
    filename = ""
    if url:
        for attemp in range(3):
            try:
                async with _download_sem:
                    async with ClientSession() as session:
                        async with session.get(url) as resp:
                            if resp.status == 200:
                                img = Image.open(io.BytesIO(await resp.read()))
                                if isinstance(img,
                                              PIL.GifImagePlugin.GifImageFile):
                                    filename = random_bytes_str(24) + ".gif"
                                    frames = ImageSequence.Iterator(img)
                                    frames = _gif_thumbnails(frames)
                                    om = next(
                                        frames
                                    )  # Handle first frame separately
                                    om.info = img.info  # Copy sequence info
                                    om.save(_COVER_PATH + filename,
                                            save_all=True,
                                            append_images=list(frames),
                                            loop=0)
                                else:
                                    filename = random_bytes_str(24) + ".png"
                                    img.thumbnail((320, 200), Image.ANTIALIAS)
                                    img.save(_COVER_PATH + filename)
                                log_e(event_id,
                                      user,
                                      'download_cover',
                                      obj={'filename': filename})
                                break
                            else:
                                log_e(event_id, user, 'download_cover', 'WARN',
                                      {
                                          'status_code': resp.status,
                                          'attemp': attemp
                                      })
            except Exception as ex:
                log_e(event_id, user, 'download_cover', 'WARN', {
                    'ex': str(ex),
                    'attemp': attemp
                })
                continue
    return filename
Ejemplo n.º 4
0
async def put_task(queue, param_json):
    task_id = random_bytes_str(16)
    param_json_for_user = copy.deepcopy(param_json)
    del param_json_for_user['user']
    del param_json_for_user['event_id']
    log_e(param_json['event_id'],
          param_json['user'],
          op='put_task',
          obj={'task_id': task_id})
    ret_json = dumps({
        'finished': False,
        'key': task_id,
        'data': None,
        'params': param_json_for_user
    })
    rdb.set(f'task-playlist-{task_id}', ret_json)
    key = 'post-playlist-tasks-' + str(param_json['user']['_id'])
    rdb.lpush(key, task_id)
    await queue.put((param_json, task_id))
    return task_id