@@ -297,42 +297,59 @@ static esp_err_t _upload_post_handler(httpd_req_t *req)
297297 return ESP_OK ;
298298}
299299
300- // Populate JSON data with list of all files (name, sie, [url])
301- void srv_file_json_list_files (cJSON * list_files ) {
302- char entrypath [FILE_PATH_MAX ];
300+ // Populate JSON data with list of all files in a directory (name, size, [url])
301+ void srv_file_json_list_dir (char * entrypath , cJSON * json_entries ) {
303302 struct dirent * entry ;
304303 struct stat entry_stat ;
305304
306- /* Open root directory */
307- snprintf (entrypath , FILE_PATH_MAX , "%s/" , _self .root_path );
308305 DIR * dir = opendir (entrypath );
309306 if (!dir ) {
310- ESP_LOGE (TAG , "%s" , " Failed to open root directory" );
307+ ESP_LOGE (TAG , "Failed to open %s directory" , entrypath );
311308 return ;
312309 }
313310
314- // FIXME: Add error handling ...
315- cJSON * json_entries = cJSON_CreateArray ();
316- cJSON_AddItemToObject (list_files , "list_files" , json_entries );
317311 /* Iterate over all files / folders and fetch their names and sizes */
312+ size_t entry_path_len = strlen (entrypath );
318313 while ((entry = readdir (dir )) != NULL ) {
319- strlcpy (entrypath + _self . root_path_len + 1 , entry -> d_name , sizeof ( entrypath ) - _self . root_path_len - 1 );
314+ strlcat (entrypath , entry -> d_name , FILE_PATH_MAX - 1 );
320315 if (stat (entrypath , & entry_stat ) == -1 ) {
321316 ESP_LOGE (TAG , "Failed to stat : %s" , entry -> d_name );
322317 continue ;
323318 }
324- // Build JSON dictionnary for this entry
325- cJSON * json_entry = cJSON_CreateObject ();
326- cJSON_AddItemToArray (json_entries , json_entry );
327- cJSON_AddStringToObject (json_entry , "name" , entry -> d_name );
328- cJSON_AddNumberToObject (json_entry , "size" , entry_stat .st_size );
329- if (!strncmp (_self .base_path , entrypath , _self .base_path_len )) {
330- cJSON_AddStringToObject (json_entry , "url" , entrypath + _self .base_path_len );
319+ if (S_ISDIR (entry_stat .st_mode )) {
320+ /* Entry is a directory, add '/' to the end of the name and
321+ * recursively list the directory */
322+ strlcat (entrypath , "/" , FILE_PATH_MAX - 1 );
323+ srv_file_json_list_dir (entrypath , json_entries );
324+ } else {
325+ /* Entry is a file, build JSON dictionnary for this entry */
326+ cJSON * json_entry = cJSON_CreateObject ();
327+ cJSON_AddItemToArray (json_entries , json_entry );
328+ cJSON_AddStringToObject (json_entry , "name" , entrypath + _self .root_path_len );
329+ cJSON_AddNumberToObject (json_entry , "size" , entry_stat .st_size );
330+ if (!strncmp (_self .base_path , entrypath , _self .base_path_len )) {
331+ cJSON_AddStringToObject (json_entry , "url" , entrypath + _self .base_path_len );
332+ }
331333 }
334+ entrypath [entry_path_len ] = 0 ; // Reset to parent path
332335 }
333336 closedir (dir );
334337}
335338
339+ // Populate JSON data with list of all files on the file system
340+ void srv_file_json_list_files (cJSON * list_files ) {
341+ char entrypath [FILE_PATH_MAX ];
342+
343+ // FIXME: Add error handling ...
344+ cJSON * json_entries = cJSON_CreateArray ();
345+ cJSON_AddItemToObject (list_files , "list_files" , json_entries );
346+
347+ /* Start from root directory */
348+ snprintf (entrypath , FILE_PATH_MAX , "%s/" , _self .root_path );
349+
350+ srv_file_json_list_dir (entrypath , json_entries );
351+ }
352+
336353/* Delete file listed in JSON data*/
337354esp_err_t srv_file_json_delete_files (cJSON * list_files ) {
338355 char filepath [FILE_PATH_MAX ];
0 commit comments