Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_dynamic_media.h File Reference

Media and static asset serving for in-process dynamic handling. More...

#include "native_connection.h"
#include "native_http_request.h"
#include <stddef.h>

Go to the source code of this file.

Functions

Media serving helpers
int native_dynamic_media_is_static_path (const char *path)
 Determines whether a request path targets a known static asset.
int native_dynamic_media_serve_asset (NativeConnection *connection, const char *artifact_dir, const char *path, const HttpRequest *request)
 Serves a static asset from the artifact directory.
char * native_dynamic_media_resolve_path (const char *artifact_dir, const char *path)
 Resolves a request path to a file-system path inside the artifact directory.
Media mutation helpers
char * safe_upload_filename_dup (const char *raw_name)
 Sanitises a raw upload filename into a safe filesystem name.
char * media_disk_path_dup (const char *artifact_dir, const char *media_url)
 Resolves a media URL into an on-disk path inside an app artifact directory.
int ensure_media_subtree (const char *artifact_dir, const char *entity_type, const char *entity_slug)
 Ensures a managed media subtree exists for an entity type by reading the media policy fixture at {artifact_dir}/data/framework/media-policy-v1.json.
int ensure_business_media_tree (const char *artifact_dir, const char *business_slug)
 Ensures the managed business media subtree exists under an app artifact.
int remove_media_url_file (const char *artifact_dir, const char *media_url)
 Removes a managed business media file from an app artifact when it exists.
char * store_uploaded_media (const char *artifact_dir, const char *content_type, const char *body_path, const char *field_name, const char *target_url_dir, const char *fallback_filename)
 Extracts a multipart upload into the app artifact media tree and returns the resulting media URL.

Detailed Description

Media and static asset serving for in-process dynamic handling.

Resolves and serves files from the app's artifact directory for routes that map to static assets such as CSS, JavaScript, images, and JSON metadata files.

Also provides media mutation helpers (upload, delete, path resolution) so that the dynamic runtime can handle file uploads and media tree management without delegating to a child process or app-owned shell logic.

Definition in file native_dynamic_media.h.

Function Documentation

◆ ensure_business_media_tree()

int ensure_business_media_tree ( const char * artifact_dir,
const char * business_slug )

Ensures the managed business media subtree exists under an app artifact.

Creates the directory structure for a business's media by delegating to ensure_media_subtree() with the "business" entity type, reading the directory layout from the media policy fixture.

Parameters
artifact_dirApp artifact root directory.
business_slugBusiness slug identifying the subtree.
Returns
0 on success, or a non-zero status code on failure.

Definition at line 484 of file native_dynamic_media.c.

References ensure_media_subtree().

Referenced by handle_action_route(), and handle_dynamic_ensure_business_media_tree_command().

◆ ensure_media_subtree()

int ensure_media_subtree ( const char * artifact_dir,
const char * entity_type,
const char * entity_slug )

Ensures a managed media subtree exists for an entity type by reading the media policy fixture at {artifact_dir}/data/framework/media-policy-v1.json.

Loads the fixture, finds the entry matching entity_type, substitutes entity_slug into the base_path_template, and creates the base directory and all declared subdirectories.

Parameters
artifact_dirApp artifact root directory.
entity_typeEntity type key to look up in the fixture (e.g. "business").
entity_slugEntity slug to substitute into the path template.
Returns
0 on success, or a non-zero status code on failure.

Definition at line 320 of file native_dynamic_media.c.

References ensure_directory(), native_json_doc_free(), native_json_doc_load_file(), native_json_obj_get(), native_json_obj_get_array(), native_json_obj_get_string_dup(), path_join(), streq(), yyjson_arr_get(), yyjson_arr_size(), yyjson_doc_get_root(), yyjson_get_str(), yyjson_is_obj(), and yyjson_is_str().

Referenced by ensure_business_media_tree().

◆ media_disk_path_dup()

char * media_disk_path_dup ( const char * artifact_dir,
const char * media_url )

Resolves a media URL into an on-disk path inside an app artifact directory.

Parameters
artifact_dirArtifact directory that owns the media tree.
media_urlMedia URL beginning with /media/.
Returns
Newly allocated disk path, or NULL when the URL is outside the managed tree. Caller must free.

Definition at line 313 of file native_dynamic_media.c.

References path_join(), and starts_with().

Referenced by handle_dynamic_media_disk_path_command(), and remove_media_url_file().

◆ native_dynamic_media_is_static_path()

int native_dynamic_media_is_static_path ( const char * path)

Determines whether a request path targets a known static asset.

Paths beginning with /static/, /media/, or matching a well-known JSON endpoint (e.g. /manifest.json) are considered static assets that can be served directly from the artifact directory.

Parameters
pathNormalised request path (query string stripped).
Returns
Non-zero when the path is a known static asset, 0 otherwise.

Definition at line 69 of file native_dynamic_media.c.

References starts_with(), streq(), and WELL_KNOWN_JSON_PATHS.

Referenced by native_dynamic_runtime_handle_request().

◆ native_dynamic_media_resolve_path()

char * native_dynamic_media_resolve_path ( const char * artifact_dir,
const char * path )

Resolves a request path to a file-system path inside the artifact directory.

Returns NULL when the path does not map to a valid asset file.

Parameters
artifact_dirApp artifact root directory.
pathNormalised request path.
Returns
Newly allocated file-system path, or NULL. Caller must free.

Definition at line 91 of file native_dynamic_media.c.

References path_join(), starts_with(), and streq().

Referenced by native_dynamic_media_serve_asset().

◆ native_dynamic_media_serve_asset()

int native_dynamic_media_serve_asset ( NativeConnection * connection,
const char * artifact_dir,
const char * path,
const HttpRequest * request )

Serves a static asset from the artifact directory.

Resolves the file path, determines the content type, and writes the full HTTP response to connection.

Parameters
connectionConnection to write the response to.
artifact_dirApp artifact root directory.
pathNormalised request path.
requestParsed request (used for conditional GET).
Returns
HTTP status code written, or a negative value on error.

Definition at line 142 of file native_dynamic_media.c.

References mime_type_for_path(), native_connection_printf(), native_connection_write(), native_dynamic_media_resolve_path(), native_send_text_response(), path_exists(), path_is_directory(), path_join(), starts_with(), and streq().

Referenced by handle_page_route(), and native_dynamic_runtime_handle_request().

◆ remove_media_url_file()

int remove_media_url_file ( const char * artifact_dir,
const char * media_url )

Removes a managed business media file from an app artifact when it exists.

Only removes files whose URL begins with /media/businesses/. Silently succeeds when the file does not exist or the URL is not a recognised business media path.

Parameters
artifact_dirApp artifact root directory.
media_urlMedia URL of the file to remove.
Returns
0 on success or when nothing to remove, non-zero on failure.

Definition at line 488 of file native_dynamic_media.c.

References media_disk_path_dup(), path_exists(), and starts_with().

Referenced by handle_action_route(), and handle_dynamic_remove_media_url_file_command().

◆ safe_upload_filename_dup()

char * safe_upload_filename_dup ( const char * raw_name)

Sanitises a raw upload filename into a safe filesystem name.

Strips directory components, replaces unsafe characters with _, and removes leading/trailing dots, dashes, and underscores.

Parameters
raw_nameRaw filename from the upload (may be NULL).
Returns
Newly allocated safe filename, or an empty string on failure. Caller must free.

Definition at line 266 of file native_dynamic_media.c.

Referenced by handle_dynamic_safe_upload_filename_command(), and store_uploaded_media().

◆ store_uploaded_media()

char * store_uploaded_media ( const char * artifact_dir,
const char * content_type,
const char * body_path,
const char * field_name,
const char * target_url_dir,
const char * fallback_filename )

Extracts a multipart upload into the app artifact media tree and returns the resulting media URL.

Reads the request body from body_path, locates the multipart part for field_name, sanitises the filename, writes the file to target_url_dir inside artifact_dir, and returns the public media URL.

Parameters
artifact_dirApp artifact root directory.
content_typeFull Content-Type header of the request.
body_pathPath to the request body file on disk.
field_nameForm field name of the file input.
target_url_dirTarget URL directory under /media/ (e.g. /media/businesses/lotus-studio).
fallback_filenameFallback filename when the upload provides none.
Returns
Newly allocated media URL string, or NULL on failure. Caller must free.

Definition at line 501 of file native_dynamic_media.c.

References ensure_directory(), native_dynamic_multipart_find_part(), path_join(), read_file_bytes_dup_runtime(), safe_upload_filename_dup(), and write_binary_file_runtime().

Referenced by handle_action_route(), and handle_dynamic_store_uploaded_media_command().