mirror of
https://github.com/Telecominfraproject/wlan-cloud-ucentralsec.git
synced 2025-11-01 11:17:51 +00:00
84 lines
3.2 KiB
C++
84 lines
3.2 KiB
C++
//
|
|
// Created by stephane bourque on 2021-07-15.
|
|
//
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
#include "RESTAPI_avatarHandler.h"
|
|
#include "StorageService.h"
|
|
#include "Poco/Net/HTMLForm.h"
|
|
#include "framework/RESTAPI_protocol.h"
|
|
#include "framework/MicroService.h"
|
|
|
|
namespace OpenWifi {
|
|
|
|
void AvatarPartHandler::handlePart(const Poco::Net::MessageHeader &Header, std::istream &Stream) {
|
|
FileType_ = Header.get(RESTAPI::Protocol::CONTENTTYPE, RESTAPI::Protocol::UNSPECIFIED);
|
|
if (Header.has(RESTAPI::Protocol::CONTENTDISPOSITION)) {
|
|
std::string Disposition;
|
|
Poco::Net::NameValueCollection Parameters;
|
|
Poco::Net::MessageHeader::splitParameters(Header[RESTAPI::Protocol::CONTENTDISPOSITION], Disposition, Parameters);
|
|
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();
|
|
};
|
|
|
|
void RESTAPI_avatarHandler::DoPost() {
|
|
std::string Id = GetBinding(RESTAPI::Protocol::ID, "");
|
|
SecurityObjects::UserInfo UInfo;
|
|
|
|
if (Id.empty() || !StorageService()->GetUserById(Id, UInfo)) {
|
|
return NotFound();
|
|
}
|
|
|
|
// if there is an avatar, just remove it...
|
|
StorageService()->DeleteAvatar(UserInfo_.userinfo.email,Id);
|
|
|
|
Poco::TemporaryFile TmpFile;
|
|
AvatarPartHandler partHandler(Id, Logger_, TmpFile);
|
|
|
|
Poco::Net::HTMLForm form(*Request, Request->stream(), partHandler);
|
|
Poco::JSON::Object Answer;
|
|
if (!partHandler.Name().empty() && partHandler.Length()< MicroService::instance().ConfigGetInt("openwifi.avatar.maxsize",2000000)) {
|
|
Answer.set(RESTAPI::Protocol::AVATARID, Id);
|
|
Answer.set(RESTAPI::Protocol::ERRORCODE, 0);
|
|
Logger_.information(Poco::format("Uploaded avatar: %s Type: %s", partHandler.Name(), partHandler.ContentType()));
|
|
StorageService()->SetAvatar(UserInfo_.userinfo.email,
|
|
Id, TmpFile, partHandler.ContentType(), partHandler.Name());
|
|
} else {
|
|
Answer.set(RESTAPI::Protocol::AVATARID, Id);
|
|
Answer.set(RESTAPI::Protocol::ERRORCODE, 13);
|
|
Answer.set(RESTAPI::Protocol::ERRORTEXT, "Avatar upload could not complete.");
|
|
}
|
|
ReturnObject(Answer);
|
|
}
|
|
|
|
void RESTAPI_avatarHandler::DoGet() {
|
|
std::string Id = GetBinding(RESTAPI::Protocol::ID, "");
|
|
if (Id.empty()) {
|
|
return NotFound();
|
|
}
|
|
Poco::TemporaryFile TempAvatar;
|
|
std::string Type, Name;
|
|
if (!StorageService()->GetAvatar(UserInfo_.userinfo.email, Id, TempAvatar, Type, Name)) {
|
|
return NotFound();
|
|
}
|
|
SendFile(TempAvatar, Type, Name);
|
|
}
|
|
|
|
void RESTAPI_avatarHandler::DoDelete() {
|
|
std::string Id = GetBinding(RESTAPI::Protocol::ID, "");
|
|
if (Id.empty()) {
|
|
return NotFound();
|
|
}
|
|
if (!StorageService()->DeleteAvatar(UserInfo_.userinfo.email, Id)) {
|
|
return NotFound();
|
|
}
|
|
OK();
|
|
}
|
|
}
|