示例#1
0
        public void PathParseTest()
        {
            var text = @"zfs.local: / data  / images /  1/ year   /   month-day / [1]123.jpg";
            var path = Path.Parse(text);

            Assert.Equal("zfs.local", path.Scheme);
            Assert.Equal("/data/images/1/year/month-day/[1]123.jpg", path.FullPath);
            Assert.Equal("/data/images/1/year/month-day/", path.DirectoryName);
            Assert.Equal("[1]123.jpg", path.FileName);

            Assert.True(Zongsoft.IO.Path.TryParse("/images/avatar/large/steve.jpg", out path));
            Assert.Null(path.Scheme);
            Assert.True(path.IsFile);

            Assert.False(Zongsoft.IO.Path.TryParse("zs:", out path));
            Assert.True(Zongsoft.IO.Path.TryParse("zs: / ", out path));
            Assert.Equal("zs", path.Scheme);
            Assert.Equal(PathAnchor.Root, path.Anchor);
            Assert.True(path.IsDirectory);
            Assert.Equal("/", path.FullPath);
            Assert.Equal("zs:/", path.Url);
            Assert.Equal(0, path.Segments.Length);

            Assert.True(Zongsoft.IO.Path.TryParse("../directory/", out path));
            Assert.True(string.IsNullOrEmpty(path.Scheme));
            Assert.Equal(PathAnchor.Parent, path.Anchor);
            Assert.True(path.IsDirectory);
            Assert.Equal("../directory/", path.FullPath);
            Assert.Equal("../directory/", path.Url);
            Assert.Equal(2, path.Segments.Length);
            Assert.Equal("directory", path.Segments[0]);
            Assert.True(string.IsNullOrEmpty(path.Segments[1]));
        }
        public static string GetLocalPath(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException("text");
            }

            var path = Path.Parse(text);

            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.MacOSX:
            case PlatformID.Unix:
                return(path.FullPath);
            }

            var driveName = path.Schema;
            var fullPath  = path.FullPath;

            if (string.IsNullOrEmpty(driveName))
            {
                var parts = fullPath.Split('/');

                if (parts != null && parts.Length > 0)
                {
                    var index = (string.IsNullOrEmpty(parts[0]) && parts.Length > 1) ? 1 : 0;
                    driveName = parts[index];
                    fullPath  = "/" + string.Join("/", parts, index + 1, parts.Length - (index + 1));
                }

                if (string.IsNullOrWhiteSpace(driveName))
                {
                    throw new PathException(string.Format("The '{0}' path not cantians drive.", text));
                }
            }

            if (driveName.Length > 1)
            {
                var drives  = System.IO.DriveInfo.GetDrives();
                var matched = false;

                foreach (var drive in drives)
                {
                    matched = MatchDriver(drive, driveName);

                    if (matched)
                    {
                        driveName = drive.Name[0].ToString();
                        break;
                    }
                }

                if (!matched)
                {
                    throw new PathException(string.Format("Not matched drive for '{0}' path.", text));
                }
            }

            return(driveName + ":" + fullPath);
        }
        private static IFileSystem GetFileSystem(string text, bool throwException, out Path path)
        {
            //设置输出参数的默认值
            path = null;

            var providers = _providers;

            if (providers == null)
            {
                if (throwException)
                {
                    throw new InvalidOperationException("The value of 'Providers' property is null.");
                }

                return(null);
            }

            //解析路径文本
            path = Path.Parse(text, throwException);

            if (path == null)
            {
                return(null);
            }

            var scheme = path.Scheme;

            //如果路径模式为空则使用默认文件系统方案
            if (string.IsNullOrEmpty(scheme))
            {
                scheme = FileSystem.Scheme;

                //如果文件系统模式为空则返回本地文件方案
                if (string.IsNullOrEmpty(scheme))
                {
                    return(LocalFileSystem.Instance);
                }
            }

            //根据文件系统模式从服务容器中获得对应的文件系统提供程序
            var fileSystem = providers.Resolve <IFileSystem>(scheme);

            if (fileSystem == null)
            {
                if (throwException)
                {
                    throw new InvalidOperationException(string.Format("Can not obtain the File or Directory provider by the '{0}'.", text));
                }

                return(null);
            }

            return(fileSystem);
        }
示例#4
0
        public void PathParseTest()
        {
            var text = @"zfs.local:/data/images/1/year/month-day/[1]123.jpg";
            var path = Path.Parse(text);

            Assert.AreEqual("zfs.local", path.Schema);
            Assert.AreEqual("/data/images/1/year/month-day/[1]123.jpg", path.FullPath);
            Assert.AreEqual("/data/images/1/year/month-day/", path.DirectoryName);
            Assert.AreEqual("[1]123.jpg", path.FileName);

            Assert.IsTrue(Zongsoft.IO.Path.TryParse("/images/avatar/large/steve.jpg", out path));
        }
示例#5
0
        public void DirectoryCreate()
        {
            var text = @"zfs.local:/d/temp/sub-dir/[1]123.jpg";
            var path = Path.Parse(text);

            var directoryUrl = path.Scheme + ":" + path.DirectoryName;

            //创建目录
            FileSystem.Directory.Create(directoryUrl);

            Assert.True(FileSystem.Directory.Exists(directoryUrl));
            Assert.True(FileSystem.Directory.Delete(directoryUrl));
        }
示例#6
0
        private static IFileSystem GetFileSystem(string text, bool throwException, out Path path)
        {
            if (throwException)
            {
                path = Path.Parse(text);
            }
            else if (!Path.TryParse(text, out path))
            {
                return(null);
            }

            var scheme = path.Scheme;

            //如果路径模式为空则使用默认文件系统方案
            if (string.IsNullOrEmpty(scheme))
            {
                if (path.Anchor == PathAnchor.Application)
                {
                    return(LocalFileSystem.Instance);
                }

                scheme = FileSystem.Scheme;

                //如果文件系统模式为空则返回本地文件方案
                if (string.IsNullOrEmpty(scheme))
                {
                    return(LocalFileSystem.Instance);
                }
            }

            //尝试获取对应的文件系统提供程序
            _providers.TryGet(scheme, out var fileSystem);

            if (fileSystem == null)
            {
                if (throwException)
                {
                    throw new IOException($"Can not obtain the File or Directory provider by the '{text}'.");
                }

                return(null);
            }

            return(fileSystem);
        }
示例#7
0
        public PathInfo(string path, DateTime?createdTime = null, DateTime?modifiedTime = null, string url = null)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException("path");
            }

            _path = Path.Parse(path);
            _url  = url;

            if (createdTime.HasValue)
            {
                _createdTime = createdTime.Value;
            }

            if (modifiedTime.HasValue)
            {
                _modifiedTime = modifiedTime.Value;
            }
            else
            {
                _modifiedTime = _createdTime;
            }
        }
示例#8
0
 public static string GetLocalPath(string text)
 {
     return(GetLocalPath(Path.Parse(text)));
 }