aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-03-18 15:49:00 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-03-18 15:49:00 -0500
commitcac0d9ea1826e226e04eab5bed22f8571c8bec7a (patch)
tree26847c872d5eb174e7f029dc420542b6282842bb /src
parent0f12d85c5babd68d8b3702279cfcb7255978a9a9 (diff)
downloadslskd-cac0d9ea1826e226e04eab5bed22f8571c8bec7a.tar.gz
add logic to create File with metadata
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Shares/Extensions.cs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/slskd/Shares/Extensions.cs b/src/slskd/Shares/Extensions.cs
new file mode 100644
index 00000000..aa1936b5
--- /dev/null
+++ b/src/slskd/Shares/Extensions.cs
@@ -0,0 +1,66 @@
+// <copyright file="Extensions.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+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<string> 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<FileAttribute> attributeList = default;
+
+ if (SupportedExtensions.Contains(extension))
+ {
+ attributeList = new List<FileAttribute>();
+
+ try
+ {
+ var file = TagLib.File.Create(filename);
+
+ 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
+ }
+ }
+
+ return new File(code, maskedFilename, size, extension, attributeList);
+ }
+ }
+}