forked from s60sc/ESP32-CAM_MJPEG2SD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.cpp
263 lines (244 loc) · 8.38 KB
/
ftp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Store SD card or SPIFFS content on a remote server using FTP
//
// s60sc 2022, based on code contributed by gemi254
#include "appGlobals.h"
// Ftp server params, setup via web page
char ftp_server[MAX_HOST_LEN];
uint16_t ftp_port = 21;
char ftp_user[MAX_HOST_LEN];
char FTP_Pass[MAX_PWD_LEN];
char ftp_wd[FILE_NAME_LEN];
uint8_t percentLoaded = 0;
bool useFtps = false;
// FTP control
static char rspBuf[256]; // Ftp response buffer
static char respCodeRx[4]; // ftp response code
TaskHandle_t ftpHandle = NULL;
static char storedPathName[FILE_NAME_LEN];
static bool uploadInProgress = false;
bool deleteAfter = false; // auto delete after upload
bool autoUpload = false; // Automatically upload every created file to remote ftp server
static fs::FS fp = STORAGE;
static byte* chunk;
#define NO_CHECK "999"
// WiFi Clients
WiFiClient rclient;
WiFiClient dclient;
static bool sendFtpCommand(const char* cmd, const char* param, const char* respCode, const char* respCode2 = NO_CHECK) {
// build and send ftp command
if (strlen(cmd)) {
rclient.print(cmd);
rclient.println(param);
}
LOG_DBG("Sent cmd: %s%s", cmd, param);
// wait for ftp server response
uint32_t start = millis();
while (!rclient.available() && millis() < start + (responseTimeoutSecs * 1000)) delay(1);
if (!rclient.available()) {
LOG_ERR("FTP server response timeout");
return false;
}
// read in response code and message
rclient.read((uint8_t*)respCodeRx, 3);
respCodeRx[3] = 0; // terminator
int readLen = rclient.read((uint8_t*)rspBuf, 255);
rspBuf[readLen] = 0;
while (rclient.available()) rclient.read(); // bin the rest of response
// check response code with expected
LOG_DBG("Rx code: %s, resp: %s", respCodeRx, rspBuf);
if (strcmp(respCode, NO_CHECK) == 0) return true; // response code not checked
if (strcmp(respCodeRx, respCode) != 0) {
if (strcmp(respCodeRx, respCode2) != 0) {
// incorrect response code
LOG_ERR("Command %s got wrong response: %s %s", cmd, respCodeRx, rspBuf);
return false;
}
}
return true;
}
static bool ftpConnect(){
// Connect to ftp or ftps
if (rclient.connect(ftp_server, ftp_port)) {LOG_DBG("FTP connected at %s:%u", ftp_server, ftp_port);}
else {
LOG_ERR("Error opening ftp connection to %s:%u", ftp_server, ftp_port);
return false;
}
if (!sendFtpCommand("", "", "220")) return false;
if (useFtps) {
if (sendFtpCommand("AUTH ", "TLS", "234")) {
/* NOT IMPLEMENTED */
} else LOG_WRN("FTPS not available");
}
if (!sendFtpCommand("USER ", ftp_user, "331")) return false;
if (!sendFtpCommand("PASS ", FTP_Pass, "230")) return false;
// change to supplied folder
if (!sendFtpCommand("CWD ", ftp_wd, "250")) return false;
if (!sendFtpCommand("Type I", "", "200")) return false;
return true;
}
static bool createFtpFolder(const char* folderName) {
// create folder if non existent then change to it
LOG_DBG("Check for folder %s", folderName);
sendFtpCommand("CWD ", folderName, NO_CHECK);
if (strcmp(respCodeRx, "550") == 0) {
// non existent folder, create it
if (!sendFtpCommand("MKD ", folderName, "257")) return false;
//sendFtpCommand("SITE CHMOD 755 ", folderName, "200", "550"); // unix only
if (!sendFtpCommand("CWD ", folderName, "250")) return false;
}
return true;
}
static bool getFolderName(const char* folderPath) {
// extract folder names from path name
char folderName[FILE_NAME_LEN];
strcpy(folderName, folderPath);
int pos = 1; // skip 1st '/'
// get each folder name in sequence
for (char* p = strchr(folderName, '/'); (p = strchr(++p, '/')) != NULL; pos = p + 1 - folderName) {
*p = 0; // terminator
if (!createFtpFolder(folderName + pos)) return false;
}
return true;
}
static bool openDataPort() {
// set up port for data transfer
if (!sendFtpCommand("PASV", "", "227")) return false;
// derive data port number
char* p = strchr(rspBuf, '('); // skip over initial text
int p1, p2;
int items = sscanf(p, "(%*d,%*d,%*d,%*d,%d,%d)", &p1, &p2);
if (items != 2) {
LOG_ERR("Failed to parse data port");
return false;
}
int dataPort = (p1 << 8) + p2;
// Connect to data port
LOG_DBG("Data port: %i", dataPort);
if (!dclient.connect(ftp_server, dataPort)) {
LOG_ERR("Data connection failed");
return false;
}
return true;
}
static bool ftpStoreFile(File &fh) {
// Upload individual file to current folder, overwrite any existing file
// reject if folder, or not valid file type
#ifdef ISCAM
if (strstr(fh.name(), AVI_EXT) == NULL && strstr(fh.name(), CSV_EXT) == NULL) return false;
#else
if (strstr(fh.name(), FILE_EXT) == NULL) return false;
#endif
char ftpSaveName[FILE_NAME_LEN];
strcpy(ftpSaveName, fh.name());
size_t fileSize = fh.size();
LOG_INF("Upload file: %s, size: %s", ftpSaveName, fmtSize(fileSize));
// open data connection
openDataPort();
uint32_t writeBytes = 0;
uint32_t uploadStart = millis();
size_t readLen, writeLen;
if (!sendFtpCommand("STOR ", ftpSaveName, "150", "125")) return false;
do {
// upload file in chunks
readLen = fh.read(chunk, CHUNKSIZE);
if (readLen) {
writeLen = dclient.write((const uint8_t*)chunk, readLen);
writeBytes += writeLen;
if (writeLen == 0) {
LOG_ERR("Upload file to ftp failed");
return false;
}
if (calcProgress(writeBytes, fileSize, 5, percentLoaded)) LOG_INF("Uploaded %u%%", percentLoaded);
}
} while (readLen > 0);
dclient.stop();
percentLoaded = 100;
bool res = sendFtpCommand("", "", "226");
if (res) {
LOG_ALT("Uploaded %s in %u sec", fmtSize(writeBytes), (millis() - uploadStart) / 1000);
//sendFtpCommand("SITE CHMOD 644 ", ftpSaveName, "200", "550"); // unix only
}
else LOG_ERR("File transfer not successful");
return res;
}
static bool uploadFolderOrFileFtp() {
// Upload a single file or whole folder using ftp
// folder is uploaded file by file
if (strlen(storedPathName) < 2){
LOG_DBG("Root or null is not allowed %s", storedPathName);
return false;
}
if (!ftpConnect()) {
LOG_ERR("Unable to make ftp connection");
return false;
}
File root = fp.open(storedPathName);
if (!root) {
LOG_ERR("Failed to open: %s", storedPathName);
return false;
}
bool res = false;
const int saveRefreshVal = refreshVal;
refreshVal = 1;
if (!root.isDirectory()) {
// Upload a single file
if (getFolderName(root.path())) res = ftpStoreFile(root);
#ifdef ISCAM
// upload corresponding csv file if exists
if (res) {
char ftpSaveName[FILE_NAME_LEN];
strcpy(ftpSaveName, root.path());
changeExtension(ftpSaveName, CSV_EXT);
if (fp.exists(ftpSaveName)) {
File csv = fp.open(ftpSaveName);
res = ftpStoreFile(csv);
csv.close();
}
}
#endif
} else {
// Upload a whole folder, file by file
LOG_INF("Uploading folder: ", root.name());
if (!createFtpFolder(root.name())) {
refreshVal = saveRefreshVal;
return false;
}
File fh = root.openNextFile();
while (fh) {
res = ftpStoreFile(fh);
if (!res) break; // abandon rest of files
fh.close();
fh = root.openNextFile();
}
if (fh) fh.close();
}
refreshVal = saveRefreshVal;
root.close();
return res;
}
static void FTPtask(void* parameter) {
// process an FTP request
#ifdef ISCAM
doPlayback = false; // close any current playback
#endif
chunk = psramFound() ? (byte*)ps_malloc(CHUNKSIZE) : (byte*)malloc(CHUNKSIZE);
bool res = uploadFolderOrFileFtp();
// Disconnect from ftp server
rclient.println("QUIT");
dclient.stop();
rclient.stop();
if (res && deleteAfter) deleteFolderOrFile(storedPathName);
uploadInProgress = false;
free(chunk);
vTaskDelete(NULL);
}
bool ftpFileOrFolder(const char* fileFolder) {
// called from other functions to commence FTP upload
setFolderName(fileFolder, storedPathName);
if (!uploadInProgress) {
uploadInProgress = true;
xTaskCreate(&FTPtask, "FTPtask", FTP_STACK_SIZE, NULL, 1, &ftpHandle);
return true;
} else LOG_WRN("Unable to upload %s as another upload in progress", storedPathName);
return false;
}