private string ExportCsv(IEnumerable <FolderView> decryptedFolders, IEnumerable <CipherView> decryptedCiphers) { var foldersMap = decryptedFolders.Where(f => f.Id != null).ToDictionary(f => f.Id); var exportCiphers = new List <ExportCipher>(); foreach (var c in decryptedCiphers) { // only export logins and secure notes if (c.Type != CipherType.Login && c.Type != CipherType.SecureNote) { continue; } if (c.OrganizationId != null) { continue; } var cipher = new ExportCipher(); cipher.Folder = c.FolderId != null && foldersMap.ContainsKey(c.FolderId) ? foldersMap[c.FolderId].Name : null; cipher.Favorite = c.Favorite ? "1" : null; BuildCommonCipher(cipher, c); exportCiphers.Add(cipher); } using (var writer = new StringWriter()) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(exportCiphers); csv.Flush(); return(writer.ToString()); } }
public async Task <string> GetExport(string format = "csv") { _decryptedFolders = await _folderService.GetAllDecryptedAsync(); _decryptedCiphers = await _cipherService.GetAllDecryptedAsync(); if (format == "csv") { var foldersMap = _decryptedFolders.Where(f => f.Id != null).ToDictionary(f => f.Id); var exportCiphers = new List <ExportCipher>(); foreach (var c in _decryptedCiphers) { // only export logins and secure notes if (c.Type != CipherType.Login && c.Type != CipherType.SecureNote) { continue; } if (c.OrganizationId != null) { continue; } var cipher = new ExportCipher(); cipher.Folder = c.FolderId != null && foldersMap.ContainsKey(c.FolderId) ? foldersMap[c.FolderId].Name : null; cipher.Favorite = c.Favorite ? "1" : null; BuildCommonCipher(cipher, c); exportCiphers.Add(cipher); } using (var writer = new StringWriter()) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(exportCiphers); csv.Flush(); return(writer.ToString()); } } else { var jsonDoc = new { Folders = _decryptedFolders.Where(f => f.Id != null).Select(f => new FolderWithId(f)), Items = _decryptedCiphers.Where(c => c.OrganizationId == null) .Select(c => new CipherWithId(c) { CollectionIds = null }) }; return(CoreHelpers.SerializeJson(jsonDoc, new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() })); } }
private void BuildCommonCipher(ExportCipher cipher, CipherView c) { cipher.Type = null; cipher.Name = c.Name; cipher.Notes = c.Notes; cipher.Fields = null; // Login props cipher.LoginUris = null; cipher.LoginUsername = null; cipher.LoginPassword = null; cipher.LoginTotp = null; if (c.Fields != null) { foreach (var f in c.Fields) { if (cipher.Fields == null) { cipher.Fields = ""; } else { cipher.Fields += "\n"; } cipher.Fields += (f.Name ?? "") + ": " + f.Value; } } switch (c.Type) { case CipherType.Login: cipher.Type = "login"; cipher.LoginUsername = c.Login.Username; cipher.LoginPassword = c.Login.Password; cipher.LoginTotp = c.Login.Totp; if (c.Login.Uris != null) { foreach (var u in c.Login.Uris) { if (cipher.LoginUris == null) { cipher.LoginUris = ""; } else { cipher.LoginUris += ","; } cipher.LoginUris += u.Uri; } } break; case CipherType.SecureNote: cipher.Type = "note"; break; default: return; } }