![]() |
MantisBase v0.4.9
|
File management for entity file assets. More...
#include <files.h>


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 |
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:
The class handles:
|
explicit |
| 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.
| entity_name | MbEntity/table name (must be valid alphanumeric + underscore) |
| MbException | if entity name is invalid |
| void mb::MbFilesMgr::deleteDir | ( | const std::string & | entity_name | ) | const |
Delete an entity directory and all its contents.
| entity_name | MbEntity/table name |
|
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.
| entity_name | MbEntity/table name (must be valid) |
| create_if_missing | If true, creates the directory if it doesn't exist |
| MbException | if entity name is invalid or path traversal is detected |
|
nodiscard |
Check if a file exists in an entity's directory.
| entity_name | MbEntity/table name |
| filename | File name to check |
|
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.
| entity_name | MbEntity/table name (must be valid) |
| filename | File name (must not be empty) |
| MbException | if entity name is invalid, filename is empty, or path traversal is detected |
|
nodiscard |
Get the base directory for all entity files.
Returns the path to the files directory (typically <dataDir>/files).
|
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.
| path | Path to canonicalize and validate |
| MbException | if path traversal is detected (400 status) |
| 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.
| entity_name | MbEntity/table name |
| filename | File name |
| MbException | if entity name is invalid |
|
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.
| path | Path to check |
|
nodiscard |
Remove a file from an entity's directory.
| entity_name | MbEntity/table name |
| filename | File name to remove |
| void mb::MbFilesMgr::removeFiles | ( | const std::string & | entity_name, |
| const std::vector< std::string > & | files ) const |
Remove multiple files from an entity's directory.
| entity_name | MbEntity/table name |
| files | Vector of file names to remove |
| 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.
| old_entity_name | Current entity name |
| new_entity_name | New entity name |
| MbException | if either entity name is invalid |