File size: 1,945 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;

namespace VersOne.Epub.Internal
{
    internal static class ContentPathUtils
    {
        private const string DIRECTORY_UP = "../";

        public static bool IsLocalPath(string path) => path != null ? !path.Contains("://") : throw new ArgumentNullException(nameof(path));

        public static string GetDirectoryPath(string filePath)
        {
            int lastSlashIndex = filePath.LastIndexOf('/');
            if (lastSlashIndex == -1)
            {
                return String.Empty;
            }
            else
            {
                return filePath.Substring(0, lastSlashIndex);
            }
        }

        public static string Combine(string directory, string fileName)
        {
            directory = RemoveRepeatingSlashes(directory);
            fileName = RemoveRepeatingSlashes(fileName);
            if (String.IsNullOrEmpty(directory))
            {
                return fileName;
            }
            else if (String.IsNullOrEmpty(fileName))
            {
                return directory;
            }
            else
            {
                while (fileName.StartsWith(DIRECTORY_UP))
                {
                    int lastDirectorySlashIndex = directory.LastIndexOf('/');
                    directory = lastDirectorySlashIndex != -1 ? directory.Substring(0, lastDirectorySlashIndex) : String.Empty;
                    fileName = fileName.Substring(DIRECTORY_UP.Length);
                }
                return String.IsNullOrEmpty(directory) ? fileName : String.Concat(directory, '/', fileName);
            }
        }

        private static string RemoveRepeatingSlashes(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                return path;
            }
            while (path.Contains("//"))
            {
                path = path.Replace("//", "/");
            }
            return path;
        }
    }
}