blob: 204f2413f7be9b4f002418201baa70cedcfe4058 (
plain)
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
|
/*
* UploadingNetworkResponder.cpp
*
* Created on: 3 Feb 2019
* Author: David
*/
#include "UploadingNetworkResponder.h"
#include "Socket.h"
#include "Platform.h"
UploadingNetworkResponder::UploadingNetworkResponder(NetworkResponder *n) : NetworkResponder(n), uploadError(false)
{
}
// This is called when we lose a connection or when we are asked to terminate. Overridden in some derived classes.
void UploadingNetworkResponder::ConnectionLost()
{
CancelUpload();
NetworkResponder::ConnectionLost();
}
// Start writing to a new file
void UploadingNetworkResponder::StartUpload(FileStore *file, const char *fileName)
{
fileBeingUploaded.Set(file);
filenameBeingProcessed.copy(fileName);
responderState = ResponderState::uploading;
uploadError = false;
}
// If this responder has an upload in progress, cancel it
void UploadingNetworkResponder::CancelUpload()
{
if (fileBeingUploaded.IsLive())
{
fileBeingUploaded.Close();
if (!filenameBeingProcessed.IsEmpty())
{
GetPlatform().GetMassStorage()->Delete(nullptr, filenameBeingProcessed.c_str());
filenameBeingProcessed.Clear();
}
}
}
// Finish a file upload. Set variable uploadError if anything goes wrong.
void UploadingNetworkResponder::FinishUpload(uint32_t fileLength, time_t fileLastModified)
{
// Flush remaining data for FSO
if (!fileBeingUploaded.Flush())
{
uploadError = true;
GetPlatform().Message(ErrorMessage, "Could not flush remaining data while finishing upload!\n");
}
// Check the file length is as expected
if (fileLength != 0 && fileBeingUploaded.Length() != fileLength)
{
uploadError = true;
GetPlatform().MessageF(ErrorMessage, "Uploaded file size is different (%lu vs. expected %lu bytes)!\n", fileBeingUploaded.Length(), fileLength);
}
// Close the file
if (fileBeingUploaded.IsLive())
{
fileBeingUploaded.Close();
}
// Delete the file again if an error has occurred
if (!filenameBeingProcessed.IsEmpty())
{
if (uploadError)
{
GetPlatform().GetMassStorage()->Delete(nullptr, filenameBeingProcessed.c_str());
}
else if (fileLastModified != 0)
{
// Update the file timestamp if it was specified
(void)GetPlatform().GetMassStorage()->SetLastModifiedTime(nullptr, filenameBeingProcessed.c_str(), fileLastModified);
}
filenameBeingProcessed.Clear();
}
}
// End
|