MantisBase v0.4.9
Loading...
Searching...
No Matches
mb::MbFilesMgr Class Reference

File management for entity file assets. More...

#include <files.h>

Inheritance diagram for mb::MbFilesMgr:
Collaboration diagram for mb::MbFilesMgr:

Public Member Functions

 MbFilesMgr (const MantisBase &app)
void createDir (const std::string &entity_name) const
 Create a directory for the given entity.
void renameDir (const std::string &old_entity_name, const std::string &new_entity_name) const
 Rename an entity directory.
void deleteDir (const std::string &entity_name) const
 Delete an entity directory and all its contents.
std::string dirPath (const std::string &entity_name, bool create_if_missing=false) const
 Get the directory path for an entity.
std::string filePath (const std::string &entity_name, const std::string &filename) const
 Get the absolute file path for an entity and filename.
std::optional< std::string > getFilePath (const std::string &entity_name, const std::string &filename)
 Get file path only if the file exists.
bool removeFile (const std::string &entity_name, const std::string &filename) const
 Remove a file from an entity's directory.
void removeFiles (const std::string &entity_name, const std::vector< std::string > &files) const
 Remove multiple files from an entity's directory.
bool fileExists (const std::string &entity_name, const std::string &filename) const
 Check if a file exists in an entity's directory.
fs::path getCanonicalPath (const fs::path &path) const
 Get canonical path and verify it's within the files base directory.
bool isCanonicalPath (const fs::path &path) const
 Check if a path is canonical and within the files base directory.
fs::path filesBaseDir () const
 Get the base directory for all entity files.
Public Member Functions inherited from mb::MbAppMixin
 MbAppMixin (const MantisBase &app)
 Constrictor for instantiating this interface.
 MbAppMixin (MbAppMixin &&)=default
MbAppMixin & operator= (MbAppMixin &&)=delete
 MbAppMixin (const MbAppMixin &)=default
MbAppMixin & operator= (const MbAppMixin &)=delete
const MantisBase & mbApp () const
 Application that owns this service or request context.
const MbLogger & logger () const

Detailed Description

File management for entity file assets.

Handles file storage and retrieval for entities with file or files field types. Files are stored on disk in entity-specific directories, with filenames saved to the database.

Security features:

  • MbEntity name validation to prevent SQL injection
  • Path canonicalization to prevent directory traversal attacks
  • Automatic sanitization of filenames

The class handles:

  • Creating and deleting entity directories
  • Creating and deleting file resources
  • Updating folder names when entities are renamed
  • Deleting folder contents when entities are dropped
// Create directory for an entity
Files::createDir("products");
// Get file path (validates entity name and prevents path traversal)
std::string path = Files::filePath("products", "image.jpg");
// Check if file exists
if (Files::fileExists("products", "image.jpg")) {
// File exists
}

Constructor & Destructor Documentation

◆ MbFilesMgr()

mb::MbFilesMgr::MbFilesMgr ( const MantisBase & app)
explicit

Member Function Documentation

◆ createDir()

void mb::MbFilesMgr::createDir ( const std::string & entity_name) const

Create a directory for the given entity.

Validates the entity name and creates the directory if it doesn't exist. Throws MbException if entity name is invalid.

Parameters
entity_nameMbEntity/table name (must be valid alphanumeric + underscore)
Exceptions
MbExceptionif entity name is invalid

◆ deleteDir()

void mb::MbFilesMgr::deleteDir ( const std::string & entity_name) const

Delete an entity directory and all its contents.

Parameters
entity_nameMbEntity/table name

◆ dirPath()

std::string mb::MbFilesMgr::dirPath ( const std::string & entity_name,
bool create_if_missing = false ) const
nodiscard

Get the directory path for an entity.

Returns the absolute path to the entity's file directory. Validates entity name and checks for path traversal attacks before returning the path.

Parameters
entity_nameMbEntity/table name (must be valid)
create_if_missingIf true, creates the directory if it doesn't exist
Returns
Absolute path string to the entity's file directory
Exceptions
MbExceptionif entity name is invalid or path traversal is detected

◆ fileExists()

bool mb::MbFilesMgr::fileExists ( const std::string & entity_name,
const std::string & filename ) const
nodiscard

Check if a file exists in an entity's directory.

Parameters
entity_nameMbEntity/table name
filenameFile name to check
Returns
true if file exists, false otherwise

◆ filePath()

std::string mb::MbFilesMgr::filePath ( const std::string & entity_name,
const std::string & filename ) const
nodiscard

Get the absolute file path for an entity and filename.

Constructs and canonicalizes the file path, ensuring it's within the entity's directory to prevent path traversal attacks. Validates entity name and filename.

Parameters
entity_nameMbEntity/table name (must be valid)
filenameFile name (must not be empty)
Returns
Absolute canonical path string to the file
Exceptions
MbExceptionif entity name is invalid, filename is empty, or path traversal is detected

◆ filesBaseDir()

fs::path mb::MbFilesMgr::filesBaseDir ( ) const
nodiscard

Get the base directory for all entity files.

Returns the path to the files directory (typically <dataDir>/files).

Returns
Filesystem path to the base files directory

◆ getCanonicalPath()

fs::path mb::MbFilesMgr::getCanonicalPath ( const fs::path & path) const
nodiscard

Get canonical path and verify it's within the files base directory.

Canonicalizes the given path (resolves symlinks, removes "..", etc.) and verifies that it's within the files base directory to prevent path traversal attacks.

Parameters
pathPath to canonicalize and validate
Returns
Canonical path if valid
Exceptions
MbExceptionif path traversal is detected (400 status)

◆ getFilePath()

std::optional< std::string > mb::MbFilesMgr::getFilePath ( const std::string & entity_name,
const std::string & filename )

Get file path only if the file exists.

Returns the file path if the file exists on disk, otherwise returns std::nullopt. Validates entity name before checking file existence.

Parameters
entity_nameMbEntity/table name
filenameFile name
Returns
Absolute file path if file exists, std::nullopt otherwise
Exceptions
MbExceptionif entity name is invalid

◆ isCanonicalPath()

bool mb::MbFilesMgr::isCanonicalPath ( const fs::path & path) const
nodiscard

Check if a path is canonical and within the files base directory.

Canonicalizes the path and checks if it's within the files base directory without throwing an exception.

Parameters
pathPath to check
Returns
true if path is canonical and within base directory, false otherwise

◆ removeFile()

bool mb::MbFilesMgr::removeFile ( const std::string & entity_name,
const std::string & filename ) const
nodiscard

Remove a file from an entity's directory.

Parameters
entity_nameMbEntity/table name
filenameFile name to remove
Returns
true if file was successfully removed, false otherwise

◆ removeFiles()

void mb::MbFilesMgr::removeFiles ( const std::string & entity_name,
const std::vector< std::string > & files ) const

Remove multiple files from an entity's directory.

Parameters
entity_nameMbEntity/table name
filesVector of file names to remove

◆ renameDir()

void mb::MbFilesMgr::renameDir ( const std::string & old_entity_name,
const std::string & new_entity_name ) const

Rename an entity directory.

Renames the directory if it exists, otherwise creates a new one. Validates both old and new entity names.

Parameters
old_entity_nameCurrent entity name
new_entity_nameNew entity name
Exceptions
MbExceptionif either entity name is invalid

The documentation for this class was generated from the following files:
  • /home/runner/work/mantisbase/mantisbase/include/mantisbase/core/files.h
  • /home/runner/work/mantisbase/mantisbase/src/core/files.cpp