API - move file utils
This commit is contained in:
parent
10a9ff5e28
commit
9700b98af1
23
fittrackee/files.py
Normal file
23
fittrackee/files.py
Normal 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)
|
@ -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,
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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)
|
|
Loading…
Reference in New Issue
Block a user