From ef73185e540d66a650164909993e84c6e3396aa5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Mar 2022 21:06:15 -0500 Subject: move extension method to SoulseekFileFactory --- src/slskd/Shares/Extensions.cs | 71 ------------------------------ src/slskd/Shares/SharedFileCache.cs | 10 +++-- src/slskd/Shares/SoulseekFileFactory.cs | 76 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 74 deletions(-) delete mode 100644 src/slskd/Shares/Extensions.cs create mode 100644 src/slskd/Shares/SoulseekFileFactory.cs (limited to 'src') diff --git a/src/slskd/Shares/Extensions.cs b/src/slskd/Shares/Extensions.cs deleted file mode 100644 index 01e7e000..00000000 --- a/src/slskd/Shares/Extensions.cs +++ /dev/null @@ -1,71 +0,0 @@ -// -// 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; - -namespace slskd.Shares -{ - using System.Collections.Generic; - using System.Linq; - using Soulseek; - - public static class Extensions - { - private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; - private static readonly string[] AudioExtensions = { "aa", "aax", "aac", "aiff", "ape", "dsf", "flac", "m4a", "m4b", "m4p", "mp3", "mpc", "mpp", "ogg", "oga", "wav", "wma", "wv", "webm" }; - private static readonly HashSet SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet(); - - public static File ToSoulseekFile(this string filename, Share share) - { - var code = 1; - var maskedFilename = filename.ReplaceFirst(share.LocalPath, share.RemotePath); - var size = new FileInfo(filename).Length; - var extension = Path.GetExtension(filename)[1..].ToLowerInvariant(); - List attributeList = default; - - if (SupportedExtensions.Contains(extension)) - { - attributeList = new List(); - TagLib.File file = default; - - try - { - file = TagLib.File.Create(filename, TagLib.ReadStyle.Average); - - attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); - attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); - - if (file.Properties.BitsPerSample > 0) - { - attributeList.Add(new FileAttribute(FileAttributeType.SampleRate, file.Properties.AudioSampleRate)); - attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); - } - } - catch - { - // unsupported or corrupt file, noop - } - finally - { - file?.Dispose(); - } - } - - return new File(code, maskedFilename, size, extension, attributeList); - } - } -} diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index f13eaad4..d68cbc3b 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -86,10 +86,13 @@ namespace slskd.Shares /// Initializes a new instance of the class. /// /// + /// public SharedFileCache( - IOptionsMonitor optionsMonitor) + IOptionsMonitor optionsMonitor, + ISoulseekFileFactory soulseekFileFactory = null) { OptionsMonitor = optionsMonitor; + SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); } /// @@ -105,6 +108,7 @@ namespace slskd.Shares private List Shares { get; set; } private SqliteConnection SQLite { get; set; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); + private ISoulseekFileFactory SoulseekFileFactory { get; } /// /// Returns the contents of the cache. @@ -249,8 +253,8 @@ namespace slskd.Shares try { var newFiles = System.IO.Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly) - .Select(f => f.ToSoulseekFile(share)) - .ToDictionary(f => f.Filename, f => f); + .Select(filename => SoulseekFileFactory.Create(filename, share.LocalPath, share.RemotePath)) + .ToDictionary(file => file.Filename, file => file); // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. diff --git a/src/slskd/Shares/SoulseekFileFactory.cs b/src/slskd/Shares/SoulseekFileFactory.cs new file mode 100644 index 00000000..b7223c56 --- /dev/null +++ b/src/slskd/Shares/SoulseekFileFactory.cs @@ -0,0 +1,76 @@ +// +// 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; + +namespace slskd.Shares +{ + using System.Collections.Generic; + using System.Linq; + using Soulseek; + + public interface ISoulseekFileFactory + { + File Create(string filename, string localPath, string remotePath); + } + + public class SoulseekFileFactory : ISoulseekFileFactory + { + private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; + private static readonly string[] AudioExtensions = { "aa", "aax", "aac", "aiff", "ape", "dsf", "flac", "m4a", "m4b", "m4p", "mp3", "mpc", "mpp", "ogg", "oga", "wav", "wma", "wv", "webm" }; + private static readonly HashSet SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet(); + + public File Create(string filename, string localPath, string remotePath) + { + var code = 1; + var maskedFilename = filename.ReplaceFirst(localPath, remotePath); + var size = new FileInfo(filename).Length; + var extension = Path.GetExtension(filename)[1..].ToLowerInvariant(); + List attributeList = default; + + if (SupportedExtensions.Contains(extension)) + { + attributeList = new List(); + TagLib.File file = default; + + try + { + file = TagLib.File.Create(filename, TagLib.ReadStyle.Average); + + attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); + attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); + + if (file.Properties.BitsPerSample > 0) + { + attributeList.Add(new FileAttribute(FileAttributeType.SampleRate, file.Properties.AudioSampleRate)); + attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); + } + } + catch + { + // unsupported or corrupt file, noop + } + finally + { + file?.Dispose(); + } + } + + return new File(code, maskedFilename, size, extension, attributeList); + } + } +} -- cgit v1.2.3