flask_file_share.web package

Initialization module for the File Sharing Flask application.

Description:

This module initializes the Flask application and imports routes and configuration settings.

Usage:

This module is the entry point of the application. It sets up the Flask app, imports routes, and configuration settings, and runs the application if executed directly.

Submodules

File: auth.py

Authentication module for the File Sharing Flask application.

Functions:
  • api_login_required(func): Decorator to require API login for a route.

  • clear_session(): Clear user session.

  • is_logged_in(): Check if user is logged in.

  • login_required(func): Decorator to require login for a route.

  • successful_login_redirect(): Redirect user after successful login.

  • validate_credentials(username: str, password: str) -> bool: Validate user credentials.

Usage:

This module provides authentication-related functionality such as login validation, session management, and decorators for requiring login for certain routes.

flask_file_share.web.auth.api_login_required(func)

Decorator to check if API user is logged in.

Parameters:

func (function) – The function to be decorated.

Returns:

Decorated function.

Return type:

function

flask_file_share.web.auth.clear_session()

Clear the user session.

flask_file_share.web.auth.is_logged_in() bool

Check if the user is logged in.

Returns:

True if the user is logged in, False otherwise.

Return type:

bool

flask_file_share.web.auth.login_required(func)

Decorator to check if user is logged in.

Parameters:

func (function) – The function to be decorated.

Returns:

Decorated function.

Return type:

function

flask_file_share.web.auth.successful_login_redirect() Response

Redirect user after successful login.

Returns:

Flask response for redirecting to the previous URL or the home page.

Return type:

Wk_Response

flask_file_share.web.auth.validate_credentials(username: str, password: str) bool

Validate user credentials.

Parameters:
  • username (str) – Username provided by the user.

  • password (str) – Password provided by the user.

Returns:

True if the credentials are valid, False otherwise.

Return type:

bool

File: config.py

Configuration module for the File Sharing Flask application.

Description:

This module contains configuration settings for the Flask application, including upload folder, static folder, and other application-specific configurations.

Usage:

The configuration settings defined in this module are imported and used throughout the application to configure Flask app, such as setting upload folder, static folder, and other Flask app configurations.

File: routes.py

Route definitions for the File Sharing Flask application.

Functions:
  • index(): Render the index page.

  • api_index(): API endpoint to retrieve file information.

  • login(): Handle user login.

  • api_login(): Handle API user login.

  • logout(): Handle user logout.

  • api_logout(): Handle API user logout.

  • upload(): Handle file upload.

  • api_upload(): Handle API file upload.

  • download(filename: str): Handle file download.

  • api_download(filename: str): Handle API file download.

  • api_last_n_files_download(nb_files: int): Handle API download of last n files.

  • open_file(filename: str): Open a file.

  • raw_file(filename: str): Return raw file content.

  • static_files(filename: str): Serve static files.

  • add_header(response: Response): Add headers to HTTP response.

Usage:

The routes are accessed through HTTP requests to corresponding endpoints.

Example

Accessing the endpoint “/” renders the index page.

flask_file_share.web.routes.add_header(response: Response)

Add headers to tell the browser not to cache the rendered page.

Note

If we wanted to we could change max-age to 600 seconds which would be 10 minutes.

Parameters:

response (Response) – HTTP response.

Returns:

HTTP response with added headers.

Return type:

Response

flask_file_share.web.routes.api_download(filename: str)

Handle API file download.

Parameters:

filename (str) – Name of the file to download.

Returns:

File download response.

Return type:

Response

flask_file_share.web.routes.api_index()

API endpoint to retrieve file information.

Returns:

JSON response containing file information.

Return type:

str

flask_file_share.web.routes.api_last_n_files_download(nb_files: int)

Handle API download of last n files.

Parameters:

nb_files (int) – Number of files to download.

Returns:

ZIP archive containing the requested files.

Return type:

Response

flask_file_share.web.routes.api_login()

Handle API user login.

Returns:

JSON response indicating login status.

Return type:

str

flask_file_share.web.routes.api_logout()

Handle API user logout.

Returns:

JSON response indicating logout status.

Return type:

str

flask_file_share.web.routes.api_upload()

Handle API file upload.

Returns:

JSON response indicating file upload status.

Return type:

str

flask_file_share.web.routes.download(filename: str)

Handle file download.

Parameters:

filename (str) – Name of the file to download.

Returns:

File download response.

Return type:

Response

flask_file_share.web.routes.index()

Render the index page.

Returns:

The rendered index page.

Return type:

str

flask_file_share.web.routes.login()

Handle user login.

Returns:

Rendered login page or redirect to index page.

Return type:

str

flask_file_share.web.routes.logout()

Handle user logout.

Returns:

Redirects to the login page.

Return type:

redirect

flask_file_share.web.routes.open_file(filename: str)

Open a file.

Parameters:

filename (str) – Name of the file to open.

Returns:

File content response.

Return type:

Response

flask_file_share.web.routes.raw_file(filename: str)

Return raw file content.

Parameters:

filename (str) – Name of the file.

Returns:

Raw file content.

Return type:

bytes

flask_file_share.web.routes.static_files(filename: str)

Serve static files.

Parameters:

filename (str) – Name of the static file.

Returns:

Static file response.

Return type:

Response

flask_file_share.web.routes.upload()

Handle file upload.

Returns:

Redirects to the index page.

Return type:

redirect

File: utils.py

Utility functions for the File Sharing Flask application.

Functions:

  • load_data_from_json(): Load data from the JSON file.

  • get_files_with_dates(): Retrieve files with their modification dates.

  • update_data_file(filename: str): Update the data file with new file information.

  • handle_file_saving(file: FileStorage) -> str: Handle saving of uploaded files.

  • get_last_n_files(nb_files: int) -> List[str]: Get the last n files.

  • get_content_type(file_path: str) -> Union[str, None]: Get content type based on file extension.

  • create_sharefile_zip_archive(filepaths: List[str], output_fname: Optional[str] = None) -> Tuple[str, bytes]: Create a ZIP archive containing specified files.

Usage:

These utility functions are used across the application to perform various tasks such as loading data, handling file uploads, retrieving file information, and creating ZIP archives.

flask_file_share.web.utils.create_sharefile_zip_archive(filepaths: List[str], output_fname: str | None = None) Tuple[str, bytes]

Create a ZIP archive containing specified files.

Parameters:
  • filepaths (list) – List of files to be included in the ZIP archive.

  • output_fname (str, optional) – Name of the ZIP archive. If not provided,

  • generated. (a unique filename will be) –

Returns:

A tuple containing the filename of the ZIP archive and its content as bytes.

Return type:

tuple

flask_file_share.web.utils.get_content_type(file_path: str) str | None

Get content type based on file extension.

Parameters:

file_path (str) – Path to the file.

Returns:

Content type of the file.

Return type:

str

flask_file_share.web.utils.get_files_with_dates() List[Tuple]

Retrieve files with their modification dates.

Returns:

List of tuples containing filename and modification dates.

Return type:

list

flask_file_share.web.utils.get_last_n_files(nb_files: int) List[str]

Get the last n files.

Parameters:

nb_files (int) – Number of files to retrieve.

Returns:

List of filenames.

Return type:

list

flask_file_share.web.utils.handle_file_saving(file: FileStorage) str

Handle saving of uploaded files.

Parameters:

file (FileStorage) – The uploaded file.

Returns:

Filename of the saved file.

Return type:

str

flask_file_share.web.utils.load_data_from_json() Dict[str, str]

Load data from the JSON file.

Returns:

Loaded data from the JSON file.

Return type:

dict

flask_file_share.web.utils.update_data_file(filename: str)

Update the data file with new file information.

Parameters:

filename (str) – Name of the file to update.

File: views.py

View rendering module for the File Sharing Flask application.

Functions:
  • render_index_page(files: List[Tuple]): Render the index page.

  • render_login_page(): Render the login page.

Usage:

This module provides functions to render HTML pages for the File Sharing Flask application, including the index page and the login page.

flask_file_share.web.views.render_index_page(files: List) str

Render the index page.

Parameters:

files (List) – List of files to display on the index page.

Returns:

The rendered index page.

Return type:

str

flask_file_share.web.views.render_login_page() str

Render the default login page.

Returns:

The rendered login page.

Return type:

str