#include "mologram/ui/media/MediaSender.hpp"
#include <filesystem>
#include "mologram/ui/media/MediaFormat.hpp"
namespace mologram::ui::media {
namespace fs = std::filesystem;
std::string MediaSender::send(const Peer& peer, const std::string& path, const Options& opt, Done done) {
std::string name = fs::path(path).filename().string();
std::string uid = store_.trackUpload("upload:" + name, "Sending " + name); // the daemon reports progress under "upload:<name>"
js::Object p;
p["peer"] = peer.toJson();
p["path"] = js::Value(path);
if (!opt.caption.empty()) p["caption"] = js::Value(opt.caption);
p["kind"] = js::Value(sendKindForPath(path, opt.asDocument));
RequestId req = client_.request("send " + name, "messages.send_file", std::move(p),
[this, uid, done](const RpcResult&) { store_.finishUpload(uid, true, ""); if (done) done(true, ""); },
[this, uid, done](const RpcError& e) { store_.finishUpload(uid, false, e.code == ErrorCode::Cancelled ? "cancelled" : e.text); if (done) done(false, e.text); });
store_.setRequest(uid, req);
return uid;
}
std::string MediaSender::sendAlbum(const Peer& peer, const std::vector<std::string>& paths, const std::string& caption, Done done) {
std::string uid = store_.trackUpload("upload:album", "Sending album of " + std::to_string(paths.size()));
js::Object p;
p["peer"] = peer.toJson();
js::Array a;
for (const auto& path : paths) a.push_back(js::Value(path));
p["paths"] = js::Value(std::move(a));
if (!caption.empty()) p["caption"] = js::Value(caption);
RequestId req = client_.request("send album", "messages.send_album", std::move(p),
[this, uid, done](const RpcResult&) { store_.finishUpload(uid, true, ""); if (done) done(true, ""); },
[this, uid, done](const RpcError& e) { store_.finishUpload(uid, false, e.code == ErrorCode::Cancelled ? "cancelled" : e.text); if (done) done(false, e.text); });
store_.setRequest(uid, req);
return uid;
}
static int hexv(char c) { return c >= '0' && c <= '9' ? c - '0' : (c | 32) >= 'a' && (c | 32) <= 'f' ? (c | 32) - 'a' + 10 : -1; }
std::vector<std::string> MediaSender::pathsFromPaste(const std::string& text) {
std::vector<std::string> tokens;
std::string cur;
bool inTok = false;
char quote = 0;
auto flush = [&]() { if (inTok) tokens.push_back(cur); cur.clear(); inTok = false; };
for (size_t i = 0; i < text.size(); i++) {
char c = text[i];
if (quote) {
if (c == quote) quote = 0; else cur += c;
} else if (c == '\\' && i + 1 < text.size()) { cur += text[++i]; inTok = true; }
else if (c == '\'' || c == '"') { quote = c; inTok = true; }
else if (c == '\n' || c == '\r') flush();
else if (c == ' ' || c == '\t') {
// a bare space inside a path pasted as one line without quotes: keep it when the whole token so far is not an existing file
if (inTok && !cur.empty() && fs::is_regular_file(cur)) flush();
else if (inTok) cur += c;
} else { cur += c; inTok = true; }
}
flush();
std::vector<std::string> out;
for (auto t : tokens) {
if (t.rfind("file://", 0) == 0) {
std::string d;
for (size_t i = 7; i < t.size(); i++) {
if (t[i] == '%' && i + 2 < t.size() && hexv(t[i + 1]) >= 0 && hexv(t[i + 2]) >= 0) { d += (char)(hexv(t[i + 1]) * 16 + hexv(t[i + 2])); i += 2; }
else d += t[i];
}
t = d;
}
while (!t.empty() && (t.back() == ' ' || t.back() == '\t')) t.pop_back();
std::error_code ec;
if (!t.empty() && fs::is_regular_file(t, ec)) out.push_back(t);
}
return out;
}
} // namespace mologram::ui::media