clear cache

This commit is contained in:
Kyle Spearrin 2017-11-16 17:18:25 -05:00
parent c1ae3f1fb2
commit 0dd9ad43e8
3 changed files with 51 additions and 38 deletions

View file

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Bit.App.Models; using Bit.App.Models;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using System; using System;
using Bit.App.Models.Data;
namespace Bit.App.Abstractions namespace Bit.App.Abstractions
{ {
@ -13,9 +14,13 @@ namespace Bit.App.Abstractions
Task<IEnumerable<Cipher>> GetAllAsync(bool favorites); Task<IEnumerable<Cipher>> GetAllAsync(bool favorites);
Task<Tuple<IEnumerable<Cipher>, IEnumerable<Cipher>>> GetAllAsync(string uriString); Task<Tuple<IEnumerable<Cipher>, IEnumerable<Cipher>>> GetAllAsync(string uriString);
Task<ApiResult<CipherResponse>> SaveAsync(Cipher cipher); Task<ApiResult<CipherResponse>> SaveAsync(Cipher cipher);
Task UpsertDataAsync(CipherData cipher);
Task<ApiResult> DeleteAsync(string id); Task<ApiResult> DeleteAsync(string id);
Task DeleteDataAsync(string id);
Task<byte[]> DownloadAndDecryptAttachmentAsync(string url, string orgId = null); Task<byte[]> DownloadAndDecryptAttachmentAsync(string url, string orgId = null);
Task<ApiResult<CipherResponse>> EncryptAndSaveAttachmentAsync(Cipher cipher, byte[] data, string fileName); Task<ApiResult<CipherResponse>> EncryptAndSaveAttachmentAsync(Cipher cipher, byte[] data, string fileName);
Task UpsertAttachmentDataAsync(IEnumerable<AttachmentData> attachments);
Task<ApiResult> DeleteAttachmentAsync(Cipher cipher, string attachmentId); Task<ApiResult> DeleteAttachmentAsync(Cipher cipher, string attachmentId);
Task DeleteAttachmentDataAsync(string attachmentId);
} }
} }

View file

@ -236,17 +236,8 @@ namespace Bit.App.Services
if(response.Succeeded) if(response.Succeeded)
{ {
var data = new CipherData(response.Result, _authService.UserId); var data = new CipherData(response.Result, _authService.UserId);
if(cipher.Id == null) await UpsertDataAsync(data);
{ cipher.Id = data.Id;
await _cipherRepository.InsertAsync(data);
cipher.Id = data.Id;
}
else
{
await _cipherRepository.UpdateAsync(data);
}
_cachedCiphers = null;
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized) || response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
@ -257,13 +248,18 @@ namespace Bit.App.Services
return response; return response;
} }
public async Task UpsertDataAsync(CipherData cipher)
{
await _cipherRepository.UpsertAsync(cipher);
_cachedCiphers = null;
}
public async Task<ApiResult> DeleteAsync(string id) public async Task<ApiResult> DeleteAsync(string id)
{ {
var response = await _cipherApiRepository.DeleteAsync(id); var response = await _cipherApiRepository.DeleteAsync(id);
if(response.Succeeded) if(response.Succeeded)
{ {
await _cipherRepository.DeleteAsync(id); await DeleteDataAsync(id);
_cachedCiphers = null;
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized) || response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
@ -274,6 +270,12 @@ namespace Bit.App.Services
return response; return response;
} }
public async Task DeleteDataAsync(string id)
{
await _cipherRepository.DeleteAsync(id);
_cachedCiphers = null;
}
public async Task<byte[]> DownloadAndDecryptAttachmentAsync(string url, string orgId = null) public async Task<byte[]> DownloadAndDecryptAttachmentAsync(string url, string orgId = null)
{ {
using(var client = new HttpClient()) using(var client = new HttpClient())
@ -318,10 +320,7 @@ namespace Bit.App.Services
if(response.Succeeded) if(response.Succeeded)
{ {
var attachmentData = response.Result.Attachments.Select(a => new AttachmentData(a, cipher.Id)); var attachmentData = response.Result.Attachments.Select(a => new AttachmentData(a, cipher.Id));
foreach(var attachment in attachmentData) await UpsertAttachmentDataAsync(attachmentData);
{
await _attachmentRepository.UpsertAsync(attachment);
}
cipher.Attachments = response.Result.Attachments.Select(a => new Attachment(a)); cipher.Attachments = response.Result.Attachments.Select(a => new Attachment(a));
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
@ -333,12 +332,21 @@ namespace Bit.App.Services
return response; return response;
} }
public async Task UpsertAttachmentDataAsync(IEnumerable<AttachmentData> attachments)
{
foreach(var attachment in attachments)
{
await _attachmentRepository.UpsertAsync(attachment);
}
_cachedCiphers = null;
}
public async Task<ApiResult> DeleteAttachmentAsync(Cipher cipher, string attachmentId) public async Task<ApiResult> DeleteAttachmentAsync(Cipher cipher, string attachmentId)
{ {
var response = await _cipherApiRepository.DeleteAttachmentAsync(cipher.Id, attachmentId); var response = await _cipherApiRepository.DeleteAttachmentAsync(cipher.Id, attachmentId);
if(response.Succeeded) if(response.Succeeded)
{ {
await _attachmentRepository.DeleteAsync(attachmentId); await DeleteAttachmentDataAsync(attachmentId);
} }
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized) || response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
@ -349,6 +357,12 @@ namespace Bit.App.Services
return response; return response;
} }
public async Task DeleteAttachmentDataAsync(string attachmentId)
{
await _attachmentRepository.DeleteAsync(attachmentId);
_cachedCiphers = null;
}
private Tuple<string, string[]> InfoFromMobileAppUri(string mobileAppUriString) private Tuple<string, string[]> InfoFromMobileAppUri(string mobileAppUriString)
{ {
if(UriIsAndroidApp(mobileAppUriString)) if(UriIsAndroidApp(mobileAppUriString))

View file

@ -20,7 +20,7 @@ namespace Bit.App.Services
private readonly ISettingsApiRepository _settingsApiRepository; private readonly ISettingsApiRepository _settingsApiRepository;
private readonly ISyncApiRepository _syncApiRepository; private readonly ISyncApiRepository _syncApiRepository;
private readonly IFolderRepository _folderRepository; private readonly IFolderRepository _folderRepository;
private readonly ICipherRepository _cipherRepository; private readonly ICipherService _cipherService;
private readonly IAttachmentRepository _attachmentRepository; private readonly IAttachmentRepository _attachmentRepository;
private readonly ISettingsRepository _settingsRepository; private readonly ISettingsRepository _settingsRepository;
private readonly IAuthService _authService; private readonly IAuthService _authService;
@ -35,7 +35,7 @@ namespace Bit.App.Services
ISettingsApiRepository settingsApiRepository, ISettingsApiRepository settingsApiRepository,
ISyncApiRepository syncApiRepository, ISyncApiRepository syncApiRepository,
IFolderRepository folderRepository, IFolderRepository folderRepository,
ICipherRepository cipherRepository, ICipherService cipherService,
IAttachmentRepository attachmentRepository, IAttachmentRepository attachmentRepository,
ISettingsRepository settingsRepository, ISettingsRepository settingsRepository,
IAuthService authService, IAuthService authService,
@ -49,7 +49,7 @@ namespace Bit.App.Services
_settingsApiRepository = settingsApiRepository; _settingsApiRepository = settingsApiRepository;
_syncApiRepository = syncApiRepository; _syncApiRepository = syncApiRepository;
_folderRepository = folderRepository; _folderRepository = folderRepository;
_cipherRepository = cipherRepository; _cipherService = cipherService;
_attachmentRepository = attachmentRepository; _attachmentRepository = attachmentRepository;
_settingsRepository = settingsRepository; _settingsRepository = settingsRepository;
_authService = authService; _authService = authService;
@ -78,18 +78,15 @@ namespace Bit.App.Services
try try
{ {
var cipherData = new CipherData(cipher.Result, _authService.UserId); var cipherData = new CipherData(cipher.Result, _authService.UserId);
await _cipherRepository.UpsertAsync(cipherData).ConfigureAwait(false); await _cipherService.UpsertDataAsync(cipherData).ConfigureAwait(false);
var localAttachments = (await _attachmentRepository.GetAllByCipherIdAsync(cipherData.Id) var localAttachments = (await _attachmentRepository.GetAllByCipherIdAsync(cipherData.Id)
.ConfigureAwait(false)); .ConfigureAwait(false));
if(cipher.Result.Attachments != null) if(cipher.Result.Attachments != null)
{ {
foreach(var attachment in cipher.Result.Attachments) var attachmentData = cipher.Result.Attachments.Select(a => new AttachmentData(a, cipherData.Id));
{ await _cipherService.UpsertAttachmentDataAsync(attachmentData).ConfigureAwait(false);
var attachmentData = new AttachmentData(attachment, cipherData.Id);
await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false);
}
} }
if(localAttachments != null) if(localAttachments != null)
@ -99,7 +96,7 @@ namespace Bit.App.Services
{ {
try try
{ {
await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false); await _cipherService.DeleteAttachmentDataAsync(attachment.Id).ConfigureAwait(false);
} }
catch(SQLite.SQLiteException) { } catch(SQLite.SQLiteException) { }
} }
@ -178,7 +175,7 @@ namespace Bit.App.Services
try try
{ {
await _cipherRepository.DeleteAsync(id).ConfigureAwait(false); await _cipherService.DeleteDataAsync(id).ConfigureAwait(false);
SyncCompleted(true); SyncCompleted(true);
return true; return true;
} }
@ -359,7 +356,7 @@ namespace Bit.App.Services
return; return;
} }
var localCiphers = (await _cipherRepository.GetAllByUserIdAsync(_authService.UserId) var localCiphers = (await _cipherService.GetAllAsync()
.ConfigureAwait(false)) .ConfigureAwait(false))
.GroupBy(s => s.Id) .GroupBy(s => s.Id)
.Select(s => s.First()) .Select(s => s.First())
@ -383,15 +380,12 @@ namespace Bit.App.Services
localCiphers[serverCipher.Value.Id] : null; localCiphers[serverCipher.Value.Id] : null;
var data = new CipherData(serverCipher.Value, _authService.UserId); var data = new CipherData(serverCipher.Value, _authService.UserId);
await _cipherRepository.UpsertAsync(data).ConfigureAwait(false); await _cipherService.UpsertDataAsync(data).ConfigureAwait(false);
if(serverCipher.Value.Attachments != null) if(serverCipher.Value.Attachments != null)
{ {
foreach(var attachment in serverCipher.Value.Attachments) var attachmentData = serverCipher.Value.Attachments.Select(a => new AttachmentData(a, data.Id));
{ await _cipherService.UpsertAttachmentDataAsync(attachmentData).ConfigureAwait(false);
var attachmentData = new AttachmentData(attachment, data.Id);
await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false);
}
} }
if(localCipher != null && localAttachments != null && localAttachments.ContainsKey(localCipher.Id)) if(localCipher != null && localAttachments != null && localAttachments.ContainsKey(localCipher.Id))
@ -401,7 +395,7 @@ namespace Bit.App.Services
{ {
try try
{ {
await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false); await _cipherService.DeleteAttachmentDataAsync(attachment.Id).ConfigureAwait(false);
} }
catch(SQLite.SQLiteException) { } catch(SQLite.SQLiteException) { }
} }
@ -414,7 +408,7 @@ namespace Bit.App.Services
{ {
try try
{ {
await _cipherRepository.DeleteAsync(cipher.Value.Id).ConfigureAwait(false); await _cipherService.DeleteDataAsync(cipher.Value.Id).ConfigureAwait(false);
} }
catch(SQLite.SQLiteException) { } catch(SQLite.SQLiteException) { }
} }