aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/IO/FileSystemRepository.cs
blob: 07328d72d9207d27533a0651118b9404fe5821da (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using MediaBrowser.Common.Extensions;
using System;
using System.IO;

namespace MediaBrowser.Common.IO
{
    /// <summary>
    /// This is a wrapper for storing large numbers of files within a directory on a file system.
    /// Simply pass a filename into GetResourcePath and it will return a full path location of where the file should be stored.
    /// </summary>
    public class FileSystemRepository
    {
        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>The path.</value>
        protected string Path { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileSystemRepository" /> class.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        public FileSystemRepository(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException();
            }

            Path = path;
        }

        /// <summary>
        /// Gets the full path of where a resource should be stored within the repository
        /// </summary>
        /// <param name="uniqueName">Name of the unique.</param>
        /// <param name="fileExtension">The file extension.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        public string GetResourcePath(string uniqueName, string fileExtension)
        {
            if (string.IsNullOrEmpty(uniqueName))
            {
                throw new ArgumentNullException("uniqueName");
            }

            if (string.IsNullOrEmpty(fileExtension))
            {
                throw new ArgumentNullException("fileExtension");
            }
            
            var filename = uniqueName.GetMD5() + fileExtension;

            return GetResourcePath(filename);
        }

        /// <summary>
        /// Gets the resource path.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public string GetResourcePath(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }
            
            var prefix = filename.Substring(0, 1);

            var path = System.IO.Path.Combine(Path, prefix);
            
            return System.IO.Path.Combine(path, filename);
        }

        /// <summary>
        /// Determines if a resource is present in the repository
        /// </summary>
        /// <param name="uniqueName">Name of the unique.</param>
        /// <param name="fileExtension">The file extension.</param>
        /// <returns><c>true</c> if the specified unique name contains resource; otherwise, <c>false</c>.</returns>
        public bool ContainsResource(string uniqueName, string fileExtension)
        {
            return ContainsFilePath(GetResourcePath(uniqueName, fileExtension));
        }

        /// <summary>
        /// Determines if a file with a given name is present in the repository
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns><c>true</c> if the specified filename contains filename; otherwise, <c>false</c>.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public bool ContainsFilename(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException();
            }

            return ContainsFilePath(GetResourcePath(filename));
        }

        /// <summary>
        /// Determines if a file is present in the repository
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns><c>true</c> if [contains file path] [the specified path]; otherwise, <c>false</c>.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public bool ContainsFilePath(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException();
            }
            
            return File.Exists(path);
        }
    }
}