FtpDataStore.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FtpDataStore.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "FtpDataStream.h"
14 #include "FileSystem.h"
15 
17 {
18 public:
19  FtpDataStore(FtpServerConnection* connection, const String& fileName)
20  : FtpDataStream(connection), file(fileOpen(fileName, eFO_WriteOnly | eFO_CreateNewAlways))
21  {
22  }
23 
25  {
26  fileClose(file);
27  }
28 
29  err_t onReceive(pbuf* buf) override
30  {
31  if(completed) {
32  return TcpConnection::onReceive(buf);
33  }
34 
35  if(buf == nullptr) {
36  completed = true;
37  response(226, "Transfer completed");
38  return TcpConnection::onReceive(buf);
39  }
40 
41  pbuf* cur = buf;
42  while(cur != nullptr && cur->len > 0) {
43  fileWrite(file, (uint8_t*)cur->payload, cur->len);
44  cur = cur->next;
45  }
46 
47  return TcpConnection::onReceive(buf);
48  }
49 
50 private:
51  file_t file;
52 };
Definition: FtpDataStore.h:16
err_t onReceive(pbuf *buf) override
Definition: FtpDataStore.h:29
Definition: FtpDataStream.h:16
void fileClose(file_t file)
Clode file.
The String class.
Definition: WString.h:136
FtpDataStore(FtpServerConnection *connection, const String &fileName)
Definition: FtpDataStore.h:19
void response(int code, String text=nullptr)
Definition: FtpDataStream.h:46
bool completed
Definition: FtpDataStream.h:74
Definition: FtpServerConnection.h:29
signed short file_t
File handle.
Definition: FileSystem.h:22
file_t fileOpen(const String &name, FileOpenFlags flags)
Open file.
int fileWrite(file_t file, const void *data, size_t size)
Write to file.
~FtpDataStore()
Definition: FtpDataStore.h:24
virtual err_t onReceive(pbuf *buf)
Create new file even if file exists.
Definition: FileSystem.h:32
Write only file.
Definition: FileSystem.h:27