Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_dynamic_media.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Media and static asset serving for in-process dynamic handling.
4 *
5 * Resolves and serves files from the app's artifact directory for
6 * routes that map to static assets such as CSS, JavaScript, images,
7 * and JSON metadata files.
8 *
9 * Also provides media mutation helpers (upload, delete, path resolution)
10 * so that the dynamic runtime can handle file uploads and media tree
11 * management without delegating to a child process or app-owned shell logic.
12 */
13
14#ifndef PHAROS_NATIVE_DYNAMIC_MEDIA_H
15#define PHAROS_NATIVE_DYNAMIC_MEDIA_H
16
17#include "native_connection.h"
18#include "native_http_request.h"
19
20#include <stddef.h>
21
22/** @name Media serving helpers
23 * @{
24 */
25
26/**
27 * @brief Determines whether a request path targets a known static asset.
28 *
29 * Paths beginning with @c /static/, @c /media/, or matching a
30 * well-known JSON endpoint (e.g. @c /manifest.json) are considered
31 * static assets that can be served directly from the artifact directory.
32 *
33 * @param path Normalised request path (query string stripped).
34 * @return Non-zero when the path is a known static asset, 0 otherwise.
35 */
36int native_dynamic_media_is_static_path(const char *path);
37
38/**
39 * @brief Serves a static asset from the artifact directory.
40 *
41 * Resolves the file path, determines the content type, and writes
42 * the full HTTP response to @p connection.
43 *
44 * @param connection Connection to write the response to.
45 * @param artifact_dir App artifact root directory.
46 * @param path Normalised request path.
47 * @param request Parsed request (used for conditional GET).
48 * @return HTTP status code written, or a negative value on error.
49 */
51 NativeConnection *connection,
52 const char *artifact_dir,
53 const char *path,
54 const HttpRequest *request
55);
56
57/**
58 * @brief Resolves a request path to a file-system path inside the artifact directory.
59 *
60 * Returns NULL when the path does not map to a valid asset file.
61 *
62 * @param artifact_dir App artifact root directory.
63 * @param path Normalised request path.
64 * @return Newly allocated file-system path, or NULL. Caller must free.
65 */
67 const char *artifact_dir,
68 const char *path
69);
70
71/** @} */
72
73/** @name Media mutation helpers
74 * @{
75 */
76
77/**
78 * @brief Sanitises a raw upload filename into a safe filesystem name.
79 *
80 * Strips directory components, replaces unsafe characters with @c _,
81 * and removes leading/trailing dots, dashes, and underscores.
82 *
83 * @param raw_name Raw filename from the upload (may be NULL).
84 * @return Newly allocated safe filename, or an empty string on failure.
85 * Caller must free.
86 */
87char *safe_upload_filename_dup(const char *raw_name);
88
89/**
90 * @brief Resolves a media URL into an on-disk path inside an app artifact directory.
91 *
92 * @param artifact_dir Artifact directory that owns the media tree.
93 * @param media_url Media URL beginning with @c /media/.
94 * @return Newly allocated disk path, or NULL when the URL is outside the managed tree.
95 * Caller must free.
96 */
97char *media_disk_path_dup(const char *artifact_dir, const char *media_url);
98
99/**
100 * @brief Ensures a managed media subtree exists for an entity type by reading
101 * the media policy fixture at {artifact_dir}/data/framework/media-policy-v1.json.
102 *
103 * Loads the fixture, finds the entry matching @p entity_type, substitutes
104 * @p entity_slug into the @c base_path_template, and creates the base
105 * directory and all declared subdirectories.
106 *
107 * @param artifact_dir App artifact root directory.
108 * @param entity_type Entity type key to look up in the fixture (e.g. "business").
109 * @param entity_slug Entity slug to substitute into the path template.
110 * @return 0 on success, or a non-zero status code on failure.
111 */
112int ensure_media_subtree(const char *artifact_dir, const char *entity_type, const char *entity_slug);
113
114/**
115 * @brief Ensures the managed business media subtree exists under an app artifact.
116 *
117 * Creates the directory structure for a business's media by delegating to
118 * @c ensure_media_subtree() with the @c "business" entity type, reading
119 * the directory layout from the media policy fixture.
120 *
121 * @param artifact_dir App artifact root directory.
122 * @param business_slug Business slug identifying the subtree.
123 * @return 0 on success, or a non-zero status code on failure.
124 */
125int ensure_business_media_tree(const char *artifact_dir, const char *business_slug);
126
127/**
128 * @brief Removes a managed business media file from an app artifact when it exists.
129 *
130 * Only removes files whose URL begins with @c /media/businesses/.
131 * Silently succeeds when the file does not exist or the URL is not
132 * a recognised business media path.
133 *
134 * @param artifact_dir App artifact root directory.
135 * @param media_url Media URL of the file to remove.
136 * @return 0 on success or when nothing to remove, non-zero on failure.
137 */
138int remove_media_url_file(const char *artifact_dir, const char *media_url);
139
140/**
141 * @brief Extracts a multipart upload into the app artifact media tree and returns the resulting media URL.
142 *
143 * Reads the request body from @p body_path, locates the multipart part
144 * for @p field_name, sanitises the filename, writes the file to
145 * @p target_url_dir inside @p artifact_dir, and returns the public media URL.
146 *
147 * @param artifact_dir App artifact root directory.
148 * @param content_type Full Content-Type header of the request.
149 * @param body_path Path to the request body file on disk.
150 * @param field_name Form field name of the file input.
151 * @param target_url_dir Target URL directory under @c /media/ (e.g. @c /media/businesses/lotus-studio).
152 * @param fallback_filename Fallback filename when the upload provides none.
153 * @return Newly allocated media URL string, or NULL on failure. Caller must free.
154 */
156 const char *artifact_dir,
157 const char *content_type,
158 const char *body_path,
159 const char *field_name,
160 const char *target_url_dir,
161 const char *fallback_filename
162);
163
164/** @} */
165
166#endif
Declares the native connection abstraction and transport metadata helpers.
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.
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.
char * safe_upload_filename_dup(const char *raw_name)
Sanitises a raw upload filename into a safe filesystem name.
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.
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 {art...
int ensure_business_media_tree(const char *artifact_dir, const char *business_slug)
Ensures the managed business media subtree exists under an app artifact.
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 native_dynamic_media_is_static_path(const char *path)
Determines whether a request path targets a known static asset.
Declares the parsed HTTP request record and request parsing helpers.
Parsed HTTP request fields extracted from a native connection.
Accepted client connection and its resolved transport metadata.