| 835 | } |
| 836 | |
| 837 | void GrWorkspace::LoadAvatar() { |
| 838 | context_->PostTask([=, this]() { |
| 839 | auto avatar_path = user_mgr_->GetAvatarPath(); |
| 840 | if (avatar_path.starts_with("./")) { |
| 841 | avatar_path = avatar_path.substr(1); |
| 842 | } |
| 843 | auto avatar_url_path = std::format("https://{}:{}{}?appkey={}", settings_->GetSpvrServerHost(), settings_->GetSpvrServerPort(), avatar_path, grApp->GetAppkey()); |
| 844 | auto target_avatar_path = settings_->GetGrDataCachePath() + "/" + user_mgr_->GetUserId() + + "." + FileUtil::GetFileSuffix(avatar_path); |
| 845 | LOGI("Cached avatar path: {}", target_avatar_path); |
| 846 | if (File::Exists(target_avatar_path)) { |
| 847 | LOGI("Load local avatar first"); |
| 848 | context_->PostUITask([=, this]() { |
| 849 | this->SetAvatar(target_avatar_path); |
| 850 | }); |
| 851 | } |
| 852 | |
| 853 | auto target_avatar_cache_path = settings_->GetGrDataCachePath() + "/" + user_mgr_->GetUserId() + + "_cache." + FileUtil::GetFileSuffix(avatar_path); |
| 854 | auto file = File::OpenForWriteB(target_avatar_cache_path); |
| 855 | auto r = HttpClient::Download(avatar_url_path, [=, this](const std::string& d) { |
| 856 | file->Append(d); |
| 857 | }); |
| 858 | if (r.status == 200) { |
| 859 | LOGI("Load avatar from server and refresh it!"); |
| 860 | file->Close(); |
| 861 | File::Delete(target_avatar_path); |
| 862 | FileUtil::ReName(target_avatar_cache_path, target_avatar_path); |
| 863 | File::Delete(target_avatar_cache_path); |
| 864 | context_->PostUITask([=, this]() { |
| 865 | this->SetAvatar(target_avatar_path); |
| 866 | }); |
| 867 | } |
| 868 | else { |
| 869 | file->Close(); |
| 870 | } |
| 871 | }); |
| 872 | } |
| 873 | |
| 874 | void GrWorkspace::SetAvatar(const std::string& filepath) { |
| 875 | auto pixmap = QPixmap::fromImage(QImage(filepath.c_str())); |
no test coverage detected