diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-03-18 15:48:43 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-03-18 15:48:43 -0500 |
| commit | 0f12d85c5babd68d8b3702279cfcb7255978a9a9 (patch) | |
| tree | 66daeaa6b5915c5ae8af13eb784e448b39b2d8f1 /src | |
| parent | 98eda1c5485195da455c3a7d4d9c6b8fd677226c (diff) | |
| download | slskd-0f12d85c5babd68d8b3702279cfcb7255978a9a9.tar.gz | |
move Share
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Shares/Domain/Share.cs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/slskd/Shares/Domain/Share.cs b/src/slskd/Shares/Domain/Share.cs new file mode 100644 index 00000000..890c8565 --- /dev/null +++ b/src/slskd/Shares/Domain/Share.cs @@ -0,0 +1,75 @@ +// <copyright file="Share.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> + +namespace slskd.Shares +{ + using System.Linq; + using System.Text.RegularExpressions; + + /// <summary> + /// A file share. + /// </summary> + public sealed class Share + { + /// <summary> + /// Initializes a new instance of the <see cref="Share"/> class. + /// </summary> + /// <param name="share"></param> + public Share(string share) + { + Raw = share; + IsExcluded = share.StartsWith('-') || share.StartsWith('!'); + + if (IsExcluded) + { + share = share[1..]; + } + + // test to see whether an alias has been specified + var matches = Regex.Matches(share, @"^\[(.*)\](.*)$"); + + if (matches.Any()) + { + // split the alias from the path, and validate the alias + var groups = matches[0].Groups; + Alias = groups[1].Value; + LocalPath = groups[2].Value; + } + else + { + Alias = share.Split(new[] { '/', '\\' }).Last(); + LocalPath = share; + } + + var parent = System.IO.Directory.GetParent(LocalPath).FullName.TrimEnd('/', '\\'); + + Mask = Compute.MaskHash(parent); + + var maskedPath = LocalPath.ReplaceFirst(parent, Mask); + + var aliasedSegment = LocalPath[(parent.Length + 1)..]; + RemotePath = maskedPath.ReplaceFirst(aliasedSegment, Alias); + } + + public string Alias { get; init; } + public bool IsExcluded { get; init; } + public string LocalPath { get; init; } + public string Mask { get; init; } + public string Raw { get; init; } + public string RemotePath { get; init; } + } +} |
