From e1f31bf25787b9d8c6aa0085d8c7900b514c9fd2 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Jul 2024 17:01:02 +0700 Subject: [PATCH] Fix BLOB upload issue in Firebase Storage --- README.md | 16 +- .../CustomAuthFile/CustomAuthFile.ino | 25 +-- .../ServiceAuthFile/ServiceAuthFile.ino | 25 +-- .../CustomAuthFile/CustomAuthFile.ino | 25 +-- .../ServiceAuthFile/ServiceAuthFile.ino | 25 +-- .../Sync/CustomAuthFile/CustomAuthFile.ino | 25 +-- .../Sync/ServiceAuthFile/ServiceAuthFile.ino | 25 +-- .../Async/Callback/Download/Download.ino | 25 +-- .../Async/Callback/Upload/Upload.ino | 26 +-- .../Async/NoCallback/Download/Download.ino | 25 +-- .../Async/NoCallback/Upload/Upload.ino | 26 +-- .../CloudStorage/Sync/Download/Download.ino | 25 +-- examples/CloudStorage/Sync/Upload/Upload.ino | 26 +-- .../Async/Callback/File/File.ino | 49 +++--- .../Async/NoCallback/File/File.ino | 49 +++--- .../Async/Callback/Download/Download.ino | 25 +-- .../Storage/Async/Callback/Upload/Upload.ino | 25 +-- .../Async/NoCallback/Download/Download.ino | 25 +-- .../Async/NoCallback/Upload/Upload.ino | 25 +-- examples/Storage/Sync/Download/Download.ino | 25 +-- examples/Storage/Sync/Upload/Upload.ino | 25 +-- library.json | 2 +- library.properties | 2 +- resources/docs/realtime_database.md | 72 ++++++-- src/cloud_storage/CloudStorage.h | 60 +++---- src/core/AsyncClient/AsyncClient.h | 26 --- src/core/Core.h | 2 +- src/core/FileConfig.h | 166 +----------------- src/storage/Storage.h | 4 +- 29 files changed, 375 insertions(+), 526 deletions(-) diff --git a/README.md b/README.md index 57f670a..ecddf89 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github) -![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.3.7-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient) +![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.3.8-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient) [![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt) -Revision `2024-07-26T08:35:39Z` +Revision `2024-07-27T09:57:58Z` ## Table of Contents @@ -1260,21 +1260,25 @@ In case of file errors, you have to verify your hardware and your code to make s ```cpp +#include +File myFile; // Define the File object globally. + #define MY_FS SPIFFS void fileCallback(File &file, const char *filename, file_operating_mode mode) { // FILE_OPEN_MODE_READ, FILE_OPEN_MODE_WRITE and FILE_OPEN_MODE_APPEND are defined in this library + // MY_FS is defined in this example switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -1282,6 +1286,8 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } FileConfig media_file("/media.mp4", fileCallback); diff --git a/examples/App/AppInitialization/Async/Callback/CustomAuthFile/CustomAuthFile.ino b/examples/App/AppInitialization/Async/Callback/CustomAuthFile/CustomAuthFile.ino index 650b5c6..7144651 100644 --- a/examples/App/AppInitialization/Async/Callback/CustomAuthFile/CustomAuthFile.ino +++ b/examples/App/AppInitialization/Async/Callback/CustomAuthFile/CustomAuthFile.ino @@ -54,20 +54,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -228,13 +227,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -242,5 +241,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif diff --git a/examples/App/AppInitialization/Async/Callback/ServiceAuthFile/ServiceAuthFile.ino b/examples/App/AppInitialization/Async/Callback/ServiceAuthFile/ServiceAuthFile.ino index d5a0a54..0ed57db 100644 --- a/examples/App/AppInitialization/Async/Callback/ServiceAuthFile/ServiceAuthFile.ino +++ b/examples/App/AppInitialization/Async/Callback/ServiceAuthFile/ServiceAuthFile.ino @@ -47,20 +47,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -219,13 +218,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -233,5 +232,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/App/AppInitialization/Async/NoCallback/CustomAuthFile/CustomAuthFile.ino b/examples/App/AppInitialization/Async/NoCallback/CustomAuthFile/CustomAuthFile.ino index d356c12..e2f7e04 100644 --- a/examples/App/AppInitialization/Async/NoCallback/CustomAuthFile/CustomAuthFile.ino +++ b/examples/App/AppInitialization/Async/NoCallback/CustomAuthFile/CustomAuthFile.ino @@ -54,20 +54,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -222,13 +221,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -236,5 +235,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif diff --git a/examples/App/AppInitialization/Async/NoCallback/ServiceAuthFile/ServiceAuthFile.ino b/examples/App/AppInitialization/Async/NoCallback/ServiceAuthFile/ServiceAuthFile.ino index 2711aa0..f5c7154 100644 --- a/examples/App/AppInitialization/Async/NoCallback/ServiceAuthFile/ServiceAuthFile.ino +++ b/examples/App/AppInitialization/Async/NoCallback/ServiceAuthFile/ServiceAuthFile.ino @@ -47,20 +47,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -213,13 +212,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -227,5 +226,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/App/AppInitialization/Sync/CustomAuthFile/CustomAuthFile.ino b/examples/App/AppInitialization/Sync/CustomAuthFile/CustomAuthFile.ino index ce79345..e85cda2 100644 --- a/examples/App/AppInitialization/Sync/CustomAuthFile/CustomAuthFile.ino +++ b/examples/App/AppInitialization/Sync/CustomAuthFile/CustomAuthFile.ino @@ -54,20 +54,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -229,13 +228,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -243,5 +242,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif diff --git a/examples/App/AppInitialization/Sync/ServiceAuthFile/ServiceAuthFile.ino b/examples/App/AppInitialization/Sync/ServiceAuthFile/ServiceAuthFile.ino index 1ed82fa..cab0065 100644 --- a/examples/App/AppInitialization/Sync/ServiceAuthFile/ServiceAuthFile.ino +++ b/examples/App/AppInitialization/Sync/ServiceAuthFile/ServiceAuthFile.ino @@ -47,20 +47,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -220,13 +219,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -234,5 +233,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/CloudStorage/Async/Callback/Download/Download.ino b/examples/CloudStorage/Async/Callback/Download/Download.ino index c4675f4..c8b1a5c 100644 --- a/examples/CloudStorage/Async/Callback/Download/Download.ino +++ b/examples/CloudStorage/Async/Callback/Download/Download.ino @@ -46,20 +46,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -256,13 +255,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -270,5 +269,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/CloudStorage/Async/Callback/Upload/Upload.ino b/examples/CloudStorage/Async/Callback/Upload/Upload.ino index b1a9276..c4dbd41 100644 --- a/examples/CloudStorage/Async/Callback/Upload/Upload.ino +++ b/examples/CloudStorage/Async/Callback/Upload/Upload.ino @@ -47,20 +47,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -179,7 +178,6 @@ void loop() options.mime = "video/mp4"; options.uploadType = GoogleCloudStorage::upload_type_resumable; // options.uploadType = GoogleCloudStorage::upload_type_simple; - // options.uploadType = GoogleCloudStorage::upload_type_multipart; #if defined(ENABLE_FS) cstorage.upload(aClient, GoogleCloudStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file), options, asyncCB, "uploadTask"); @@ -259,13 +257,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -273,5 +271,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/CloudStorage/Async/NoCallback/Download/Download.ino b/examples/CloudStorage/Async/NoCallback/Download/Download.ino index f6b1141..226adc1 100644 --- a/examples/CloudStorage/Async/NoCallback/Download/Download.ino +++ b/examples/CloudStorage/Async/NoCallback/Download/Download.ino @@ -45,20 +45,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -249,13 +248,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -263,5 +262,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino b/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino index e07bfb8..c68222d 100644 --- a/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino +++ b/examples/CloudStorage/Async/NoCallback/Upload/Upload.ino @@ -46,20 +46,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -178,7 +177,6 @@ void loop() options.mime = "video/mp4"; options.uploadType = GoogleCloudStorage::upload_type_resumable; // options.uploadType = GoogleCloudStorage::upload_type_simple; - // options.uploadType = GoogleCloudStorage::upload_type_multipart; #if defined(ENABLE_FS) cstorage.upload(aClient, GoogleCloudStorage::Parent(STORAGE_BUCKET_ID, "media.mp4"), getFile(media_file), options, aResult_no_callback); @@ -252,13 +250,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -266,5 +264,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/CloudStorage/Sync/Download/Download.ino b/examples/CloudStorage/Sync/Download/Download.ino index 335bfee..32815b3 100644 --- a/examples/CloudStorage/Sync/Download/Download.ino +++ b/examples/CloudStorage/Sync/Download/Download.ino @@ -46,20 +46,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -289,13 +288,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -303,5 +302,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/CloudStorage/Sync/Upload/Upload.ino b/examples/CloudStorage/Sync/Upload/Upload.ino index fbd0ffd..bcf21ba 100644 --- a/examples/CloudStorage/Sync/Upload/Upload.ino +++ b/examples/CloudStorage/Sync/Upload/Upload.ino @@ -47,20 +47,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -183,7 +182,6 @@ void loop() options.mime = "media.mp4"; options.uploadType = GoogleCloudStorage::upload_type_resumable; // options.uploadType = GoogleCloudStorage::upload_type_simple; - // options.uploadType = GoogleCloudStorage::upload_type_multipart; #if defined(ENABLE_FS) @@ -264,13 +262,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -278,5 +276,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/RealtimeDatabase/Async/Callback/File/File.ino b/examples/RealtimeDatabase/Async/Callback/File/File.ino index f31dd32..5d5e0e5 100644 --- a/examples/RealtimeDatabase/Async/Callback/File/File.ino +++ b/examples/RealtimeDatabase/Async/Callback/File/File.ino @@ -43,20 +43,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -227,27 +226,27 @@ void printResult(AsyncResult &aResult) } #if defined(ENABLE_FS) -void fileCallback(File &file, const char *filename, file_operating_mode mode) -{ - // FILE_OPEN_MODE_READ, FILE_OPEN_MODE_WRITE and FILE_OPEN_MODE_APPEND are defined in this library - // MY_FS is defined in this example - switch (mode) - { +void fileCallback(File &file, const char *filename, file_operating_mode mode) { + // FILE_OPEN_MODE_READ, FILE_OPEN_MODE_WRITE and FILE_OPEN_MODE_APPEND are defined in this library + // MY_FS is defined in this example + switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); - break; + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); + break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); - break; + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); - break; + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + break; case file_mode_remove: - MY_FS.remove(filename); - break; + MY_FS.remove(filename); + break; default: - break; - } + break; + } + // Set the internal FS object with global File object. + file = myFile; } void printFile() diff --git a/examples/RealtimeDatabase/Async/NoCallback/File/File.ino b/examples/RealtimeDatabase/Async/NoCallback/File/File.ino index fbd15f2..b66bb1a 100644 --- a/examples/RealtimeDatabase/Async/NoCallback/File/File.ino +++ b/examples/RealtimeDatabase/Async/NoCallback/File/File.ino @@ -42,20 +42,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -221,27 +220,27 @@ void printResult(AsyncResult &aResult) } #if defined(ENABLE_FS) -void fileCallback(File &file, const char *filename, file_operating_mode mode) -{ - // FILE_OPEN_MODE_READ, FILE_OPEN_MODE_WRITE and FILE_OPEN_MODE_APPEND are defined in this library - // MY_FS is defined in this example - switch (mode) - { +void fileCallback(File &file, const char *filename, file_operating_mode mode) { + // FILE_OPEN_MODE_READ, FILE_OPEN_MODE_WRITE and FILE_OPEN_MODE_APPEND are defined in this library + // MY_FS is defined in this example + switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); - break; + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); + break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); - break; + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); - break; + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + break; case file_mode_remove: - MY_FS.remove(filename); - break; + MY_FS.remove(filename); + break; default: - break; - } + break; + } + // Set the internal FS object with global File object. + file = myFile; } void printFile() diff --git a/examples/Storage/Async/Callback/Download/Download.ino b/examples/Storage/Async/Callback/Download/Download.ino index e8d1f38..c30fc94 100644 --- a/examples/Storage/Async/Callback/Download/Download.ino +++ b/examples/Storage/Async/Callback/Download/Download.ino @@ -45,20 +45,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -224,13 +223,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -238,5 +237,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/Storage/Async/Callback/Upload/Upload.ino b/examples/Storage/Async/Callback/Upload/Upload.ino index 6ad8b27..8cdab85 100644 --- a/examples/Storage/Async/Callback/Upload/Upload.ino +++ b/examples/Storage/Async/Callback/Upload/Upload.ino @@ -44,20 +44,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -220,13 +219,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -234,5 +233,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/Storage/Async/NoCallback/Download/Download.ino b/examples/Storage/Async/NoCallback/Download/Download.ino index 6b84bb1..73863ca 100644 --- a/examples/Storage/Async/NoCallback/Download/Download.ino +++ b/examples/Storage/Async/NoCallback/Download/Download.ino @@ -44,20 +44,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -216,13 +215,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -230,5 +229,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/Storage/Async/NoCallback/Upload/Upload.ino b/examples/Storage/Async/NoCallback/Upload/Upload.ino index 37c650b..ab82c9a 100644 --- a/examples/Storage/Async/NoCallback/Upload/Upload.ino +++ b/examples/Storage/Async/NoCallback/Upload/Upload.ino @@ -43,20 +43,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -213,13 +212,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -227,5 +226,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/Storage/Sync/Download/Download.ino b/examples/Storage/Sync/Download/Download.ino index 2a5dc85..0220790 100644 --- a/examples/Storage/Sync/Download/Download.ino +++ b/examples/Storage/Sync/Download/Download.ino @@ -45,20 +45,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -230,13 +229,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -244,5 +243,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/examples/Storage/Sync/Upload/Upload.ino b/examples/Storage/Sync/Upload/Upload.ino index ddcf231..cd9404b 100644 --- a/examples/Storage/Sync/Upload/Upload.ino +++ b/examples/Storage/Sync/Upload/Upload.ino @@ -44,20 +44,19 @@ #include #endif -#include +// In ESP32 Core SDK v3.x.x, to use filesystem in this library, +// the File object should be defined globally +// and the library's internal defined FS object should be set with +// this global FS object in fileCallback function. +#include +File myFile; -#if defined(ENABLE_FS) // Defined in this library -#if defined(FLASH_SUPPORTS) // Defined in this library #if defined(ESP32) #include #endif #define MY_FS SPIFFS -#else -#include -#include -#define MY_FS SD -#endif -#endif + +#include #define WIFI_SSID "WIFI_AP" #define WIFI_PASSWORD "WIFI_PASSWORD" @@ -228,13 +227,13 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) switch (mode) { case file_mode_open_read: - file = MY_FS.open(filename, FILE_OPEN_MODE_READ); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_READ); break; case file_mode_open_write: - file = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_WRITE); break; case file_mode_open_append: - file = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); + myFile = MY_FS.open(filename, FILE_OPEN_MODE_APPEND); break; case file_mode_remove: MY_FS.remove(filename); @@ -242,5 +241,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode) default: break; } + // Set the internal FS object with global File object. + file = myFile; } #endif \ No newline at end of file diff --git a/library.json b/library.json index 3eb3b65..385986e 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "FirebaseClient", - "version": "1.3.7", + "version": "1.3.8", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "Async Firebase Client library for Arduino.", "repository": { diff --git a/library.properties b/library.properties index 0435364..65aa4c6 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=FirebaseClient -version=1.3.7 +version=1.3.8 author=Mobizt diff --git a/resources/docs/realtime_database.md b/resources/docs/realtime_database.md index 3d3e98a..8e01770 100644 --- a/resources/docs/realtime_database.md +++ b/resources/docs/realtime_database.md @@ -267,20 +267,23 @@ class RealtimeDatabase ### Example ```cpp + #include + File myFile; // Define the File object globally. + void fileCallback(File &file, const char *filename, file_operating_mode mode) { switch (mode) { case file_mode_open_read: - file = SPIFFS.open(filename, "r"); + myFile = SPIFFS.open(filename, "r"); break; case file_mode_open_write: - file = SPIFFS.open(filename, "w"); + myFile = SPIFFS.open(filename, "w"); break; case file_mode_open_append: - file = SPIFFS.open(filename, "a"); + myFile = SPIFFS.open(filename, "a"); break; case file_mode_remove: @@ -290,6 +293,9 @@ class RealtimeDatabase default: break; } + + // Set the internal FS object with global File object. + file = myFile; } FileConfig fileConfig("/example.txt", fileCallback); @@ -315,20 +321,23 @@ class RealtimeDatabase ### Example ```cpp + #include + File myFile; // Define the File object globally. + void fileCallback(File &file, const char *filename, file_operating_mode mode) { switch (mode) { case file_mode_open_read: - file = SPIFFS.open(filename, "r"); + myFile = SPIFFS.open(filename, "r"); break; case file_mode_open_write: - file = SPIFFS.open(filename, "w"); + myFile = SPIFFS.open(filename, "w"); break; case file_mode_open_append: - file = SPIFFS.open(filename, "a"); + myFile = SPIFFS.open(filename, "a"); break; case file_mode_remove: @@ -338,6 +347,9 @@ class RealtimeDatabase default: break; } + + // Set the internal FS object with global File object. + file = myFile; } FileConfig fileConfig("/example.txt", fileCallback); @@ -530,20 +542,23 @@ class RealtimeDatabase ### Example ```cpp + #include + File myFile; // Define the File object globally. + void fileCallback(File &file, const char *filename, file_operating_mode mode) { switch (mode) { case file_mode_open_read: - file = SPIFFS.open(filename, "r"); + myFile = SPIFFS.open(filename, "r"); break; case file_mode_open_write: - file = SPIFFS.open(filename, "w"); + myFile = SPIFFS.open(filename, "w"); break; case file_mode_open_append: - file = SPIFFS.open(filename, "a"); + myFile = SPIFFS.open(filename, "a"); break; case file_mode_remove: @@ -553,6 +568,9 @@ class RealtimeDatabase default: break; } + + // Set the internal FS object with global File object. + file = myFile; } FileConfig fileConfig("/example.txt", fileCallback); @@ -579,20 +597,23 @@ class RealtimeDatabase ### Example ```cpp + #include + File myFile; // Define the File object globally. + void fileCallback(File &file, const char *filename, file_operating_mode mode) { switch (mode) { case file_mode_open_read: - file = SPIFFS.open(filename, "r"); + myFile = SPIFFS.open(filename, "r"); break; case file_mode_open_write: - file = SPIFFS.open(filename, "w"); + myFile = SPIFFS.open(filename, "w"); break; case file_mode_open_append: - file = SPIFFS.open(filename, "a"); + myFile = SPIFFS.open(filename, "a"); break; case file_mode_remove: @@ -602,6 +623,9 @@ class RealtimeDatabase default: break; } + + // Set the internal FS object with global File object. + file = myFile; } FileConfig fileConfig("/example.txt", fileCallback); @@ -727,20 +751,23 @@ class RealtimeDatabase ### Example ```cpp + #include + File myFile; // Define the File object globally. + void fileCallback(File &file, const char *filename, file_operating_mode mode) { switch (mode) { case file_mode_open_read: - file = SPIFFS.open(filename, "r"); + myFile = SPIFFS.open(filename, "r"); break; case file_mode_open_write: - file = SPIFFS.open(filename, "w"); + myFile = SPIFFS.open(filename, "w"); break; case file_mode_open_append: - file = SPIFFS.open(filename, "a"); + myFile = SPIFFS.open(filename, "a"); break; case file_mode_remove: @@ -750,6 +777,9 @@ class RealtimeDatabase default: break; } + + // Set the internal FS object with global File object. + file = myFile; } FileConfig fileConfig("/example.txt", fileCallback); @@ -777,20 +807,23 @@ class RealtimeDatabase ### Example ```cpp + #include + File myFile; // Define the File object globally. + void fileCallback(File &file, const char *filename, file_operating_mode mode) { switch (mode) { case file_mode_open_read: - file = SPIFFS.open(filename, "r"); + myFile = SPIFFS.open(filename, "r"); break; case file_mode_open_write: - file = SPIFFS.open(filename, "w"); + myFile = SPIFFS.open(filename, "w"); break; case file_mode_open_append: - file = SPIFFS.open(filename, "a"); + myFile = SPIFFS.open(filename, "a"); break; case file_mode_remove: @@ -800,6 +833,9 @@ class RealtimeDatabase default: break; } + + // Set the internal FS object with global File object. + file = myFile; } FileConfig fileConfig("/example.txt", fileCallback); diff --git a/src/cloud_storage/CloudStorage.h b/src/cloud_storage/CloudStorage.h index a3886a9..1b88234 100644 --- a/src/cloud_storage/CloudStorage.h +++ b/src/cloud_storage/CloudStorage.h @@ -1,5 +1,5 @@ /** - * Created July 26, 2024 + * Created July 27, 2024 * * The MIT License (MIT) * Copyright (c) 2024 K. Suwatchai (Mobizt) @@ -444,7 +444,7 @@ class CloudStorage options.requestType = requestType; options.parent = parent; - if (uploadOptions && strlen(uploadOptions->insertProps.c_str()) && (uploadOptions->uploadType == GoogleCloudStorage::upload_type_multipart || uploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable)) + if (uploadOptions && strlen(uploadOptions->insertProps.c_str()) && uploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable) options.payload = uploadOptions->insertProps.c_str(); async_request_handler_t::http_request_method method = async_request_handler_t::http_post; @@ -465,27 +465,29 @@ class CloudStorage options.extras += "?name="; options.extras += uut.encode(parent.getObject()); - options.extras += "&uploadType="; - if (uploadOptions && uploadOptions->uploadType == GoogleCloudStorage::upload_type_simple) - options.extras += "media"; - else if (uploadOptions && uploadOptions->uploadType == GoogleCloudStorage::upload_type_multipart) - options.extras += "multipart"; - else if (uploadOptions && uploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable) + size_t sz = 0; + + if (file && file->cb && file->filename.length()) { #if defined(ENABLE_FS) - // resumable upload is only for file bigger than 256k - if (file && file->cb) - { - file->cb(file->file, file->filename.c_str(), file_mode_open_read); - options.extras += file->file.size() < 256 * 1024 ? "multipart" : "resumable"; - file->file.close(); - } - else if (file && file->data_size) - { - options.extras += file->data_size < 256 * 1024 ? "multipart" : "resumable"; - } + file->cb(file->file, file->filename.c_str(), file_mode_open_read); + sz = file->file.size(); + file->file.close(); #endif } + else if (file && file->data_size) + sz = file->data_size; + + options.extras += "&uploadType="; + + // resumable upload is only for file bigger than 256k + if (uploadOptions && uploadOptions->uploadType == GoogleCloudStorage::upload_type_resumable && sz >= 256 * 1024) + options.extras += "resumable"; + else + { + options.extras += "media"; + options.payload.remove(0, options.payload.length()); + } } else if (requestType == GoogleCloudStorage::google_cloud_storage_request_type_delete) method = async_request_handler_t::http_delete; @@ -583,24 +585,6 @@ class CloudStorage sData->request.file_data.resumable.setSize(sData->request.file_data.file_size ? sData->request.file_data.file_size : sData->request.file_data.data_size); sData->request.file_data.resumable.updateRange(); } - else if (request.options->extras.indexOf("uploadType=multipart") > -1) - { - request.aClient->setContentType(sData, "multipart/related; boundary=" + sData->request.file_data.multipart.getBoundary()); - - ObjectWriter owriter; - JSONUtil jut; - String name, mime; - jut.addObject(name, "name", request.options->parent.getObject(), true, true); - jut.addObject(mime, "contentType", request.mime, true, true); - owriter.addMember(request.options->payload, name, false, "}"); - owriter.addMember(request.options->payload, mime, false, "}"); - sData->request.file_data.multipart.setOptions(request.options->payload); - request.options->payload.remove(0, request.options->payload.length()); - request.aClient->setFileContentLength(sData, sData->request.file_data.multipart.getOptions().length() + sData->request.file_data.multipart.getLast().length(), "Content-Length"); - sData->request.val[req_hndlr_ns::header] += "\r\n"; - sData->request.file_data.multipart.setSize(sData->request.file_data.file_size ? sData->request.file_data.file_size : sData->request.file_data.data_size); - sData->request.file_data.multipart.updateState(0); - } #endif } else @@ -610,7 +594,7 @@ class CloudStorage request.aClient->setFileContentLength(sData, 0); } - if (sData->request.file_data.file_size == 0) + if (sData->request.file_data.filename.length() > 0 && sData->request.file_data.file_size == 0) return setClientError(request, FIREBASE_ERROR_FILE_READ); if (request.options->extras.indexOf("uploadType=media") == -1) diff --git a/src/core/AsyncClient/AsyncClient.h b/src/core/AsyncClient/AsyncClient.h index 447ec6c..286c78d 100644 --- a/src/core/AsyncClient/AsyncClient.h +++ b/src/core/AsyncClient/AsyncClient.h @@ -285,19 +285,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase size_t totalLen = sData->request.file_data.file_size; bool fileopen = sData->request.payloadIndex == 0; -#if defined(ENABLE_CLOUD_STORAGE) - - if (sData->request.file_data.multipart.isEnabled()) - { - totalLen += sData->request.file_data.multipart.getOptions().length() + sData->request.file_data.multipart.getLast().length(); - if (sData->request.file_data.multipart.isEnabled() && sData->request.file_data.multipart.getState() == file_upload_multipart_data::multipart_state_send_options_payload) - return send(sData, reinterpret_cast(sData->request.file_data.multipart.getOptions().c_str()), sData->request.file_data.multipart.getOptions().length(), totalLen, async_state_send_payload); - else if (sData->request.file_data.multipart.isEnabled() && sData->request.file_data.multipart.getState() == file_upload_multipart_data::multipart_state_send_last_payload) - return send(sData, reinterpret_cast(sData->request.file_data.multipart.getLast().c_str()), sData->request.file_data.multipart.getLast().length(), totalLen, async_state_send_payload); - - fileopen |= sData->request.file_data.multipart.isEnabled() && sData->request.payloadIndex == sData->request.file_data.multipart.getOptions().length(); - } -#endif if (sData->upload) sData->upload_progress_enabled = true; @@ -372,8 +359,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase #if defined(ENABLE_CLOUD_STORAGE) if (sData->request.file_data.resumable.isEnabled() && sData->request.file_data.resumable.isUpload()) toSend = sData->request.file_data.resumable.getChunkSize(totalLen, sData->request.payloadIndex, sData->request.file_data.data_pos); - else if (sData->request.file_data.multipart.isEnabled() && sData->request.file_data.multipart.getState() == file_upload_multipart_data::multipart_state_send_data_payload) - toSend = sData->request.file_data.multipart.getChunkSize(totalLen, sData->request.payloadIndex, sData->request.file_data.data_pos); else #endif toSend = totalLen - sData->request.file_data.data_pos < FIREBASE_CHUNK_SIZE ? totalLen - sData->request.file_data.data_pos : FIREBASE_CHUNK_SIZE; @@ -466,15 +451,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase return sData->return_type; } } - else if (sData->request.file_data.multipart.isEnabled() && sData->request.file_data.multipart.isUpload()) - { - sData->request.file_data.multipart.updateState(sData->request.payloadIndex); - if (sData->request.file_data.multipart.isUpload()) - { - sData->return_type = function_return_type_continue; - return sData->return_type; - } - } else if (sData->request.payloadIndex < size) { sData->return_type = function_return_type_continue; @@ -518,8 +494,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase { if (sData->request.file_data.resumable.isEnabled()) sData->request.file_data.resumable.updateState(sData->request.payloadIndex); - else if (sData->request.file_data.multipart.isEnabled()) - sData->request.file_data.multipart.updateState(sData->request.payloadIndex); } #endif diff --git a/src/core/Core.h b/src/core/Core.h index 64c74bd..64a1bc2 100644 --- a/src/core/Core.h +++ b/src/core/Core.h @@ -7,7 +7,7 @@ #undef FIREBASE_CLIENT_VERSION #endif -#define FIREBASE_CLIENT_VERSION "1.3.7" +#define FIREBASE_CLIENT_VERSION "1.3.8" static void sys_idle() { diff --git a/src/core/FileConfig.h b/src/core/FileConfig.h index 1351bc1..d46e814 100644 --- a/src/core/FileConfig.h +++ b/src/core/FileConfig.h @@ -1,5 +1,5 @@ /** - * Created July 11, 2024 + * Created July 27, 2024 * * The MIT License (MIT) * Copyright (c) 2024 K. Suwatchai (Mobizt) @@ -192,169 +192,6 @@ struct file_upload_resumable_data String getLocation() { return location.c_str(); } }; -struct file_upload_multipart_data -{ - enum multipart_state - { - multipart_state_undefined, - multipart_state_send_header, - multipart_state_send_options_payload, - multipart_state_send_data_payload, - multipart_state_send_last_payload, - multipart_state_read_response - }; - -private: - bool enable = false; - int index = 0; - int size = 0; - int read = 0; - String boaundary, options_part, last_part; - multipart_state state = multipart_state_undefined; - -public: - file_upload_multipart_data() {} - bool isEnabled() { return enable; } - void setSize(size_t size) - { - this->size = size; - enable = size > 0; - } - int getChunkSize(int size, int payloadIndex, int dataIndex) - { - int chunkSize = size - dataIndex < FIREBASE_CHUNK_SIZE ? size - dataIndex : FIREBASE_CHUNK_SIZE; - - if (state == multipart_state_send_options_payload && options_part.length() && payloadIndex + chunkSize > index + (int)options_part.length()) - { - chunkSize = index + options_part.length() - payloadIndex; - } - else if (state == multipart_state_send_data_payload && size && payloadIndex + chunkSize > index + size) - { - chunkSize = index + size - payloadIndex; - } - else if (state == multipart_state_send_last_payload && last_part.length() && payloadIndex + chunkSize > index + (int)last_part.length()) - { - chunkSize = index + last_part.length() - payloadIndex; - } - - return chunkSize; - } - bool isUpload() { return state == multipart_state_send_options_payload || state == multipart_state_send_data_payload || state == multipart_state_send_last_payload; } - void updateState(uint32_t payloadIndex) - { - if (enable) - { - if (state == multipart_state_undefined) - { - state = multipart_state_send_header; - } - else if (state == multipart_state_send_header) - { - state = multipart_state_send_options_payload; - } - else if (state == multipart_state_send_options_payload) - { - if (options_part.length() && payloadIndex == index + options_part.length()) - { - state = multipart_state_send_data_payload; - index += options_part.length(); - } - } - else if (state == multipart_state_send_data_payload) - { - if (size && (int)payloadIndex >= index + size) - { - state = multipart_state_send_last_payload; - index += size; - } - } - else if (state == multipart_state_send_last_payload) - { - if (last_part.length() && payloadIndex == index + last_part.length()) - { - state = multipart_state_read_response; - index += last_part.length(); - } - } - } - } - - multipart_state getState() { return state; } - - void clear() - { - boaundary.remove(0, boaundary.length()); - options_part.remove(0, options_part.length()); - last_part.remove(0, last_part.length()); - enable = false; - size = 0; - index = 0; - state = multipart_state_undefined; - } - - void setOptions(const String &options) - { - options_part = "--"; - options_part += getBoundary(); - options_part += "\r\n"; - options_part += FPSTR("Content-Type: application/json; charset=UTF-8\r\n\r\n"); - options_part += options; - options_part += "\r\n\r\n"; - options_part += "--"; - options_part += getBoundary(); - options_part += "\r\n\r\n"; - } - - String getOptions() - { - return options_part; - } - - void setLast() - { - last_part = "\r\n--"; - last_part += getBoundary(); - last_part += "--"; - } - - String getLast() - { - setLast(); - return last_part; - } - - String getBoundary(bool newLine = false) - { - if (boaundary.length() == 0) - boaundary = makeBoundary(15); - if (newLine) - return String(boaundary + "\r\n"); - return boaundary; - } - - String makeBoundary(size_t len) - { - Memory mem; - String temp = FPSTR("=_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); - char *buf = reinterpret_cast(mem.alloc(len)); - if (len) - { - --len; - buf[0] = temp[0]; - buf[1] = temp[1]; - for (size_t n = 2; n < len; n++) - { - int key = rand() % (int)(temp.length() - 1); - buf[n] = temp[key]; - } - buf[len] = '\0'; - } - String out = buf; - mem.release(&buf); - return out; - } -}; - #endif struct file_config_data @@ -372,7 +209,6 @@ struct file_config_data #if defined(ENABLE_FS) && defined(ENABLE_CLOUD_STORAGE) file_upload_resumable_data resumable; - file_upload_multipart_data multipart; #endif #if defined(ENABLE_FS) FileConfigCallback cb = NULL; diff --git a/src/storage/Storage.h b/src/storage/Storage.h index 0795f60..bfc258f 100644 --- a/src/storage/Storage.h +++ b/src/storage/Storage.h @@ -1,5 +1,5 @@ /** - * Created July 26, 2024 + * Created July 27, 2024 * * The MIT License (MIT) * Copyright (c) 2024 K. Suwatchai (Mobizt) @@ -507,7 +507,7 @@ class Storage request.aClient->setFileContentLength(sData, 0); - if (sData->request.file_data.file_size == 0) + if (sData->request.file_data.filename.length() > 0 && sData->request.file_data.file_size == 0) return setClientError(request, FIREBASE_ERROR_FILE_READ); URLUtil uut;