Skip to content

Commit

Permalink
TgBot++: socket: Fix CMD_DOWNLOAD_FILE handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Jun 4, 2024
1 parent f6f2381 commit ce92307
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/socket/TgBotSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ int main(int argc, char** argv) {
data::DownloadFile data{};
copyToStrBuf(data.filepath, argv[0]);
copyToStrBuf(data.destfilename, argv[1]);
pkt = Packet(cmd, &data, sizeof(data));
pkt = Packet(cmd, data);
break;
}
default:
Expand Down
18 changes: 9 additions & 9 deletions src/socket/interface/impl/bot/TgBotSocketFileHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
#include <string>
#include <system_error>

#include "SocketBase.hpp"
#include "TgBotSocket_Export.hpp"
#include <TgBotSocket_Export.hpp>

template <size_t size>
inline void copyTo(std::array<char, size>& arr_in, const char* buf) {
strncpy(arr_in.data(), buf, size);
}

namespace FileDataHelper {
using len_t = SocketInterfaceBase::buffer_len_t;
using len_t = uint64_t;
using RAIIMalloc = std::unique_ptr<void, decltype(&free)>;

enum Pass {
UPLOAD_FILE_DRY,
UPLOAD_FILE,
DOWNLOAD_FILE,
};

struct DataFromFileParam {
std::filesystem::path filepath;
std::filesystem::path destfilepath;
Expand Down Expand Up @@ -60,22 +60,22 @@ bool DataToFile<DOWNLOAD_FILE>(const void* ptr, len_t len);

template <Pass p>
static std::optional<TgBotSocket::Packet> DataFromFile(
const DataFromFileParam&) = delete;
const DataFromFileParam& params) = delete;

// Client ->
template <>
std::optional<TgBotSocket::Packet> DataFromFile<UPLOAD_FILE>(
const DataFromFileParam&);
const DataFromFileParam& params);

// Client ->
template <>
std::optional<TgBotSocket::Packet> DataFromFile<UPLOAD_FILE_DRY>(
const DataFromFileParam&);
const DataFromFileParam& params);

// Server ->
template <>
std::optional<TgBotSocket::Packet> DataFromFile<DOWNLOAD_FILE>(
const DataFromFileParam&);
const DataFromFileParam& params);
}; // namespace FileDataHelper

// Implementation starts
Expand Down Expand Up @@ -211,7 +211,7 @@ bool FileDataHelper::DataToFile<FileDataHelper::DOWNLOAD_FILE>(const void* ptr,
len_t len) {
const auto* data = static_cast<const DownloadFile*>(ptr);

return writeFileCommon(data->filepath.data(), &data->buf[0],
return writeFileCommon(data->destfilename.data(), &data->buf[0],
len - sizeof(DownloadFile));
}

Expand Down Expand Up @@ -294,7 +294,7 @@ FileDataHelper::DataFromFile<FileDataHelper::DOWNLOAD_FILE>(
}
// Create result packet buffer
auto resultPointer = createMemory(result->size + sizeof(DownloadFile));
// The front bytes of the buffer is UploadFile, hence cast it
// The front bytes of the buffer is DownloadFile, hence cast it
auto* downloadFile = static_cast<DownloadFile*>(resultPointer.get());
// Copy destination file name info to the buffer
copyTo(downloadFile->destfilename, params.destfilepath.string().c_str());
Expand Down

0 comments on commit ce92307

Please sign in to comment.