MCPcopy Create free account
hub / github.com/comaps/comaps / ExportMultipleFiles

Function ExportMultipleFiles

libs/map/bookmark_manager.cpp:151–196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

149}
150
151BookmarkManager::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
198BookmarkManager::SharingResult GetFileForSharing(BookmarkManager::KMLDataCollectionPtr collection,
199 KmlFileType kmlFileType)

Callers 1

GetFileForSharingFunction · 0.85

Calls 15

to_stringFunction · 0.85
GenerateYYMMDDFunction · 0.85
JoinPathFunction · 0.85
DeleteFileXFunction · 0.85
FilenameWithoutExtFunction · 0.85
GetFileNameForExportFunction · 0.85
IsASCIIStringFunction · 0.85
SaveKmlFileSafeFunction · 0.85
BuildIndexFileFunction · 0.85
CreateZipFromFilesFunction · 0.85
TmpDirMethod · 0.80
push_backMethod · 0.45

Tested by

no test coverage detected