aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Files/FileService.cs
blob: 7194cc04362ecf56273c040a41669ec072c9188d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// <copyright file="FileService.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 Microsoft.Extensions.Options;

namespace slskd.Files
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;

    /// <summary>
    ///     Manages files on disk.
    /// </summary>
    public class FileService : IFileService
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="FileService"/> class.
        /// </summary>
        public FileService(
            IOptionsSnapshot<Options> optionsSnapshot)
        {
            OptionsSnapshot = optionsSnapshot;
        }

        private IOptionsSnapshot<Options> OptionsSnapshot { get; }

        /// <summary>
        ///     Lists the contents in the specified <paramref name="parentDirectory"/>, optionally applying the
        ///     specified <paramref name="enumerationOptions"/>.
        /// </summary>
        /// <param name="rootDirectory">The root directory.</param>
        /// <param name="parentDirectory">An optional subdirectory from which to start the listing.</param>
        /// <param name="enumerationOptions">Optional enumeration options to apply.</param>
        /// <returns>The list of found contents.</returns>
        /// <exception cref="NotFoundException">Thrown if the specified directory does not exist.</exception>
        /// <exception cref="ForbiddenException">Thrown if the specified directory is restricted.</exception>
        public async Task<IEnumerable<FilesystemDirectory>> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null)
        {
            var configuredDirectories = OptionsSnapshot.Value.Directories;
            var allowedDirectories = new[] { configuredDirectories.Incomplete, configuredDirectories.Downloads };

            if (!allowedDirectories.Any(d => rootDirectory == d))
            {
                throw new ForbiddenException($"For security reasons, only the configured Downloads and Incomplete directories can be listed");
            }

            var fullDirectory = Path.Combine(rootDirectory, parentDirectory);

            if (!Directory.Exists(fullDirectory))
            {
                throw new NotFoundException($"The directory '{fullDirectory}' does not exist");
            }

            return await Task.Run(() =>
            {
                var dir = new DirectoryInfo(fullDirectory);

                var contents = dir.GetFileSystemInfos("*", enumerationOptions);

                var files = contents
                    .OfType<FileInfo>()
                    .Select(f => FilesystemFile.FromFileInfo(f));

                var dirs = contents
                    .OfType<DirectoryInfo>()
                    .Prepend(dir)
                    .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with
                    {
                        FullName = d.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'),
                        Files = files
                            .Where(f => Path.GetDirectoryName(f.FullName) == d.FullName)
                            .Select(f => f with { FullName = f.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/') }),
                    });

                return dirs;
            });
        }
    }
}