Просмотр исходного кода

Check and delete before download

jblanked 1 год назад
Родитель
Сommit
51a1ca2817
2 измененных файлов с 17 добавлено и 1 удалено
  1. 15 1
      flipper_http/flipper_http.c
  2. 2 0
      flipper_http/flipper_http.h

+ 15 - 1
flipper_http/flipper_http.c

@@ -16,6 +16,17 @@ bool flipper_http_append_to_file(
 
 
     if (start_new_file)
     if (start_new_file)
     {
     {
+        // Delete the file if it already exists
+        if (storage_file_exists(storage, file_path))
+        {
+            if (!storage_simply_remove_recursive(storage, file_path))
+            {
+                FURI_LOG_E(HTTP_TAG, "Failed to delete file: %s", file_path);
+                storage_file_free(file);
+                furi_record_close(RECORD_STORAGE);
+                return false;
+            }
+        }
         // Open the file in write mode
         // Open the file in write mode
         if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
         if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
         {
         {
@@ -183,12 +194,13 @@ int32_t flipper_http_worker(void *context)
                         if (!flipper_http_append_to_file(
                         if (!flipper_http_append_to_file(
                                 file_buffer,
                                 file_buffer,
                                 file_buffer_len,
                                 file_buffer_len,
-                                !fhttp.just_started_get && !fhttp.just_started_post,
+                                fhttp.just_started_bytes,
                                 fhttp.file_path))
                                 fhttp.file_path))
                         {
                         {
                             FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
                             FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
                         }
                         }
                         file_buffer_len = 0;
                         file_buffer_len = 0;
+                        fhttp.just_started_bytes = false;
                     }
                     }
                 }
                 }
 
 
@@ -1319,6 +1331,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         fhttp.state = RECEIVING;
         fhttp.state = RECEIVING;
         // for GET request, save data only if it's a bytes request
         // for GET request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
         fhttp.save_bytes = fhttp.is_bytes_request;
+        fhttp.just_started_bytes = true;
         file_buffer_len = 0;
         file_buffer_len = 0;
         return;
         return;
     }
     }
@@ -1330,6 +1343,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         fhttp.state = RECEIVING;
         fhttp.state = RECEIVING;
         // for POST request, save data only if it's a bytes request
         // for POST request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
         fhttp.save_bytes = fhttp.is_bytes_request;
+        fhttp.just_started_bytes = true;
         file_buffer_len = 0;
         file_buffer_len = 0;
         return;
         return;
     }
     }

+ 2 - 0
flipper_http/flipper_http.h

@@ -76,6 +76,8 @@ typedef struct
     bool is_bytes_request;     // Flag to indicate if the request is for bytes
     bool is_bytes_request;     // Flag to indicate if the request is for bytes
     bool save_bytes;           // Flag to save the received data to a file
     bool save_bytes;           // Flag to save the received data to a file
     bool save_received_data;   // Flag to save the received data to a file
     bool save_received_data;   // Flag to save the received data to a file
+
+    bool just_started_bytes; // Indicates if bytes data reception has just started
 } FlipperHTTP;
 } FlipperHTTP;
 
 
 extern FlipperHTTP fhttp;
 extern FlipperHTTP fhttp;