|
| 1 | +#include "FTPServer.h" |
| 2 | +#include <stdint.h> |
| 3 | +#include <fstream> |
| 4 | +#include <dirent.h> |
| 5 | +#include <esp_log.h> |
| 6 | + |
| 7 | +static const char* LOG_TAG = "FTPCallbacks"; |
| 8 | +/** |
| 9 | + * Called at the start of a STOR request. The file name is the name of the file the client would like to |
| 10 | + * save. |
| 11 | + */ |
| 12 | +void FTPFileCallbacks::onStoreStart(std::string fileName) { |
| 13 | + ESP_LOGD(LOG_TAG, ">> FTPFileCallbacks::onStoreStart: fileName=%s", fileName.c_str()); |
| 14 | + m_storeFile.open(fileName, std::ios::binary); // Open the file for writing. |
| 15 | + if (m_storeFile.fail()) { |
| 16 | + throw FTPServer::FileException(); |
| 17 | + } |
| 18 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onStoreStart"); |
| 19 | +} // FTPFileCallbacks#onStoreStart |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * Called when the client presents a new chunk of data to be saved. |
| 24 | + */ |
| 25 | +size_t FTPFileCallbacks::onStoreData(uint8_t* data, size_t size) { |
| 26 | + ESP_LOGD(LOG_TAG,">> FTPFileCallbacks::onStoreData: size=%d", size); |
| 27 | + m_storeFile.write((char *)data, size); // Store data received. |
| 28 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onStoreData: size=%d", size); |
| 29 | + return size; |
| 30 | +} // FTPFileCallbacks#onStoreData |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Called at the end of a STOR request. This indicates that the client has completed its transmission of the |
| 35 | + * file. |
| 36 | + */ |
| 37 | +void FTPFileCallbacks::onStoreEnd() { |
| 38 | + ESP_LOGD(LOG_TAG,">> FTPFileCallbacks::onStoreEnd"); |
| 39 | + m_storeFile.close(); // Close the open file. |
| 40 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onStoreEnd"); |
| 41 | +} // FTPFileCallbacks#onStoreEnd |
| 42 | + |
| 43 | + |
| 44 | +/** |
| 45 | + * Called when the client requests retrieval of a file. |
| 46 | + */ |
| 47 | +void FTPFileCallbacks::onRetrieveStart(std::string fileName) { |
| 48 | + ESP_LOGD(LOG_TAG,">> FTPFileCallbacks::onRetrieveStart: fileName=%s", fileName.c_str()); |
| 49 | + m_byteCount = 0; |
| 50 | + m_retrieveFile.open(fileName, std::ios::binary); |
| 51 | + if (m_retrieveFile.fail()) { |
| 52 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onRetrieveStart: ***FileException***"); |
| 53 | + throw FTPServer::FileException(); |
| 54 | + } |
| 55 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onRetrieveStart"); |
| 56 | +} // FTPFileCallbacks#onRetrieveStart |
| 57 | + |
| 58 | + |
| 59 | +/** |
| 60 | + * Called when the client is ready to receive the next piece of the file. To indicate that there |
| 61 | + * is no more data to send, return a size of 0. |
| 62 | + * @param data The data buffer that we can fill to return data back to the client. |
| 63 | + * @param size The maximum size of the data buffer that we can populate. |
| 64 | + * @return The size of data being returned. Return 0 to indicate that there is no more data to return. |
| 65 | + */ |
| 66 | +size_t FTPFileCallbacks::onRetrieveData(uint8_t* data, size_t size) { |
| 67 | + ESP_LOGD(LOG_TAG,">> FTPFileCallbacks::onRetrieveData"); |
| 68 | + m_retrieveFile.read((char *)data, size); |
| 69 | + size_t readSize = m_retrieveFile.gcount(); |
| 70 | + m_byteCount += readSize; |
| 71 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onRetrieveData: sizeRead=%d", readSize); |
| 72 | + return m_retrieveFile.gcount(); // Return the number of bytes read. |
| 73 | +} // FTPFileCallbacks#onRetrieveData |
| 74 | + |
| 75 | + |
| 76 | +/** |
| 77 | + * Called when the retrieval has been completed. |
| 78 | + */ |
| 79 | +void FTPFileCallbacks::onRetrieveEnd() { |
| 80 | + ESP_LOGD(LOG_TAG,">> FTPFileCallbacks::onRetrieveEnd"); |
| 81 | + m_retrieveFile.close(); |
| 82 | + ESP_LOGD(LOG_TAG,"<< FTPFileCallbacks::onRetrieveEnd: bytesTransmitted=%d", m_byteCount); |
| 83 | +} // FTPFileCallbacks#onRetrieveEnd |
| 84 | + |
| 85 | + |
| 86 | +/** |
| 87 | + * Return a list of files in the file system. |
| 88 | + * @return a list of files in the file system. |
| 89 | + */ |
| 90 | +std::string FTPFileCallbacks::onDir() { |
| 91 | + |
| 92 | + DIR* dir = opendir(FTPServer::getCurrentDirectory().c_str()); |
| 93 | + std::stringstream ss; |
| 94 | + while(1) { |
| 95 | + struct dirent* pDirentry = readdir(dir); |
| 96 | + if (pDirentry == nullptr) { |
| 97 | + break; |
| 98 | + } |
| 99 | + ss << pDirentry->d_name << "\r\n"; |
| 100 | + } |
| 101 | + closedir(dir); |
| 102 | + return ss.str(); |
| 103 | +} // FTPFileCallbacks#onDir |
| 104 | + |
| 105 | + |
| 106 | +/// ---- END OF FTPFileCallbacks |
| 107 | + |
| 108 | + |
| 109 | +void FTPCallbacks::onStoreStart(std::string fileName) { |
| 110 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onStoreStart: fileName=%s", fileName.c_str()); |
| 111 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onStoreStart"); |
| 112 | +} // FTPCallbacks#onStoreStart |
| 113 | + |
| 114 | + |
| 115 | +size_t FTPCallbacks::onStoreData(uint8_t* data, size_t size) { |
| 116 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onStoreData: size=%d", size); |
| 117 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onStoreData"); |
| 118 | + return 0; |
| 119 | +} // FTPCallbacks#onStoreData |
| 120 | + |
| 121 | + |
| 122 | +void FTPCallbacks::onStoreEnd() { |
| 123 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onStoreEnd"); |
| 124 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onStoreEnd"); |
| 125 | +} // FTPCallbacks#onStoreEnd |
| 126 | + |
| 127 | + |
| 128 | +void FTPCallbacks::onRetrieveStart(std::string fileName) { |
| 129 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onRetrieveStart"); |
| 130 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onRetrieveStart"); |
| 131 | +} // FTPCallbacks#onRetrieveStart |
| 132 | + |
| 133 | + |
| 134 | +size_t FTPCallbacks::onRetrieveData(uint8_t *data, size_t size) { |
| 135 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onRetrieveData"); |
| 136 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onRetrieveData: 0"); |
| 137 | + return 0; |
| 138 | +} // FTPCallbacks#onRetrieveData |
| 139 | + |
| 140 | + |
| 141 | +void FTPCallbacks::onRetrieveEnd() { |
| 142 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onRetrieveEnd"); |
| 143 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onRetrieveEnd"); |
| 144 | +} // FTPCallbacks#onRetrieveEnd |
| 145 | + |
| 146 | + |
| 147 | +std::string FTPCallbacks::onDir() { |
| 148 | + ESP_LOGD(LOG_TAG,">> FTPCallbacks::onDir"); |
| 149 | + ESP_LOGD(LOG_TAG,"<< FTPCallbacks::onDir"); |
| 150 | + return ""; |
| 151 | +} // FTPCallbacks#onDir |
| 152 | + |
| 153 | +FTPCallbacks::~FTPCallbacks() { |
| 154 | + |
| 155 | +} // FTPCallbacks#~FTPCallbacks |
0 commit comments