diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-10 18:16:23 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-10 18:16:23 -0500 |
| commit | 3c3cfe6f2c3c49c05aa4de1e86a81fcb2c8af590 (patch) | |
| tree | e99298baaf8f2048d569c18ff25b43799b5bce3d /src | |
| parent | 4bb2484d7b5f3bd1d0e49f8393c4065fa1e59339 (diff) | |
| download | slskd-3c3cfe6f2c3c49c05aa4de1e86a81fcb2c8af590.tar.gz | |
implement share upload
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Network/NetworkClient.cs | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index 007ce80f..10e33118 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -25,6 +25,7 @@ namespace slskd.Network using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR.Client; + using Microsoft.Data.Sqlite; using Serilog; using slskd.Cryptography; using slskd.Shares; @@ -98,9 +99,10 @@ namespace slskd.Network } catch (Exception ex) { - Log.Error($"Failed to upload shares to the network controller: {ex.Message}"); - Log.Error("Disconnecting"); + Log.Error(ex, ex.Message); + Log.Error("Disconnecting from the network controller"); await StopAsync(); + throw; } Log.Information("Shares uploaded. Ready to upload files."); @@ -120,16 +122,34 @@ namespace slskd.Network public async Task UploadSharesAsync() { + var temp = Path.Combine(Path.GetTempPath(), Program.AppName, $"share_backup_{Path.GetRandomFileName()}.db"); + + Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Program.AppName)); + + Log.Debug("Backing up shares to {Filename}", temp); + Shares.BackupTo($"Data Source={temp};Pooling=False;"); // quite a leaky abstraction + Log.Debug("Share backup successful"); + Log.Debug("Requesting share upload token..."); var token = await HubConnection.InvokeAsync<Guid>(nameof(NetworkHub.GetShareUploadToken)); Log.Debug("Share upload token {Token}", token); - var temp = Path.Combine(Path.GetTempPath(), $"slskd_share_backup_{Path.GetRandomFileName()}.db"); + FileStream stream = default; - // todo: back up shares database to temp location - // todo: upload shares database backup to controller + try + { + stream = new FileStream(temp, FileMode.Open, FileAccess.Read); + } + catch (Exception ex) + { + // this happens if SQLite hangs on to the connection for whatever reason, seemingly due to pooling + // Pooling=False should be included in the connection string and this shouldn't be an issue, but + // since i don't totally understand the behavior, i'll leave this for now. + Log.Debug(ex, "Failed open stream to backup. Clearing SQLite pools and trying again."); + SqliteConnection.ClearAllPools(); + stream = new FileStream(temp, FileMode.Open, FileAccess.Read); + } - using var stream = new FileStream(temp, FileMode.Open, FileAccess.Read); using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/agents/shares/{token}"); using var content = new MultipartFormDataContent { @@ -191,7 +211,8 @@ namespace slskd.Network Log.Error(ex, "Failed to handle file request: {Message}", ex.Message); // report the failure to the controller. this avoids a failure due to timeout. - _ = HttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, $"api/v0/agents/files/{id}")); + using var request = new HttpRequestMessage(HttpMethod.Delete, $"api/v0/agents/files/{id}"); + _ = HttpClient.SendAsync(request); } } |
