mirror of
https://github.com/Telecominfraproject/wlan-cloud-ucentralsec.git
synced 2025-10-29 18:02:29 +00:00
Moving Avatars into ORM
This commit is contained in:
@@ -22,9 +22,8 @@ namespace OpenWifi {
|
||||
Name_ = Parameters.get(RESTAPI::Protocol::NAME, RESTAPI::Protocol::UNNAMED);
|
||||
}
|
||||
Poco::CountingInputStream InputStream(Stream);
|
||||
std::ofstream OutputStream(TempFile_.path(), std::ofstream::out);
|
||||
Poco::StreamCopier::copyStream(InputStream, OutputStream);
|
||||
Length_ = InputStream.chars();
|
||||
Poco::StreamCopier::copyStream(InputStream, OutputStream_);
|
||||
Length_ = OutputStream_.str().size();
|
||||
};
|
||||
|
||||
void RESTAPI_avatar_handler::DoPost() {
|
||||
@@ -38,8 +37,8 @@ namespace OpenWifi {
|
||||
// if there is an avatar, just remove it...
|
||||
StorageService()->AvatarDB().DeleteAvatar(UserInfo_.userinfo.email,Id);
|
||||
|
||||
Poco::TemporaryFile TmpFile;
|
||||
AvatarPartHandler partHandler(Id, Logger_, TmpFile);
|
||||
std::stringstream SS;
|
||||
AvatarPartHandler partHandler(Id, Logger_, SS);
|
||||
|
||||
Poco::Net::HTMLForm form(*Request, Request->stream(), partHandler);
|
||||
Poco::JSON::Object Answer;
|
||||
@@ -48,7 +47,7 @@ namespace OpenWifi {
|
||||
Answer.set(RESTAPI::Protocol::ERRORCODE, 0);
|
||||
Logger_.information(Poco::format("Uploaded avatar: %s Type: %s", partHandler.Name(), partHandler.ContentType()));
|
||||
StorageService()->AvatarDB().SetAvatar(UserInfo_.userinfo.email,
|
||||
Id, TmpFile, partHandler.ContentType(), partHandler.Name());
|
||||
Id, SS.str(), partHandler.ContentType(), partHandler.Name());
|
||||
} else {
|
||||
Answer.set(RESTAPI::Protocol::AVATARID, Id);
|
||||
Answer.set(RESTAPI::Protocol::ERRORCODE, 13);
|
||||
@@ -62,13 +61,11 @@ namespace OpenWifi {
|
||||
if (Id.empty()) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
std::string Type, Name, AvatarContent;
|
||||
if (!StorageService()->AvatarDB().GetAvatar(UserInfo_.userinfo.email, Id, AvatarContent, Type, Name)) {
|
||||
return NotFound();
|
||||
}
|
||||
std::cout << "Sending avatar" << std::endl;
|
||||
SendFileContent(AvatarContent, Type, Name);
|
||||
return SendFileContent(AvatarContent, Type, Name);
|
||||
}
|
||||
|
||||
void RESTAPI_avatar_handler::DoDelete() {
|
||||
|
||||
@@ -9,23 +9,23 @@ namespace OpenWifi {
|
||||
|
||||
class AvatarPartHandler : public Poco::Net::PartHandler {
|
||||
public:
|
||||
AvatarPartHandler(std::string Id, Poco::Logger &Logger, Poco::TemporaryFile &TmpFile) :
|
||||
AvatarPartHandler(std::string Id, Poco::Logger &Logger, std::stringstream & ofs) :
|
||||
Id_(std::move(Id)),
|
||||
Logger_(Logger),
|
||||
TempFile_(TmpFile){
|
||||
OutputStream_(ofs){
|
||||
}
|
||||
void handlePart(const Poco::Net::MessageHeader &Header, std::istream &Stream);
|
||||
[[nodiscard]] uint64_t Length() const { return Length_; }
|
||||
[[nodiscard]] std::string &Name() { return Name_; }
|
||||
[[nodiscard]] std::string &ContentType() { return FileType_; }
|
||||
[[nodiscard]] std::string FileName() const { return TempFile_.path(); }
|
||||
|
||||
private:
|
||||
uint64_t Length_ = 0;
|
||||
std::string FileType_;
|
||||
std::string Name_;
|
||||
std::string Id_;
|
||||
Poco::Logger &Logger_;
|
||||
Poco::TemporaryFile &TempFile_;
|
||||
std::stringstream &OutputStream_;
|
||||
};
|
||||
|
||||
class RESTAPI_avatar_handler : public RESTAPIHandler {
|
||||
|
||||
@@ -27,20 +27,16 @@ namespace OpenWifi {
|
||||
DB(T, Name.c_str(), AvatarDB_Fields,{}, P, L, ShortName.c_str()) {
|
||||
}
|
||||
|
||||
bool AvatarDB::SetAvatar(const std::string &Admin, std::string &Id, Poco::TemporaryFile &FileName,
|
||||
bool AvatarDB::SetAvatar(const std::string &Admin, std::string &Id, const std::string & AvatarContent,
|
||||
std::string &Type, std::string &Name) {
|
||||
|
||||
try {
|
||||
std::stringstream ss;
|
||||
std::ifstream ifs(FileName.path().c_str(), std::ios_base::in | std::ios_base::binary);
|
||||
Poco::StreamCopier::copyStream(ifs, ss);
|
||||
|
||||
SecurityObjects::Avatar A;
|
||||
A.id = Id;
|
||||
A.type = Type;
|
||||
A.name = Name;
|
||||
A.created = std::time(nullptr);
|
||||
A.avatar.assignRaw(ss.str().c_str(), ss.str().size());
|
||||
A.avatar.assignRaw(AvatarContent.c_str(), AvatarContent.size());
|
||||
|
||||
if (Exists("id", Id)) {
|
||||
return UpdateRecord("id", Id, A);
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace OpenWifi {
|
||||
public:
|
||||
AvatarDB( const std::string &name, const std::string &shortname, OpenWifi::DBType T, Poco::Data::SessionPool & P, Poco::Logger &L);
|
||||
|
||||
bool SetAvatar(const std::string & Admin, std::string &Id, Poco::TemporaryFile &FileName, std::string &Type, std::string & Name);
|
||||
bool SetAvatar(const std::string & Admin, std::string &Id, const std::string & AvatarContent, std::string &Type, std::string & Name);
|
||||
bool GetAvatar(const std::string & Admin, std::string &Id, std::string & AvatarContent, std::string &Type, std::string & Name);
|
||||
bool DeleteAvatar(const std::string & Admin, std::string &Id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user