API - move file utils

This commit is contained in:
Sam 2022-02-16 12:55:55 +01:00
parent 10a9ff5e28
commit 9700b98af1
8 changed files with 29 additions and 27 deletions

23
fittrackee/files.py Normal file
View File

@ -0,0 +1,23 @@
import os
from typing import Union
from flask import current_app
def display_readable_file_size(size_in_bytes: Union[float, int]) -> str:
"""
Return readable file size from size in bytes
"""
if size_in_bytes == 0:
return '0 bytes'
if size_in_bytes == 1:
return '1 byte'
for unit in [' bytes', 'KB', 'MB', 'GB', 'TB']:
if abs(size_in_bytes) < 1024.0:
return f'{size_in_bytes:3.1f}{unit}'
size_in_bytes /= 1024.0
return f'{size_in_bytes} bytes'
def get_absolute_file_path(relative_path: str) -> str:
return os.path.join(current_app.config['UPLOAD_FOLDER'], relative_path)

View File

@ -5,27 +5,13 @@ from flask import Response
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from fittrackee import appLog from fittrackee import appLog
from fittrackee.files import display_readable_file_size
def get_empty_data_for_datatype(data_type: str) -> Union[str, List]: def get_empty_data_for_datatype(data_type: str) -> Union[str, List]:
return '' if data_type in ['gpx', 'chart_data'] else [] return '' if data_type in ['gpx', 'chart_data'] else []
def display_readable_file_size(size_in_bytes: Union[float, int]) -> str:
"""
Return readable file size from size in bytes
"""
if size_in_bytes == 0:
return '0 bytes'
if size_in_bytes == 1:
return '1 byte'
for unit in [' bytes', 'KB', 'MB', 'GB', 'TB']:
if abs(size_in_bytes) < 1024.0:
return f'{size_in_bytes:3.1f}{unit}'
size_in_bytes /= 1024.0
return f'{size_in_bytes} bytes'
class HttpResponse(Response): class HttpResponse(Response):
def __init__( def __init__(
self, self,

View File

@ -3,7 +3,7 @@ from uuid import uuid4
import pytest import pytest
from fittrackee.responses import display_readable_file_size from fittrackee.files import display_readable_file_size
from fittrackee.utils import get_readable_duration from fittrackee.utils import get_readable_duration

View File

@ -10,6 +10,7 @@ from werkzeug.exceptions import RequestEntityTooLarge
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from fittrackee import appLog, bcrypt, db from fittrackee import appLog, bcrypt, db
from fittrackee.files import get_absolute_file_path
from fittrackee.responses import ( from fittrackee.responses import (
ForbiddenErrorResponse, ForbiddenErrorResponse,
HttpResponse, HttpResponse,
@ -22,7 +23,6 @@ from fittrackee.responses import (
from fittrackee.tasks import reset_password_email from fittrackee.tasks import reset_password_email
from fittrackee.utils import get_readable_duration, verify_extension_and_size from fittrackee.utils import get_readable_duration, verify_extension_and_size
from fittrackee.workouts.models import Sport from fittrackee.workouts.models import Sport
from fittrackee.workouts.utils_files import get_absolute_file_path
from .decorators import authenticate from .decorators import authenticate
from .models import User, UserSportPreference from .models import User, UserSportPreference

View File

@ -7,6 +7,7 @@ from flask import Blueprint, request, send_file
from sqlalchemy import exc from sqlalchemy import exc
from fittrackee import db from fittrackee import db
from fittrackee.files import get_absolute_file_path
from fittrackee.responses import ( from fittrackee.responses import (
ForbiddenErrorResponse, ForbiddenErrorResponse,
HttpResponse, HttpResponse,
@ -16,7 +17,6 @@ from fittrackee.responses import (
handle_error_and_return_response, handle_error_and_return_response,
) )
from fittrackee.workouts.models import Record, Workout, WorkoutSegment from fittrackee.workouts.models import Record, Workout, WorkoutSegment
from fittrackee.workouts.utils_files import get_absolute_file_path
from .decorators import authenticate, authenticate_as_admin from .decorators import authenticate, authenticate_as_admin
from .exceptions import UserNotFoundException from .exceptions import UserNotFoundException

View File

@ -13,8 +13,8 @@ from sqlalchemy.orm.session import Session, object_session
from sqlalchemy.types import JSON, Enum from sqlalchemy.types import JSON, Enum
from fittrackee import db from fittrackee import db
from fittrackee.files import get_absolute_file_path
from .utils_files import get_absolute_file_path
from .utils_format import convert_in_duration, convert_value_to_integer from .utils_format import convert_in_duration, convert_value_to_integer
from .utils_id import encode_uuid from .utils_id import encode_uuid

View File

@ -15,11 +15,11 @@ from werkzeug.datastructures import FileStorage
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from fittrackee import db from fittrackee import db
from fittrackee.files import get_absolute_file_path
from fittrackee.users.models import User, UserSportPreference from fittrackee.users.models import User, UserSportPreference
from .exceptions import WorkoutException from .exceptions import WorkoutException
from .models import Sport, Workout, WorkoutSegment from .models import Sport, Workout, WorkoutSegment
from .utils_files import get_absolute_file_path
from .utils_gpx import get_gpx_info from .utils_gpx import get_gpx_info

View File

@ -1,7 +0,0 @@
import os
from flask import current_app
def get_absolute_file_path(relative_path: str) -> str:
return os.path.join(current_app.config['UPLOAD_FOLDER'], relative_path)