#include "mologram/ui/media/MediaStore.hpp"
#include <algorithm>
namespace mologram::ui::media {
const std::string* MediaStore::pathOf(const std::string& token, bool thumb) const {
auto it = paths_.find(key(token, thumb));
return it == paths_.end() ? nullptr : &it->second;
}
Transfer* MediaStore::mut(const std::string& id) {
for (auto& t : transfers_) if (t.id == id) return &t;
return nullptr;
}
const Transfer* MediaStore::find(const std::string& id) const {
for (auto& t : transfers_) if (t.id == id) return &t;
return nullptr;
}
void MediaStore::fetch(const std::string& token, bool thumb, const std::string& label, Done done) {
std::string k = key(token, thumb);
if (auto it = paths_.find(k); it != paths_.end()) { done(true, it->second); return; }
auto& list = waiters_[k];
bool first = list.empty();
list.push_back(std::move(done));
if (!first) return; // already on its way: share it
Transfer t;
t.id = k; t.progressId = token; t.label = label.empty() ? token : label;
transfers_.push_back(t);
js::Object p;
p["media"] = js::Value(token);
if (thumb) p["thumb"] = js::Value(true);
RequestId id = client_.request("download", "media.download", std::move(p),
[this, k](const RpcResult& r) {
std::string path = js::getStr(r.value, "path");
paths_[k] = path;
if (Transfer* t = mut(k)) { t->state = Transfer::State::Done; t->path = path; if (t->total == 0) t->total = t->done = 1; }
auto ws = std::move(waiters_[k]);
waiters_.erase(k);
if (onChange) onChange();
for (auto& w : ws) w(true, path);
},
[this, k](const RpcError& e) {
if (Transfer* t = mut(k)) { t->state = e.code == ErrorCode::Cancelled ? Transfer::State::Cancelled : Transfer::State::Failed; t->error = e.text; }
auto ws = std::move(waiters_[k]);
waiters_.erase(k);
if (onChange) onChange();
for (auto& w : ws) w(false, e.code == ErrorCode::Cancelled ? std::string("cancelled") : e.text);
});
if (Transfer* t = mut(k)) t->request = id;
if (onChange) onChange();
}
void MediaStore::cancel(const std::string& id) {
if (Transfer* t = mut(id)) if (t->state == Transfer::State::Active && t->request != 0) client_.cancel(t->request);
}
std::string MediaStore::trackUpload(const std::string& progressId, const std::string& label) {
Transfer t;
t.id = progressId + "#" + std::to_string(++uploadSeq_);
t.progressId = progressId; t.label = label; t.upload = true;
transfers_.push_back(t);
if (onChange) onChange();
return transfers_.back().id;
}
void MediaStore::setRequest(const std::string& id, RequestId request) {
if (Transfer* t = mut(id)) t->request = request;
}
void MediaStore::finishUpload(const std::string& id, bool ok, const std::string& error) {
if (Transfer* t = mut(id)) {
t->state = ok ? Transfer::State::Done : (error == "cancelled" ? Transfer::State::Cancelled : Transfer::State::Failed);
t->error = error;
if (ok && t->total == 0) t->total = t->done = 1;
}
if (onChange) onChange();
}
void MediaStore::clearFinished() {
transfers_.erase(std::remove_if(transfers_.begin(), transfers_.end(), [](const Transfer& t) { return t.state != Transfer::State::Active; }), transfers_.end());
if (onChange) onChange();
}
void MediaStore::onEvent(const Event& e) {
if (e.name != "transfer.progress") return;
ui_.post([this, e]() { handleProgress(e); });
}
void MediaStore::handleProgress(const Event& e) {
std::string id = js::getStr(e.data, "id");
bool any = false;
for (auto& t : transfers_) { // thumbnails, full files and equally named uploads share a progress id
if (t.state != Transfer::State::Active || t.progressId != id) continue;
t.done = js::getId(e.data, "done");
t.total = js::getId(e.data, "total");
any = true;
}
if (any && onChange) onChange();
}
} // namespace mologram::ui::media