Skip to main content

FileSystemStorage

Standard filesystem storage

Attributes

AttributeTypeDescription
base_locationstringThe raw directory path where files are stored, defaulting to the MEDIA_ROOT setting if not provided.
locationstringThe absolute file system path to the storage directory used for joining file names.
base_urlstringThe base URL that serves the files from this storage, ensuring it ends with a trailing slash.
file_permissions_modeintegerThe numeric mode used to set permissions on saved files, falling back to the FILE_UPLOAD_PERMISSIONS setting.
directory_permissions_modeintegerThe numeric mode used when creating intermediate directories, falling back to the FILE_UPLOAD_DIRECTORY_PERMISSIONS setting.

Constructor

Signature

def FileSystemStorage(
location: string = None,
base_url: string = None,
file_permissions_mode: int = None,
directory_permissions_mode: int = None,
allow_overwrite: boolean = False
) - > null

Parameters

NameTypeDescription
locationstring = NoneThe absolute path to the directory that will hold the files.
base_urlstring = NoneThe URL that serves the files stored at this location.
file_permissions_modeint = NoneThe file system permissions that the file will receive.
directory_permissions_modeint = NoneThe file system permissions that the directory will receive.
allow_overwriteboolean = FalseA boolean indicating whether to overwrite existing files with the same name.

Methods


base_location()

@classmethod
def base_location() - > string

Retrieves the raw base path for storage, defaulting to the MEDIA_ROOT setting if no specific location was provided.

Returns

TypeDescription
stringThe configured base directory path before absolute path resolution

location()

@classmethod
def location() - > string

Provides the absolute file system path to the storage root directory.

Returns

TypeDescription
stringThe normalized absolute path of the storage location

base_url()

@classmethod
def base_url() - > string

Retrieves the base URL used to serve files, ensuring it ends with a trailing slash for consistent URL joining.

Returns

TypeDescription
stringThe root URL for file access, defaulting to the MEDIA_URL setting

file_permissions_mode()

@classmethod
def file_permissions_mode() - > integer

Determines the numeric mode used for setting permissions on newly created files.

Returns

TypeDescription
integerThe file permission bitmask, such as 0o644

directory_permissions_mode()

@classmethod
def directory_permissions_mode() - > integer

Determines the numeric mode used for setting permissions on newly created directories.

Returns

TypeDescription
integerThe directory permission bitmask, such as 0o755

delete()

@classmethod
def delete(
name: string
)

Removes the specified file or empty directory from the filesystem.

Parameters

NameTypeDescription
namestringThe relative path of the file or directory to be deleted

is_name_available()

@classmethod
def is_name_available(
name: string,
max_length: integer = null
) - > boolean

Checks if a filename can be used, bypassing existence checks if allow_overwrite is enabled.

Parameters

NameTypeDescription
namestringThe filename to check for availability
max_lengthinteger = nullThe maximum allowed length for the filename

Returns

TypeDescription
booleanTrue if the name is available or overwriting is permitted, False otherwise

get_alternative_name()

@classmethod
def get_alternative_name(
file_root: string,
file_ext: string
) - > string

Generates a variation of a filename to avoid collisions, or returns the original if overwriting is allowed.

Parameters

NameTypeDescription
file_rootstringThe filename without the extension
file_extstringThe file extension including the leading dot

Returns

TypeDescription
stringA unique filename or the original filename if overwriting is enabled

exists()

@classmethod
def exists(
name: string
) - > boolean

Checks whether the specified path exists on the filesystem, including broken symbolic links.

Parameters

NameTypeDescription
namestringThe relative path to check for existence

Returns

TypeDescription
booleanTrue if the path exists, False otherwise

listdir()

@classmethod
def listdir(
path: string
) - > tuple

Lists the contents of the specified directory path.

Parameters

NameTypeDescription
pathstringThe relative directory path to inspect

Returns

TypeDescription
tupleA tuple containing two lists: (directories, files) found at the path

path()

@classmethod
def path(
name: string
) - > string

Converts a relative name into an absolute filesystem path while preventing directory traversal attacks.

Parameters

NameTypeDescription
namestringThe relative path to be joined with the storage root

Returns

TypeDescription
stringThe safe, absolute filesystem path

size()

@classmethod
def size(
name: string
) - > integer

Retrieves the total size of the specified file in bytes.

Parameters

NameTypeDescription
namestringThe relative path of the file to measure

Returns

TypeDescription
integerThe file size in bytes

url()

@classmethod
def url(
name: string
) - > string

Constructs an absolute URL for accessing the file via a web browser.

Parameters

NameTypeDescription
namestringThe relative path of the file to generate a URL for

Returns

TypeDescription
stringThe full URL to the file

get_accessed_time()

@classmethod
def get_accessed_time(
name: string
) - > datetime

Retrieves the last access time of the specified file.

Parameters

NameTypeDescription
namestringThe relative path of the file

Returns

TypeDescription
datetimeThe datetime when the file was last accessed

get_created_time()

@classmethod
def get_created_time(
name: string
) - > datetime

Retrieves the creation time of the specified file.

Parameters

NameTypeDescription
namestringThe relative path of the file

Returns

TypeDescription
datetimeThe datetime when the file was created

get_modified_time()

@classmethod
def get_modified_time(
name: string
) - > datetime

Retrieves the last modification time of the specified file.

Parameters

NameTypeDescription
namestringThe relative path of the file

Returns

TypeDescription
datetimeThe datetime when the file content was last changed