add komens attachments downloading functionality
Some checks are pending
/ sync-to-origin (push) Waiting to run

This commit is contained in:
PoliEcho 2025-04-13 12:48:12 +02:00
parent 1992b0e166
commit a78f4536b2
4 changed files with 182 additions and 48 deletions

View File

@ -25,7 +25,7 @@ using nlohmann::json;
std::vector<allocation> komens_allocated; std::vector<allocation> komens_allocated;
void insert_content(WINDOW *content_win, WINDOW *attachment_win, size_t i, void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &resp_from_api); json &resp_from_api);
void komens_page(koment_type type) { void komens_page(koment_type type) {
@ -145,8 +145,8 @@ void komens_page(koment_type type) {
WINDOW *attachment_win = newwin(1, 1, LINES, COLS); WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
insert_content(content_win, attachment_win, insert_content(content_win, attachment_win,
item_index(current_item(komens_choise_menu.menu)), resp_from_api["Messages"][item_index(
resp_from_api); current_item(komens_choise_menu.menu))]);
attron(COLOR_PAIR(COLOR_BLUE)); attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(LINES - 2, 0, mvprintw(LINES - 2, 0,
@ -169,22 +169,61 @@ void komens_page(koment_type type) {
case 'k': case 'k':
menu_driver(komens_choise_menu.menu, REQ_UP_ITEM); menu_driver(komens_choise_menu.menu, REQ_UP_ITEM);
break; break;
default:
if (c >= '1' && c <= '9') {
size_t index = c - '0' - 1;
if (index < resp_from_api["Messages"][item_index(
current_item(komens_choise_menu.menu))]["Attachments"]
.size()) {
std::string default_path =
"~/Downloads/" +
resp_from_api["Messages"][item_index(current_item(
komens_choise_menu.menu))]["Attachments"][index]["Name"]
.get<std::string>();
char path[256];
// Create input prompt at bottom of screen
move(LINES - 1, 0);
clrtoeol();
printw("Save path [%s]: ", default_path.c_str());
echo();
curs_set(1);
getnstr(path, sizeof(path) - 1);
if (strlen(path) == 0)
strcpy(path, default_path.c_str());
noecho();
curs_set(0);
move(LINES - 1, 0);
clrtoeol();
refresh();
// Download the attachment
bakaapi::download_attachment(
resp_from_api["Messages"][item_index(current_item(
komens_choise_menu.menu))]["Attachments"][index]["Id"]
.get<std::string>(),
path);
}
}
break;
} }
insert_content(content_win, attachment_win, insert_content(content_win, attachment_win,
item_index(current_item(komens_choise_menu.menu)), resp_from_api["Messages"][item_index(
resp_from_api); current_item(komens_choise_menu.menu))]);
wrefresh(komens_choise_menu.win); wrefresh(komens_choise_menu.win);
} }
delete_all(&komens_allocated); delete_all(&komens_allocated);
} }
void insert_content(WINDOW *content_win, WINDOW *attachment_win, size_t i, void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &resp_from_api) { json &message) {
wclear(content_win); wclear(content_win);
mvwprintw(content_win, 0, 0, "%s", mvwprintw(content_win, 0, 0, "%s",
html_to_string(resp_from_api.at("Messages")[i]["Text"]).c_str()); html_to_string(message.at("Text")).c_str());
wrefresh(content_win); wrefresh(content_win);
if (!resp_from_api.at("Messages")[i]["Attachments"].empty()) { if (!message.at("Attachments").empty()) {
size_t max_item_lenght = 0; size_t max_item_lenght = 0;
{ {
@ -192,19 +231,15 @@ void insert_content(WINDOW *content_win, WINDOW *attachment_win, size_t i,
size_t max_size_lenght = 0; size_t max_size_lenght = 0;
size_t tmp_lenght; size_t tmp_lenght;
for (size_t j = 0; for (size_t j = 0; j < message.at("Attachments").size(); j++) {
j < resp_from_api.at("Messages")[i]["Attachments"].size(); j++) { tmp_lenght =
tmp_lenght = resp_from_api.at("Messages")[i]["Attachments"][j]["Name"] message.at("Attachments")[j]["Name"].get<std::string>().length();
.get<std::string>()
.length();
if (tmp_lenght > max_name_lenght) { if (tmp_lenght > max_name_lenght) {
max_name_lenght = tmp_lenght; max_name_lenght = tmp_lenght;
} }
tmp_lenght = tmp_lenght =
std::to_string( std::to_string(message.at("Attachments")[j]["Size"].get<size_t>())
resp_from_api.at("Messages")[i]["Attachments"][j]["Size"]
.get<size_t>())
.length(); .length();
if (tmp_lenght > max_size_lenght) { if (tmp_lenght > max_size_lenght) {
max_size_lenght = tmp_lenght; max_size_lenght = tmp_lenght;
@ -221,18 +256,14 @@ void insert_content(WINDOW *content_win, WINDOW *attachment_win, size_t i,
wborder(attachment_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE); wborder(attachment_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE);
print_in_middle(attachment_win, 0, 0, max_item_lenght + 2, "Attachments", print_in_middle(attachment_win, 0, 0, max_item_lenght + 2, "Attachments",
COLOR_RED); COLOR_PAIR(COLOR_RED));
for (size_t j = 0; for (size_t j = 0; j < message.at("Attachments").size(); j++) {
j < resp_from_api.at("Messages")[i]["Attachments"].size(); j++) {
mvwprintw(attachment_win, j + 1, 2, "%s %s", mvwprintw(
resp_from_api.at("Messages")[i]["Attachments"][j]["Name"] attachment_win, j + 1, 2, "%s %s",
.get<std::string>() message.at("Attachments")[j]["Name"].get<std::string>().c_str(),
.c_str(), std::to_string(message.at("Attachments")[j]["Size"].get<size_t>())
std::to_string( .c_str());
resp_from_api.at("Messages")[i]["Attachments"][j]["Size"]
.get<size_t>())
.c_str());
wattron(attachment_win, COLOR_PAIR(COLOR_MAGENTA)); wattron(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1); mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1);

View File

@ -1,8 +1,10 @@
#include "main.h" #include "main.h"
#include "color.h" #include "color.h"
#include "flags.h"
#include "helper_funcs.h" #include "helper_funcs.h"
#include "main_menu.h" #include "main_menu.h"
#include "net.h" #include "net.h"
#include "types.h"
#include <csignal> #include <csignal>
#include <cstdlib> #include <cstdlib>
#include <curl/curl.h> #include <curl/curl.h>
@ -12,8 +14,6 @@
#include <regex> #include <regex>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include "flags.h"
#include "types.h"
std::string baka_api_url; std::string baka_api_url;
@ -36,14 +36,27 @@ int main(int argc, char **argv) {
int opt; int opt;
while ((opt = getopt(argc, argv, "hVvLS:")) != -1) { while ((opt = getopt(argc, argv, "hVvLS:")) != -1) {
switch (opt) { switch (opt) {
case 'h': PrintHelp(); break; case 'h':
case 'V': PrintVersion(); break; PrintHelp();
case 'v': config.verbose = true; break; break;
case 'L': DeleteLogin(savedir_path); break; case 'V':
case 'S': config.ignoressl = true; break; PrintVersion();
default: std::cerr << RED"[ERROR]" << RESET" invalid option: " << (char)optopt << "\ntry: -h\n"; safe_exit(EINVAL); break;
} case 'v':
config.verbose = true;
break;
case 'L':
DeleteLogin(savedir_path);
break;
case 'S':
config.ignoressl = true;
break;
default:
std::cerr << RED "[ERROR]" << RESET " invalid option: " << (char)optopt
<< "\ntry: -h\n";
safe_exit(EINVAL);
}
} }
std::string urlfile_path = std::string(savedir_path) + "/url"; std::string urlfile_path = std::string(savedir_path) + "/url";

View File

@ -11,12 +11,17 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <fcntl.h>
#include <format> #include <format>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
#include <stdlib.h>
#include <string> #include <string>
#include <unistd.h>
#define ROUND_UP(x, y) ((((x) + (y) - 1)) & ~((y) - 1))
using nlohmann::json; using nlohmann::json;
@ -25,15 +30,44 @@ std::string access_token;
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
// Callback function to write data into a std::string // Callback function to write data into a std::string
size_t WriteCallback(void *contents, size_t size, size_t nmemb, size_t WriteCallback_to_string(void *contents, size_t size, size_t nmemb,
std::string *userp) { void *userp) {
size_t totalSize = size * nmemb; size_t totalSize = size * nmemb;
userp->append((char *)contents, totalSize); static_cast<std::string *>(userp)->append((char *)contents, totalSize);
return totalSize; return totalSize;
} }
std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type, size_t WriteCallback_to_file(void *contents, size_t size, size_t nmemb,
std::string req_data) { void *userp) {
const size_t sector_size = 4096;
size_t total = size * nmemb;
void *aligned_buf = aligned_alloc(sector_size, ROUND_UP(total, sector_size));
// Zero out the buffer before copying
memset(aligned_buf, 0, ROUND_UP(total, sector_size));
memcpy(aligned_buf, contents, total);
// Cast userp to fstream*
std::fstream *fileStream = static_cast<std::fstream *>(userp);
// Write to the file using fstream
fileStream->write(static_cast<char *>(aligned_buf), total);
// Check if write was successful
if (fileStream->fail()) {
free(aligned_buf);
return 0;
}
free(aligned_buf);
return total;
}
std::tuple<std::string, int> send_curl_request(
std::string endpoint, metod type, std::string req_data,
size_t (*WriteCallback_function)(void *, size_t, size_t,
void *) = WriteCallback_to_string,
std::fstream *fileStream = nullptr) {
std::string response; std::string response;
std::string url = baka_api_url + endpoint; std::string url = baka_api_url + endpoint;
if (type == GET) { if (type == GET) {
@ -42,15 +76,19 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type,
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback_function);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); if (fileStream) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fileStream);
} else {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
}
if (config.ignoressl) { if (config.ignoressl) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
} }
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate");
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
headers = curl_slist_append( headers = curl_slist_append(
@ -77,7 +115,15 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint, metod type,
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n"; std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20); safe_exit(20);
} }
curl_easy_perform(curl); // Perform the request CURLcode curl_return_code = curl_easy_perform(curl);
if (curl_return_code != CURLE_OK) {
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
<< curl_easy_strerror(curl_return_code) << "\n";
safe_exit(21);
}
if (fileStream) {
fileStream->close();
}
int http_code = 0; int http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
@ -172,4 +218,47 @@ access_token_refreshed:
return json::parse(response); return json::parse(response);
} }
int download_attachment(std::string id, std::string path) {
if (config.verbose) {
std::clog << "downloading attachment please wait...\n";
}
if (path[0] == '~') {
path = std::string(std::getenv("HOME")) + path.substr(1);
}
std::string savedir_path = std::filesystem::path(path).parent_path().string();
if (!std::filesystem::exists(savedir_path)) {
if (!std::filesystem::create_directories(savedir_path)) {
std::cerr << RED "[ERROR] " RESET
<< "Failed to create directory: " << savedir_path << "\n";
safe_exit(EXIT_FAILURE);
}
}
std::fstream fileStream(path, std::ios::out | std::ios::binary);
if (!fileStream.is_open()) {
std::cerr << RED "[ERROR] " RESET << "Failed to open file for writing\n";
safe_exit(EXIT_FAILURE);
}
is_access_token_empty();
access_token_refreshed:
std::string req_data =
std::format("Authorization=Bearer&access_token={}", access_token);
auto [response, http_code] =
send_curl_request(std::format("/api/3/komens/attachment/{}", id), GET,
req_data, WriteCallback_to_file, &fileStream);
if (http_code != 200) {
if (config.verbose) {
std::clog << BLUE "[LOG] " RESET << "download failed " << http_code
<< " is non 200 response\n";
}
refresh_access_token();
goto access_token_refreshed;
}
return 0;
}
} // namespace bakaapi } // namespace bakaapi

View File

@ -19,5 +19,6 @@ void login(std::string username, std::string password);
void refresh_access_token(); void refresh_access_token();
json get_data_from_endpoint(const std::string &endpoint, metod metod, json get_data_from_endpoint(const std::string &endpoint, metod metod,
std::string additional_data = ""); std::string additional_data = "");
int download_attachment(std::string id, std::string path);
} // namespace bakaapi } // namespace bakaapi
#endif #endif