| 149 | } |
| 150 | |
| 151 | BookmarkManager::SharingResult ExportMultipleFiles(BookmarkManager::KMLDataCollectionPtr collection) |
| 152 | { |
| 153 | auto const kmzFileName = "CoMaps_backup_" + std::to_string(base::GenerateYYMMDD(time(nullptr))); |
| 154 | auto kmzFilePath = base::JoinPath(GetPlatform().TmpDir(), kmzFileName + std::string{kKmzExtension}); |
| 155 | auto const filesDir = "files/"; |
| 156 | kml::GroupIdCollection categoriesIds; |
| 157 | for (auto const & kmlToExport : *collection) |
| 158 | categoriesIds.push_back(kmlToExport.second->m_categoryData.m_id); |
| 159 | std::vector<std::string> filesInArchive; |
| 160 | std::vector<std::string> pathsForArchive; |
| 161 | |
| 162 | SCOPE_GUARD(deleteFileGuard, [&pathsForArchive]() |
| 163 | { |
| 164 | for (auto const & path : pathsForArchive) |
| 165 | base::DeleteFileX(path); |
| 166 | }); |
| 167 | |
| 168 | int suffix = 1; |
| 169 | for (auto const & kmlToExport : *collection) |
| 170 | { |
| 171 | std::string fileName = base::FilenameWithoutExt(GetFileNameForExport(kmlToExport)); |
| 172 | if (!strings::IsASCIIString(fileName)) |
| 173 | fileName = "CoMaps_" + std::to_string(suffix++); |
| 174 | auto const kmlPath = base::JoinPath(GetPlatform().TmpDir(), fileName + std::string{kKmlExtension}); |
| 175 | auto const filePathInArchive = filesDir + fileName + std::string{kKmlExtension}; |
| 176 | if (!SaveKmlFileSafe(*kmlToExport.second, kmlPath, KmlFileType::Text)) |
| 177 | continue; |
| 178 | pathsForArchive.push_back(kmlPath); |
| 179 | filesInArchive.push_back(filePathInArchive); |
| 180 | } |
| 181 | std::string indexFilePath; |
| 182 | try |
| 183 | { |
| 184 | indexFilePath = BuildIndexFile(filesInArchive); |
| 185 | } |
| 186 | catch (Writer::WriteException const & e) |
| 187 | { |
| 188 | LOG(LERROR, ("Error creating index file ", indexFilePath, " error ", e.Msg(), " cause ", e.what())); |
| 189 | return {std::move(categoriesIds), BookmarkManager::SharingResult::Code::ArchiveError, "Could not create archive."}; |
| 190 | } |
| 191 | filesInArchive.insert(filesInArchive.begin(), "doc.kml"); |
| 192 | pathsForArchive.insert(pathsForArchive.begin(), indexFilePath); |
| 193 | if (!CreateZipFromFiles(pathsForArchive, filesInArchive, kmzFilePath)) |
| 194 | return {std::move(categoriesIds), BookmarkManager::SharingResult::Code::ArchiveError, "Could not create archive."}; |
| 195 | return {std::move(categoriesIds), std::move(kmzFilePath), kKMZMimeType}; |
| 196 | } |
| 197 | |
| 198 | BookmarkManager::SharingResult GetFileForSharing(BookmarkManager::KMLDataCollectionPtr collection, |
| 199 | KmlFileType kmlFileType) |
no test coverage detected