file_utils.py

About this file

The file contains functions pertaining to file information, such as getting the size and checking if the file exists. Other file functions including renaming, moving, and removing the file, as well as getting the hash and mime type. The send_file function sends the file from the specified directory.

The 'sftp_ctx()' function is the context manager that provides an SFTP client object.
The '_sftp_switch(sftp_func)' function checks if app is using SFTP. If it is using then app connects to SFTP server and passed function (sftp_func) is called with connected client. Else the 'os_func(*args, **kwargs)' which is os library to accomplish the same file-related action.

The '_get_file_serving_path(directory, filename)' help in returning the upload serving directory path for a file determined by supplied directory and filename.

Function 'move(oldpath, newpath)' is used instead of 'rename' if, when using sftp, 'oldpath' represents a local file path and 'newpath' a remote path.

Function 'get_hash(path)' returns the sha1 hash of a file a string of hexadecimal digits.

Code Issues

  • Use of MD2, MD4, MD5, or SHA1 hash function is comparably insecure. This can be avoided using SHA2 or SHA3 instead.

Code Check Report


No problems to report
                        

Documentation drawn from source code


app.file.utils
~~~~~~~~~~~~~~~~

synopsis: Helpers for manipulating files.
Switches file-handling interface between sftp and os depending on configuration.


MaxTransferSizeExceededException(Exception):

SFTPCredentialsException(Exception):

sftp_ctx():

Context manager that provides an SFTP client object
(an SFTP session across an open SSH Transport)

_sftp_switch(sftp_func):

Check if app is using SFTP and, if so, connect to SFTP server
and call passed function (sftp_func) with connected client,
otherwise call decorated function (which should be using
the os library to accomplish the same file-related action).

decorator(os_func):

wrapper(*args, **kwargs):

_raise_if_too_big(bytes_transferred, _):

_sftp_get_size(sftp, path):

_sftp_exists(sftp, path):

_sftp_mkdir(sftp, path):

_sftp_makedirs(sftp, path):
os.makedirs(path, exists_ok=True)

_sftp_remove(sftp, path):

_sftp_rename(sftp, oldpath, newpath):

_sftp_move(sftp, localpath, remotepath):

_sftp_get_mime_type(sftp, path):

_sftp_get_hash(sftp, path):

_sftp_send_file(sftp, directory, filename, **kwargs):

_get_file_serving_path(directory, filename):

Returns the upload serving directory path for a file determined by supplied directory and filename.

getsize(path):

exists(path):

mkdir(path):

makedirs(path, **kwargs):

remove(path):

rename(oldpath, newpath):

move(oldpath, newpath):

Use this instead of 'rename' if, when using sftp, 'oldpath
represents a local file path and 'newpath' a remote path.

get_mime_type(path):

os_get_mime_type(path):

get_hash(path):

Returns the sha1 hash of a file a string of
hexadecimal digits.

os_get_hash(path):

send_file(directory, filename, **kwargs):

Source code