Skip to content

Commit 7245114

Browse files
committed
Restore VFS Python v1.1
1 parent 1cfad3c commit 7245114

3 files changed

Lines changed: 55 additions & 46 deletions

File tree

examples/C++/virtual_file_system/vfs.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
# Canada.
88
#
99
# WebUI Virtual File System Generator
10-
# v1.2.0
10+
# v1.1
1111

1212
import os
1313
import sys
1414

1515
def generate_vfs_header(directory, output_header):
1616
files = []
17+
index_files = {}
1718

18-
# Walk through the directory and collect files
1919
for root, _, filenames in os.walk(directory):
2020
for filename in filenames:
2121
filepath = os.path.join(root, filename)
@@ -24,10 +24,16 @@ def generate_vfs_header(directory, output_header):
2424
relative_path = '/' + relative_path.replace('\\', '/')
2525
files.append((relative_path, filepath))
2626

27-
# Generate the C header file
27+
# Check for index files
28+
if filename.startswith("index."):
29+
dir_path = os.path.dirname(relative_path)
30+
if dir_path not in index_files:
31+
index_files[dir_path] = relative_path
32+
2833
with open(output_header, 'w') as header:
2934
header.write('#ifndef VIRTUAL_FILE_SYSTEM_H\n')
3035
header.write('#define VIRTUAL_FILE_SYSTEM_H\n\n')
36+
3137
header.write('typedef struct {\n')
3238
header.write(' const char *path;\n')
3339
header.write(' const unsigned char *data;\n')
@@ -56,6 +62,15 @@ def generate_vfs_header(directory, output_header):
5662

5763
header.write('static const int virtual_files_count = sizeof(virtual_files) / sizeof(virtual_files[0]);\n\n')
5864

65+
header.write('static const char* index_files[] = {\n')
66+
for dir_path, index_path in index_files.items():
67+
if dir_path == "/":
68+
header.write(f' "/", "{index_path}",\n')
69+
else:
70+
header.write(f' "{dir_path}/", "{index_path}",\n')
71+
header.write(' NULL\n')
72+
header.write('};\n\n')
73+
5974
header.write('bool virtual_file_system(const char* path, const unsigned char** file, int* length) {\n')
6075
header.write(' for (int i = 0; i < virtual_files_count; ++i) {\n')
6176
header.write(' if (strcmp(virtual_files[i].path, path) == 0) {\n')
@@ -70,6 +85,7 @@ def generate_vfs_header(directory, output_header):
7085
header.write('const void* vfs(const char* path, int* length) {\n')
7186
header.write(' const unsigned char* file_data;\n')
7287
header.write(' int file_length;\n\n')
88+
7389
header.write(' if (virtual_file_system(path, &file_data, &file_length)) {\n')
7490
header.write(' const char* content_type = webui_get_mime_type(path);\n')
7591
header.write(' const char* http_header_template = "HTTP/1.1 200 OK\\r\\n"\n')
@@ -82,8 +98,29 @@ def generate_vfs_header(directory, output_header):
8298
header.write(' snprintf((char*) response, header_length + 1, http_header_template, content_type, file_length);\n')
8399
header.write(' memcpy(response + header_length, file_data, file_length);\n')
84100
header.write(' return response;\n')
101+
header.write(' } else {\n')
102+
header.write(' // Check for index file redirection\n')
103+
header.write(' char redirect_path[1024];\n')
104+
header.write(' snprintf(redirect_path, sizeof(redirect_path), "%s", path);\n')
105+
header.write(' size_t len = strlen(redirect_path);\n')
106+
header.write(' if (redirect_path[len - 1] != \'/\') {\n')
107+
header.write(' redirect_path[len] = \'/\';\n')
108+
header.write(' redirect_path[len + 1] = \'\\0\';\n')
109+
header.write(' }\n')
110+
header.write(' for (int i = 0; index_files[i] != NULL; i += 2) {\n')
111+
header.write(' if (strcmp(index_files[i], redirect_path) == 0) {\n')
112+
header.write(' const char* location_header = "HTTP/1.1 302 Found\\r\\n"\n')
113+
header.write(' "Location: %s\\r\\n"\n')
114+
header.write(' "Cache-Control: no-cache\\r\\n\\r\\n";\n')
115+
header.write(' int header_length = snprintf(NULL, 0, location_header, index_files[i + 1]);\n')
116+
header.write(' *length = header_length;\n')
117+
header.write(' unsigned char* response = (unsigned char*) webui_malloc(*length);\n')
118+
header.write(' snprintf((char*) response, header_length + 1, location_header, index_files[i + 1]);\n')
119+
header.write(' return response;\n')
120+
header.write(' }\n')
121+
header.write(' }\n')
122+
header.write(' return NULL;\n')
85123
header.write(' }\n')
86-
header.write(' return NULL;\n')
87124
header.write('}\n\n')
88125

89126
header.write('#endif // VIRTUAL_FILE_SYSTEM_H\n')

examples/C/react/vfs.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,15 @@
77
# Canada.
88
#
99
# WebUI Virtual File System Generator
10-
# v1.2.0
10+
# v1.1
1111

1212
import os
1313
import sys
1414

15-
def generate_vfs_header(directory, output_header, custom_index=None):
15+
def generate_vfs_header(directory, output_header):
1616
files = []
1717
index_files = {}
1818

19-
# Handle custom index file
20-
custom_index_path = None
21-
if custom_index:
22-
custom_index_rel = custom_index.lstrip('/').replace('\\', '/')
23-
custom_index_abs = os.path.join(directory, custom_index_rel)
24-
if os.path.isfile(custom_index_abs):
25-
custom_index_path = '/' + custom_index_rel
26-
27-
# Walk through the directory and collect files
2819
for root, _, filenames in os.walk(directory):
2920
for filename in filenames:
3021
filepath = os.path.join(root, filename)
@@ -39,14 +30,10 @@ def generate_vfs_header(directory, output_header, custom_index=None):
3930
if dir_path not in index_files:
4031
index_files[dir_path] = relative_path
4132

42-
# If a custom index file is provided, override the root index
43-
if custom_index_path is not None:
44-
index_files["/"] = custom_index_path
45-
46-
# Generate the C header file
4733
with open(output_header, 'w') as header:
4834
header.write('#ifndef VIRTUAL_FILE_SYSTEM_H\n')
4935
header.write('#define VIRTUAL_FILE_SYSTEM_H\n\n')
36+
5037
header.write('typedef struct {\n')
5138
header.write(' const char *path;\n')
5239
header.write(' const unsigned char *data;\n')
@@ -98,6 +85,7 @@ def generate_vfs_header(directory, output_header, custom_index=None):
9885
header.write('const void* vfs(const char* path, int* length) {\n')
9986
header.write(' const unsigned char* file_data;\n')
10087
header.write(' int file_length;\n\n')
88+
10189
header.write(' if (virtual_file_system(path, &file_data, &file_length)) {\n')
10290
header.write(' const char* content_type = webui_get_mime_type(path);\n')
10391
header.write(' const char* http_header_template = "HTTP/1.1 200 OK\\r\\n"\n')
@@ -138,13 +126,11 @@ def generate_vfs_header(directory, output_header, custom_index=None):
138126
header.write('#endif // VIRTUAL_FILE_SYSTEM_H\n')
139127

140128
if __name__ == '__main__':
141-
if len(sys.argv) not in (3, 4):
142-
print(f'Usage: {sys.argv[0]} <directory> <output_header> [custom_index_filename]')
129+
if len(sys.argv) != 3:
130+
print(f'Usage: {sys.argv[0]} <directory> <output_header>')
143131
sys.exit(1)
144132

145133
directory = sys.argv[1]
146134
output_header = sys.argv[2]
147-
custom_index = sys.argv[3] if len(sys.argv) == 4 else None
148-
149-
generate_vfs_header(directory, output_header, custom_index)
135+
generate_vfs_header(directory, output_header)
150136
print(f'Generated {output_header} from {directory}')

examples/C/virtual_file_system/vfs.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,15 @@
77
# Canada.
88
#
99
# WebUI Virtual File System Generator
10-
# v1.2.0
10+
# v1.1
1111

1212
import os
1313
import sys
1414

15-
def generate_vfs_header(directory, output_header, custom_index=None):
15+
def generate_vfs_header(directory, output_header):
1616
files = []
1717
index_files = {}
1818

19-
# Handle custom index file
20-
custom_index_path = None
21-
if custom_index:
22-
custom_index_rel = custom_index.lstrip('/').replace('\\', '/')
23-
custom_index_abs = os.path.join(directory, custom_index_rel)
24-
if os.path.isfile(custom_index_abs):
25-
custom_index_path = '/' + custom_index_rel
26-
27-
# Walk through the directory and collect files
2819
for root, _, filenames in os.walk(directory):
2920
for filename in filenames:
3021
filepath = os.path.join(root, filename)
@@ -39,14 +30,10 @@ def generate_vfs_header(directory, output_header, custom_index=None):
3930
if dir_path not in index_files:
4031
index_files[dir_path] = relative_path
4132

42-
# If a custom index file is provided, override the root index
43-
if custom_index_path is not None:
44-
index_files["/"] = custom_index_path
45-
46-
# Generate the C header file
4733
with open(output_header, 'w') as header:
4834
header.write('#ifndef VIRTUAL_FILE_SYSTEM_H\n')
4935
header.write('#define VIRTUAL_FILE_SYSTEM_H\n\n')
36+
5037
header.write('typedef struct {\n')
5138
header.write(' const char *path;\n')
5239
header.write(' const unsigned char *data;\n')
@@ -98,6 +85,7 @@ def generate_vfs_header(directory, output_header, custom_index=None):
9885
header.write('const void* vfs(const char* path, int* length) {\n')
9986
header.write(' const unsigned char* file_data;\n')
10087
header.write(' int file_length;\n\n')
88+
10189
header.write(' if (virtual_file_system(path, &file_data, &file_length)) {\n')
10290
header.write(' const char* content_type = webui_get_mime_type(path);\n')
10391
header.write(' const char* http_header_template = "HTTP/1.1 200 OK\\r\\n"\n')
@@ -138,13 +126,11 @@ def generate_vfs_header(directory, output_header, custom_index=None):
138126
header.write('#endif // VIRTUAL_FILE_SYSTEM_H\n')
139127

140128
if __name__ == '__main__':
141-
if len(sys.argv) not in (3, 4):
142-
print(f'Usage: {sys.argv[0]} <directory> <output_header> [custom_index_filename]')
129+
if len(sys.argv) != 3:
130+
print(f'Usage: {sys.argv[0]} <directory> <output_header>')
143131
sys.exit(1)
144132

145133
directory = sys.argv[1]
146134
output_header = sys.argv[2]
147-
custom_index = sys.argv[3] if len(sys.argv) == 4 else None
148-
149-
generate_vfs_header(directory, output_header, custom_index)
135+
generate_vfs_header(directory, output_header)
150136
print(f'Generated {output_header} from {directory}')

0 commit comments

Comments
 (0)