//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
using System.IO;
using Microsoft.Extensions.Options;
namespace slskd.Shares
{
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Soulseek;
///
/// Provides control and interactions with configured shares and shared files.
///
public class ShareService : IShareService
{
///
/// Initializes a new instance of the class.
///
///
///
public ShareService(
IOptionsMonitor optionsMonitor,
ISharedFileCache sharedFileCache = null)
{
Cache = sharedFileCache ?? new SharedFileCache(
storageMode: optionsMonitor.CurrentValue.Shares.Cache.StorageMode.ToEnum(),
workerCount: optionsMonitor.CurrentValue.Shares.Cache.Workers);
Cache.StateMonitor.OnChange(cacheState =>
{
var (previous, current) = cacheState;
State.SetValue(state => state with
{
// 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 || current.Cancelled || (!(previous.Filling && !current.Filling) && state.ScanPending),
Scanning = current.Filling,
Faulted = current.Faulted,
Cancelled = current.Cancelled,
Ready = current.Filled,
ScanProgress = current.FillProgress,
Directories = current.Directories,
Files = current.Files,
});
});
Repository = new ShareRepository(connectionString: Program.ConnectionStrings.Shares);
OptionsMonitor = optionsMonitor;
OptionsMonitor.OnChange(options => Configure(options));
Configure(OptionsMonitor.CurrentValue);
}
///
/// Gets the list of configured shares.
///
public IReadOnlyList Shares => SharesList.AsReadOnly();
///
/// Gets the state monitor for the service.
///
public IStateMonitor StateMonitor => State;
private ISharedFileCache Cache { get; }
private IShareRepository Repository { get; }
private string LastOptionsHash { get; set; }
private IOptionsMonitor OptionsMonitor { get; }
private List SharesList { get; set; } = new List();
private IManagedState State { get; } = new ManagedState();
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1);
private List FilterRegexes { get; set; } = new List();
///
/// Returns the entire contents of the share.
///
/// The entire contents of the share.
public Task> BrowseAsync(Share share = null)
{
var directories = new ConcurrentDictionary();
string prefix = null;
if (share != null)
{
prefix = share.RemotePath + Path.DirectorySeparatorChar;
}
// Soulseek requires that each directory in the tree have an entry in the list returned in a browse response. if
// missing, files that are nested within directories which contain only directories (no files) are displayed as being
// in the root. to get around this, prime a dictionary with all known directories, and an empty Soulseek.Directory. if
// there are any files in the directory, this entry will be overwritten with a new Soulseek.Directory containing the
// files. if not they'll be left as is.
foreach (var directory in Repository.ListDirectories(prefix))
{
var name = directory.NormalizePathForSoulseek();
directories.TryAdd(name, new Directory(name));
}
var files = Repository.ListFiles(prefix, includeFullPath: true);
var groups = files
.GroupBy(file => Path.GetDirectoryName(file.Filename))
.Select(group => new Directory(group.Key.NormalizePathForSoulseek(), group.Select(f =>
{
return new File(
f.Code,
Path.GetFileName(f.Filename), // we can send the full path, or just the filename. save bandwidth and omit the path.
f.Size,
f.Extension,
f.Attributes);
}).OrderBy(f => f.Filename)));
// merge the dictionary containing all directories with the Soulseek.Directory instances containing their files.
// entries with no files will remain untouched.
foreach (var group in groups)
{
directories.AddOrUpdate(group.Name, group, (_, _) => group);
}
var results = directories.Values.AsEnumerable();
return Task.FromResult(results);
}
///
/// Returns the contents of the specified .
///
/// The directory for which the contents are to be listed.
/// The contents of the directory.
public Task ListDirectoryAsync(string directory)
{
var list = Cache.List(directory.LocalizePath());
var normalizedList = new Directory(list.Name.NormalizePathForSoulseek(), list.Files);
return Task.FromResult(normalizedList);
}
///
/// Resolves the local filename of the specified , if the mask is associated with a
/// configured share.
///
/// The fully qualified filename to resolve.
/// The resolved local filename.
///
/// Thrown when the specified remote filename can not be associated with a configured share.
///
public Task ResolveFileAsync(string remoteFilename)
{
var resolvedFilename = Cache.Resolve(remoteFilename.LocalizePath());
if (string.IsNullOrEmpty(resolvedFilename))
{
throw new NotFoundException($"The requested filename '{remoteFilename}' could not be resolved to a local file");
}
var fileInfo = new FileInfo(resolvedFilename);
if (!fileInfo.Exists)
{
// the shared file cache has divered from the physical filesystem; the user needs to perform a scan to reconcile.
State.SetValue(state => state with { ScanPending = true });
throw new NotFoundException($"The resolved file '{resolvedFilename}' could not be located on disk. A share scan should be performed.");
}
return Task.FromResult(fileInfo);
}
///
/// Searches the cache for the specified and returns the matching files.
///
/// The query for which to search.
/// The matching files.
public Task> SearchAsync(SearchQuery query)
{
var results = Cache.Search(query);
return Task.FromResult(results.Select(r => new File(
r.Code,
r.Filename.NormalizePathForSoulseek(),
r.Size,
r.Extension,
r.Attributes)));
}
///
/// Scans the configured shares.
///
/// The operation context.
/// Thrown when a scan is already in progress.
public Task ScanAsync()
{
return Cache.FillAsync(Shares, FilterRegexes);
}
///
/// Gets summary information for the specified .
///
/// The share to summarize.
/// The summary information.
public Task<(int Directories, int Files)> SummarizeShareAsync(Share share)
{
var dirs = Cache.CountDirectories(share);
var files = Cache.CountFiles(share);
return Task.FromResult((dirs, files));
}
///
/// Cancels the currently running scan, if one is running.
///
/// A value indicating whether a scan was cancelled.
public bool TryCancelScan()
{
return Cache.TryCancelFill();
}
///
/// Attempt to load shares from disk.
///
/// A value indicating whether shares were loaded.
public Task TryLoadFromDiskAsync()
{
return Task.FromResult(Cache.TryLoad());
}
private void Configure(Options options)
{
SyncRoot.Wait();
try
{
var optionsHash = Compute.Sha1Hash(string.Join(';', options.Shares.Directories));
if (optionsHash == LastOptionsHash)
{
return;
}
var shares = options.Shares.Directories
.Select(share => share.TrimEnd('/', '\\'))
.ToHashSet() // remove duplicates
.Select(share => new Share(share)) // convert to Shares
.OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent
.ToList();
var regexes = options.Shares.Filters
.Select(filter => new Regex(filter, RegexOptions.Compiled))
.ToList();
SharesList = shares;
FilterRegexes = regexes;
State.SetValue(state => state with { ScanPending = true });
LastOptionsHash = optionsHash;
}
finally
{
SyncRoot.Release();
}
}
}
}