-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
306 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "file.h" | ||
#include <fstream> | ||
#include <net/stream.h> | ||
#include <openssl/evp.h> | ||
#include <utils/logger.h> | ||
|
||
File::File() : path("") | ||
{ | ||
} | ||
|
||
File::File(std::string path) : path(path) | ||
{ | ||
load(); | ||
} | ||
|
||
File::~File() | ||
{ | ||
} | ||
|
||
bool File::load() | ||
{ | ||
std::ifstream file(path, std::ios::binary | std::ios::ate); | ||
std::streamsize size = file.tellg(); | ||
file.seekg(0, std::ios::beg); | ||
|
||
data.resize(size); | ||
file.read(data.data(), size); | ||
|
||
return file.good(); | ||
} | ||
|
||
void File::setPath(std::string path) | ||
{ | ||
this->path = path; | ||
} | ||
|
||
const std::string &File::getPath() const | ||
{ | ||
return path; | ||
} | ||
|
||
const char *File::getPointer() const | ||
{ | ||
return data.data(); | ||
} | ||
|
||
int File::getSize() const | ||
{ | ||
return data.size(); | ||
} | ||
|
||
PNGFile::PNGFile() : File() | ||
{ | ||
} | ||
|
||
PNGFile::PNGFile(std::string path) : File(path), width(0), height(0) | ||
{ | ||
MemoryStream m; | ||
m.write(reinterpret_cast<std::byte *>(const_cast<char *>(getPointer())), 1, 15 + 8); | ||
|
||
char buff[3]; | ||
m.read(reinterpret_cast<std::byte *>(buff), 0, 3); | ||
|
||
if (buff[0] != 'P' || buff[1] != 'N' || buff[2] != 'G') | ||
return; | ||
|
||
// Crafty way to just skip 12 bytes | ||
m.readLong(); | ||
m.readInt(); | ||
|
||
int32_t temp = m.readInt(); | ||
width = *reinterpret_cast<unsigned int *>(&temp); | ||
temp = m.readInt(); | ||
height = *reinterpret_cast<unsigned int *>(&temp); | ||
|
||
char encoded[((getSize() + 2) / 3) * 4]; | ||
EVP_EncodeBlock(reinterpret_cast<unsigned char *>(encoded), reinterpret_cast<unsigned char *>(const_cast<char *>(getPointer())), getSize()); | ||
base64String = std::string(encoded, ((getSize() + 2) / 3) * 4); | ||
} | ||
|
||
PNGFile::~PNGFile() | ||
{ | ||
} | ||
|
||
unsigned int PNGFile::getWidth() const | ||
{ | ||
return width; | ||
} | ||
|
||
unsigned int PNGFile::getHeight() const | ||
{ | ||
return height; | ||
} | ||
|
||
const std::string &PNGFile::getBase64String() const | ||
{ | ||
return base64String; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
* @file file.h | ||
* @author Mathieu Cayeux | ||
* @brief The file containing file (lol) loading logic | ||
* @version 0.1 | ||
* @date 2023-06-19 | ||
* | ||
* @copyright Copyright (c) 2023 | ||
* | ||
*/ | ||
|
||
#ifndef MINESERVER_FILE_H | ||
#define MINESERVER_FILE_H | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
/** | ||
* @brief File Loader Wrapper | ||
* | ||
*/ | ||
class File | ||
{ | ||
private: | ||
std::vector<char> data; | ||
std::string path; | ||
|
||
public: | ||
/** | ||
* @brief Constructs a new File object | ||
* | ||
* Constructs an empty file object, really | ||
* just used for the config or placeholder | ||
* objects. | ||
*/ | ||
File(); | ||
/** | ||
* @brief Construct a new File object | ||
* | ||
* Internally calls File::load() | ||
* @param path The Path of the file | ||
*/ | ||
File(std::string path); | ||
/** | ||
* @brief Destroy the File object | ||
* | ||
*/ | ||
~File(); | ||
|
||
/** | ||
* @brief Loads the data of the file into ram | ||
* | ||
* @return true file was loaded correctly | ||
* @return false file was not loaded correctly | ||
*/ | ||
bool load(); | ||
|
||
/** | ||
* @brief Set the Path of the file | ||
* | ||
* You should call File::load() to load | ||
* the file data afterwards. | ||
* @param path the path of the targeted file | ||
*/ | ||
void setPath(std::string path); | ||
/** | ||
* @brief Get the Path object | ||
* | ||
* @warning The path does not assure that the data | ||
* stored in this object contains the data | ||
* of the file at the path. | ||
* @return const std::string& the path of the file | ||
*/ | ||
const std::string &getPath() const; | ||
|
||
/** | ||
* @brief Get the pointer to the data | ||
* | ||
* @return const char* the pointer to the file data | ||
*/ | ||
const char *getPointer() const; | ||
/** | ||
* @brief Get the size of the file stored | ||
* | ||
* @return int the size of the file stored | ||
*/ | ||
int getSize() const; | ||
}; | ||
|
||
/** | ||
* @brief Wrapper around ::File for PNG files | ||
* | ||
*/ | ||
class PNGFile : public File | ||
{ | ||
private: | ||
unsigned int width, height; | ||
std::string base64String; | ||
|
||
public: | ||
/** | ||
* @brief Construct a new PNGFile object | ||
* | ||
* Constructs an empty file object, really | ||
* just used for the config or placeholder | ||
* objects. | ||
*/ | ||
PNGFile(); | ||
/** | ||
* @brief Construct a new PNGFile object | ||
* | ||
* Internally calls File::load(), then | ||
* does few calculations for loading | ||
* data used in the server. | ||
* @param path The Path of the file | ||
*/ | ||
PNGFile(std::string path); | ||
/** | ||
* @brief Destroy the PNGFile object | ||
* | ||
*/ | ||
~PNGFile(); | ||
|
||
/** | ||
* @brief Get the Width of the file | ||
* | ||
* @return unsigned int the width | ||
*/ | ||
unsigned int getWidth() const; | ||
/** | ||
* @brief Get the Height of the file | ||
* | ||
* @return unsigned int the height | ||
*/ | ||
unsigned int getHeight() const; | ||
/** | ||
* @brief Get the Base64 representation of the file | ||
* | ||
* @return const std::string& the base64 representation | ||
*/ | ||
const std::string &getBase64String() const; | ||
}; | ||
|
||
#endif // MINESERVER_FILE_H |