file saving and managing
This commit is contained in:
parent
e372c03a19
commit
353e3123f3
2
Makefile
2
Makefile
@ -25,4 +25,4 @@ $(BUILD_DIR)/%.o: %.cpp
|
|||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f twmailer-client twmailer-server
|
rm -rf $(BUILD_DIR) twmailer-client twmailer-server
|
||||||
30
mail.h
30
mail.h
@ -1,21 +1,43 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
#include "user_handler.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
struct mail {
|
struct mail {
|
||||||
std::string filename;
|
std::string filename;
|
||||||
|
|
||||||
/* metadata */
|
/* metadata */
|
||||||
|
u_int id;
|
||||||
int64_t timestamp;
|
int64_t timestamp;
|
||||||
std::string sender;
|
std::string sender;
|
||||||
std::vector<std::string> receipient;
|
std::vector<std::string> recipients;
|
||||||
std::string subject;
|
std::string subject;
|
||||||
|
|
||||||
const bool operator<(const mail& left) {
|
bool operator()(const u_int& id) const {
|
||||||
|
return id == this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<(const mail& left) const {
|
||||||
return this->timestamp < left.timestamp;
|
return this->timestamp < left.timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove();
|
fs::path getPath() {
|
||||||
|
if (this->filename.empty())
|
||||||
|
return fs::path();
|
||||||
|
return fs::path(this->filename.insert(2, "/"));
|
||||||
|
};
|
||||||
|
|
||||||
|
void remove() {
|
||||||
|
if (this->filename.empty())
|
||||||
|
return;
|
||||||
|
std::remove((user_handler::getInstance()->getSpoolDir()/"objects"/fs::path(this->filename.insert(2, "/"))).c_str());
|
||||||
|
|
||||||
|
this->filename = "";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
33
server.cpp
33
server.cpp
@ -6,6 +6,10 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <boost/compute/detail/sha1.hpp>
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@ -31,6 +35,9 @@ int new_socket = -1;
|
|||||||
void printUsage();
|
void printUsage();
|
||||||
inline bool isInteger(const std::string & s);
|
inline bool isInteger(const std::string & s);
|
||||||
|
|
||||||
|
void saveToFile(fs::path object_dir, std::string message);
|
||||||
|
std::string get_sha1(const std::string& p_arg);
|
||||||
|
|
||||||
// from myserver.c
|
// from myserver.c
|
||||||
void *clientCommunication(void *data);
|
void *clientCommunication(void *data);
|
||||||
void signalHandler(int sig);
|
void signalHandler(int sig);
|
||||||
@ -281,3 +288,29 @@ void signalHandler(int sig)
|
|||||||
exit(sig);
|
exit(sig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void saveToFile(fs::path object_dir, std::string message)
|
||||||
|
{
|
||||||
|
std::string sha1 = get_sha1(message);
|
||||||
|
std::ofstream ofs(object_dir/sha1); // possible issues with path length or file length limitations
|
||||||
|
ofs << message;
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/28489153/how-to-portably-compute-a-sha1-hash-in-c
|
||||||
|
std::string get_sha1(const std::string& p_arg)
|
||||||
|
{
|
||||||
|
boost::uuids::detail::sha1 sha1;
|
||||||
|
sha1.process_bytes(p_arg.data(), p_arg.size());
|
||||||
|
unsigned hash[5] = {0};
|
||||||
|
sha1.get_digest(hash);
|
||||||
|
|
||||||
|
// Back to string
|
||||||
|
char buf[41] = {0};
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
std::sprintf(buf + (i << 3), "%08x", hash[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
1
user.cpp
1
user.cpp
@ -1,5 +1,6 @@
|
|||||||
#include "user.h"
|
#include "user.h"
|
||||||
#include "user_handler.h"
|
#include "user_handler.h"
|
||||||
|
#include "mail.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
4
user.h
4
user.h
@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "mail.h"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -11,6 +9,8 @@
|
|||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
struct mail;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static const bool ptr_cmp(T* left, T* right) { return *left < *right; };
|
static const bool ptr_cmp(T* left, T* right) { return *left < *right; };
|
||||||
|
|
||||||
|
|||||||
@ -25,6 +25,7 @@ public:
|
|||||||
~user_handler();
|
~user_handler();
|
||||||
|
|
||||||
void setSpoolDir(fs::path p) { this->spool_dir = p; };
|
void setSpoolDir(fs::path p) { this->spool_dir = p; };
|
||||||
|
fs::path getSpoolDir() { return this->spool_dir; };
|
||||||
user* getUser(std::string name) { return this->users[name]; };
|
user* getUser(std::string name) { return this->users[name]; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user