diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-06-01 09:38:17 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-06-01 09:38:17 -0500 |
| commit | f6aad956a83155e8e89add7e24804d6b99ec4119 (patch) | |
| tree | 1d6d4d530a7bce1c7fa20650f0275eb41d70886c /src | |
| parent | 3019ed0a76fac1fdedaccc82aca7546f729d42ef (diff) | |
| download | slskd-f6aad956a83155e8e89add7e24804d6b99ec4119.tar.gz | |
light refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 44 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Search/SearchService.cs | 15 | ||||
| -rw-r--r-- | src/slskd/Shares/IShareService.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 5 | ||||
| -rw-r--r-- | src/slskd/State.cs | 1 |
6 files changed, 43 insertions, 28 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 110b1d4f..880e8f74 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -441,7 +441,7 @@ namespace slskd // send whatever counts we have currently. we'll probably connect before the cache is primed, so these will be zero // initially, but we'll update them when the cache is filled. - _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Cache.Directories, State.CurrentValue.Shares.Cache.Files); + _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); } private void Client_PrivateMessageRecieved(object sender, PrivateMessageReceivedEventArgs args) @@ -509,9 +509,7 @@ namespace slskd private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) { - // this is really verbose. Console.WriteLine($"[{args.Transfer.Direction.ToString().ToUpper()}] - // [{args.Transfer.Username}/{Path.GetFileName(args.Transfer.Filename)}] - // {args.Transfer.BytesTransferred}/{args.Transfer.Size} {args.Transfer.PercentComplete}% {args.Transfer.AverageSpeed}kb/s"); + // no-op. this is really verbose, use for troubleshooting. } private void Client_TransferStateChanged(object sender, TransferStateChangedEventArgs args) @@ -555,7 +553,7 @@ namespace slskd /// <returns>A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory.</returns> private async Task<Soulseek.Directory> DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory) { - var dir = await Shares.ListAsync(directory); + var dir = await Shares.ListDirectoryAsync(directory); return dir; } @@ -577,15 +575,25 @@ namespace slskd private async Task EnqueueDownloadAction(string username, IPEndPoint endpoint, string filename, ITransferTracker tracker) { _ = endpoint; - var localFilename = (await Shares.ResolveAsync(filename)).ToLocalOSPath(); - - var fileInfo = new FileInfo(localFilename); + string localFilename; + FileInfo fileInfo = default; Console.WriteLine($"[UPLOAD REQUESTED] [{username}/{filename}]"); - if (!fileInfo.Exists) + try + { + localFilename = (await Shares.ResolveFilenameAsync(filename)).ToLocalOSPath(); + + fileInfo = new FileInfo(localFilename); + + if (!fileInfo.Exists) + { + throw new NotFoundException(); + } + } + catch (NotFoundException) { - Console.WriteLine($"[UPLOAD REJECTED] File {localFilename} not found."); + Console.WriteLine($"[UPLOAD REJECTED] File {filename} not found."); throw new DownloadEnqueueException($"File not shared."); } @@ -840,7 +848,7 @@ namespace slskd { var (previous, current) = state; - if (!previous.Cache.Filling && current.Cache.Filling) + if (!previous.Scanning && current.Scanning) { SharesRefreshStarted = DateTime.UtcNow; @@ -848,33 +856,33 @@ namespace slskd Log.Information("Scanning shares"); } - var lastProgress = Math.Round(previous.Cache.FillProgress * 100); - var currentProgress = Math.Round(current.Cache.FillProgress * 100); + var lastProgress = Math.Round(previous.ScanProgress * 100); + var currentProgress = Math.Round(current.ScanProgress * 100); if (lastProgress != currentProgress && Math.Round(currentProgress, 0) % 10 == 0) { State.SetValue(s => s with { Shares = current }); - Log.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, current.Cache.Files); + Log.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, current.Files); } - if (previous.Cache.Filling && !current.Cache.Filling) + if (previous.Scanning && !current.Scanning) { State.SetValue(s => s with { Shares = current }); - if (current.Cache.Faulted) + if (current.Faulted) { Log.Error("Failed to scan shares."); } else { State.SetValue(s => s with { PendingShareRescan = false }); - Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Cache.Directories, current.Cache.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); + Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Directories, current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); SharesRefreshStarted = default; if (Client.State.HasFlag(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn)) { - _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Cache.Directories, State.CurrentValue.Shares.Cache.Files); + _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); } } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 1a5d2a28..df1fb55c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -682,7 +682,7 @@ namespace slskd } // if we made it this far, the caller is either looking for a route that was synthesized with a SPA router, or is genuinely confused. - // if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next + // if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next // middleware app.Use(async (context, next) => { diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 949a7088..f077e76b 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -162,18 +162,23 @@ namespace slskd.Search /// </summary> /// <param name="search">The search to delete.</param> /// <returns>The operation context.</returns> - public async Task DeleteAsync(Search search) + public Task DeleteAsync(Search search) { if (search == default) { throw new ArgumentNullException(nameof(search)); } - using var context = ContextFactory.CreateDbContext(); - context.Searches.Remove(search); - await context.SaveChangesAsync(); + return DoDeleteAsync(search); - await SearchHub.BroadcastDeleteAsync(search); + async Task DoDeleteAsync(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Searches.Remove(search); + await context.SaveChangesAsync(); + + await SearchHub.BroadcastDeleteAsync(search); + } } /// <summary> diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index cc91ba07..16f1a890 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -47,7 +47,7 @@ namespace slskd.Shares /// </summary> /// <param name="directory">The directory for which the contents are to be listed.</param> /// <returns>The contents of the directory.</returns> - Task<Directory> ListAsync(string directory); + Task<Directory> ListDirectoryAsync(string directory); /// <summary> /// Resolves the local filename of the specified <paramref name="remoteFilename"/>, if the mask is associated with a @@ -58,7 +58,7 @@ namespace slskd.Shares /// <exception cref="NotFoundException"> /// Thrown when the specified remote filename can not be associated with a configured share. /// </exception> - Task<string> ResolveAsync(string remoteFilename); + Task<string> ResolveFilenameAsync(string remoteFilename); /// <summary> /// Searches the cache for the specified <paramref name="query"/> and returns the matching files. diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index d99c5f6a..7c08ad13 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -52,6 +52,7 @@ namespace slskd.Shares // scan is pending if faulted, or if state DIDN'T just transition from filling to not filling AND a scan was already pending ScanPending = current.Faulted || (!(previous.Filling && !current.Filling) && state.ScanPending), Scanning = current.Filling, + Faulted = current.Faulted, ScanProgress = current.FillProgress, Directories = current.Directories, Files = current.Files, @@ -88,7 +89,7 @@ namespace slskd.Shares /// </summary> /// <param name="directory">The directory for which the contents are to be listed.</param> /// <returns>The contents of the directory.</returns> - public Task<Directory> ListAsync(string directory) + public Task<Directory> ListDirectoryAsync(string directory) => Task.FromResult(Cache.List(directory)); /// <summary> @@ -100,7 +101,7 @@ namespace slskd.Shares /// <exception cref="NotFoundException"> /// Thrown when the specified remote filename can not be associated with a configured share. /// </exception> - public Task<string> ResolveAsync(string remoteFilename) + public Task<string> ResolveFilenameAsync(string remoteFilename) { var resolvedFilename = Cache.Resolve(remoteFilename); diff --git a/src/slskd/State.cs b/src/slskd/State.cs index 863c59da..407aeb9e 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -82,6 +82,7 @@ namespace slskd { public bool ScanPending { get; init; } public bool Scanning { get; init; } + public bool Faulted { get; init; } public double ScanProgress { get; init; } public int Directories { get; init; } public int Files { get; init; } |
