예제 #1
0
async def settings_avatar_post():
    # if not authenticated; render login
    if not 'authenticated' in session:
        return await flash('error', 'You must be logged in to access avatar settings!', 'login')

    # constants
    AVATARS_PATH = f'{glob.config.path_to_gulag}.data/avatars'
    ALLOWED_EXTENSIONS = ['.jpeg', '.jpg', '.png']

    # form data
    avatar = (await request.files).get('avatar')
    filename, file_extension = os.path.splitext(avatar.filename.lower())

    # no file uploaded; deny post
    if not avatar or filename == '':
        return await flash('error', 'No image was selected!', 'settings/avatar')

    # bad file extension; deny post
    if not file_extension in ALLOWED_EXTENSIONS:
        return await flash('error', 'The image you select must be either a .JPG, .JPEG, or .PNG file!', 'settings/avatar')

    # remove old avatars
    tasks = [aos.remove(f'{AVATARS_PATH}/{session["user_data"]["id"]}{fx}') for fx in ALLOWED_EXTENSIONS]
    await asyncio.gather(*tasks, return_exceptions=True)

    # avatar change success
    avatar.save(os.path.join(AVATARS_PATH, f'{session["user_data"]["id"]}{file_extension.lower()}'))
    return await flash('success', 'Your avatar has been successfully changed!', 'settings/avatar')
예제 #2
0
def create_files(
        conjugations,
        file_type: ResponseType = Query("docx", alias="file-type"),
        tiers: List[Tier] = None,
        settings: FileSettings = FileSettings(),
):
    if file_type == "docx":
        document = DocxFile(conjugations, tiers, settings)
        path = document.write_to_temp()
        media_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"  # noqa: E501
        fn = "conjugations.docx"

    if file_type == "csv":
        document = CsvFile(conjugations, tiers, settings)
        path = document.write_to_temp()
        media_type = "text/csv"
        fn = "conjugations.csv"

    if file_type == "latex":
        document = LatexFile(conjugations, tiers, settings)
        path = document.write_to_temp()
        media_type = "text/plain"
        fn = "conjugations.tex"

    # TODO: LaTeX is not creating a PDF properly and causing lots of errors
    # if file_type == 'pdf':
    #     document = LatexFile(conjugations, tiers, settings)
    #     path = document.write_to_temp(ftype='pdf')
    #     media_type = "application/pdf"
    #     fn = "conjugations.pdf"

    try:
        headers = {"Content-Disposition": f'attachment; filename="{fn}"'}
        response = FileResponse(path=path,
                                filename=fn,
                                media_type=media_type,
                                headers=headers)
        return response
    finally:
        aos.remove(path)
예제 #3
0
    # Scale it
    await await_ffmpeg(
        functools.partial(generate_user_thumbnail, temp_name, output_name))

    # Upload thumbnail, delete original, delete old thumbnail in s3
    profile_pic_path = f'{user.name}/profile/{output_name}'
    async with aiofiles.open(temp_name, 'rb+') as thumbnail_img:
        await boto_session.put_object(
            Bucket=settings.S3_BUCKET_URL,
            Key=profile_pic_path,
            Body=await thumbnail_img.read(),
            ACL='public-read',
        )

    # Cleanup
    remove_original = asyncio.create_task(os.remove(temp_name))
    remove_thumbnail = asyncio.create_task(os.remove(output_name))
    if user.thumbnail_uri:
        delete_old_s3_thumbnail = asyncio.create_task(
            boto_session.delete_object(
                Bucket=settings.S3_BUCKET_URL,
                Key=user.thumbnail_uri.split('https://gg.klepp.me/')[1]))
    await asyncio.gather(remove_original, remove_thumbnail,
                         delete_old_s3_thumbnail)

    user.thumbnail_uri = f'https://gg.klepp.me/{profile_pic_path}'
    db_session.add(user)
    await db_session.commit()
    await db_session.refresh(user)
    return user.dict()
예제 #4
0
            # passing the `generate_video_thumbnail` function with the arguments temp_vido_name, temp_thumbnail_name
            functools.partial(generate_video_thumbnail, temp_vido_name,
                              temp_thumbnail_name)))
    await asyncio.gather(upload_task, ffmpeg_task)

    # Upload thumbnail and clean up
    async with aiofiles.open(temp_thumbnail_name, 'rb+') as thumbnail_img:
        await boto_session.put_object(
            Bucket=settings.S3_BUCKET_URL,
            Key=s3_path.replace('.mp4', '.png'),
            Body=await thumbnail_img.read(),
            ACL='public-read',
        )

    # Cleanup
    remove_video = asyncio.create_task(os.remove(f'{temp_name}.mp4'))
    remove_thumbnail = asyncio.create_task(os.remove(f'{temp_name}.png'))
    await asyncio.gather(remove_video, remove_thumbnail)

    # Add to DB and fetch it
    db_video: Video = Video(
        path=s3_path,
        display_name=upload_file_name.split('.mp4')[0],
        user=user,
        user_id=user.id,
        uri=video_uri,
        thumbnail_uri=thumbnail_uri,
    )
    db_session.add(db_video)
    await db_session.commit()
    return await fetch_one_or_none_video(video_path=db_video.path,