Exemple #1
0
        public static IOResource From(string value, bool strict = true)
        {
            var resource = FileResource.From(value, strict: false) ?? FolderResource.From(value, strict: false) as IOResource;

            ExpectNot(strict && resource == null, "Value is not an I/O resource");

            return(resource);
        }
Exemple #2
0
        public static bool TryFrom(string folder, string name, out FileResource resource, bool extensionOptional = false)
        {
            resource = FileName.TryFrom(name, out var parsedName, extensionOptional)
        ? new FileResource(FolderResource.From(folder), parsedName)
        : null;

            return(resource != null);
        }
Exemple #3
0
        private IEnumerable <FolderLink> ReadFolderLinksCore(FolderResource subfolder, bool recursive = false)
        {
            var folderPath = Link.Then(subfolder).ToString();
            var pattern    = "*.*";
            var option     = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            return(Directory.GetDirectories(folderPath, pattern, option).Select(subdirectory => FolderLink.From(subdirectory)));
        }
Exemple #4
0
        public static FileResource From(FolderResource folder, string name, bool extensionOptional = false)
        {
            if (!TryFrom(folder, name, out var resource, extensionOptional))
            {
                throw new FormatException($"Failed to parse file name: {name}");
            }

            return(resource);
        }
Exemple #5
0
        //
        // Subfolder reads
        //

        public Many <IOResource> ReadFolder(FolderResource subfolder, bool recursive = false)
        {
            return(ReadLinksCore(subfolder, recursive)
                   .Select(link =>
            {
                return link is FolderLink
                                                ? ((FolderLink)link).RelativeTo(Link)
                                                : ((FileLink)link).RelativeTo(Link) as IOResource;
            })
                   .ToMany());
        }
Exemple #6
0
        public void Write(FolderResource folder, bool overwrite = true)
        {
            var folderPath = Link.Then(folder).ToString();

            if (Directory.Exists(folderPath))
            {
                return;
            }

            Expect(overwrite, "Folder exists and overwrite option is not set");

            Directory.CreateDirectory(folderPath);
        }
Exemple #7
0
        public static bool TryFrom(string value, out IOResource resource)
        {
            if (FileResource.TryFrom(value, out var file))
            {
                resource = file;
            }
            else
            {
                resource = FolderResource.From(value);
            }

            return(resource != null);
        }
Exemple #8
0
        public void Delete(FolderResource folder, bool strict = true)
        {
            var folderPath = Link.Then(folder).ToString();

            if (Directory.Exists(folderPath))
            {
                Directory.Delete(folderPath, recursive: true);
            }
            else
            {
                if (strict)
                {
                    ExpectNot(strict, "Folder not found");
                }
            }
        }
Exemple #9
0
        public static FolderLink FromLocal(string value, bool strict = true)
        {
            var parsedFolder = FolderResource.From(value, strict);

            if (parsedFolder != null && parsedFolder.Path.Segments.Count > 0)
            {
                var path = parsedFolder.Path.Segments.Count == 1
                                        ? LinkPath.Root
                                        : LinkPath.From(parsedFolder.Path.Segments.Skip(1));

                return(new FolderLink(parsedFolder.Path.Segments[0], FolderResource.From(path)));
            }

            Expect(strict, "Cannot parse folder link: " + value);

            return(null);
        }
Exemple #10
0
        public static bool TryFromLocal(string value, out FolderLink link)
        {
            link = null;

            var path = FolderResource.From(value).Path;

            if (path.Segments.Any())
            {
                var root = path.Segments[0];

                var resource = path.Segments.Count == 1
          ? LinkPath.Root
          : LinkPath.From(path.Segments.Skip(1));

                link = new FolderLink(root, FolderResource.From(resource));
            }

            return(link != null);
        }
Exemple #11
0
        public static bool TryFromUnc(string value, out FolderLink link)
        {
            link = null;

            if (!String.IsNullOrEmpty(value) && HasUncPrefix(value))
            {
                var path = FolderResource.From(value.Substring(2)).Path;

                var root = $@"\\{path.Segments[0]}";

                var resource = path.Segments.Count == 1
          ? LinkPath.Root
          : LinkPath.From(path.Segments.Skip(1));

                link = new FolderLink(root, FolderResource.From(resource), isUnc: true);
            }

            return(link != null);
        }
Exemple #12
0
        public static FolderLink FromUnc(string value, bool strict = true)
        {
            if (!String.IsNullOrEmpty(value) && value.StartsWith(@"\\"))
            {
                var parsedFolder = FolderResource.From(value.Substring(2), strict);

                if (parsedFolder != null)
                {
                    var root = @"\\" + parsedFolder.Path.Segments[0].ToString();

                    var path = parsedFolder.Path.Segments.Count == 1
                                                ? LinkPath.Root
                                                : LinkPath.From(parsedFolder.Path.Segments.Skip(1));

                    return(new FolderLink(root, FolderResource.From(path), isUnc: true));
                }
            }

            ExpectNot(strict, "Cannot parse UNC link: " + value);

            return(null);
        }
Exemple #13
0
        public static bool TryFrom(string value, out FileResource resource, bool extensionOptional = false)
        {
            resource = null;

            var parsedFolder = FolderResource.From(value);

            var file = parsedFolder.Path.Segments.LastOrDefault()?.ToString();

            if (file != null)
            {
                if (FileName.TryFrom(file, out var parsedFile, extensionOptional))
                {
                    if (!parsedFolder.TryUp(out var folderUp))
                    {
                        folderUp = parsedFolder;
                    }

                    resource = new FileResource(folderUp, parsedFile);
                }
            }

            return(resource != null);
        }
Exemple #14
0
        public new static FileResource From(string value, bool strict = true)
        {
            var parsedFolder = FolderResource.From(value, strict);

            if (parsedFolder != null)
            {
                var fileSegment = parsedFolder.Path.Segments.LastOrDefault();

                if (fileSegment != null)
                {
                    var parsedName = FileName.From(fileSegment.ToString(), strict);

                    if (parsedName != null)
                    {
                        return(new FileResource(parsedFolder.Up(strict: false), parsedName));
                    }
                }
            }

            ExpectNot(strict, "Cannot parse file resource");

            return(null);
        }
Exemple #15
0
 public bool FolderExists(FolderResource subfolder)
 {
     return(Directory.Exists(Link.Then(subfolder).ToString()));
 }
Exemple #16
0
 public void Move(FolderResource sourceFolder, FolderResource destinationFolder)
 {
     Directory.Move(Link.Then(sourceFolder).ToString(), Link.Then(destinationFolder).ToString());
 }
Exemple #17
0
        public static FileResource From(FolderResource folder, string name, bool strict = true)
        {
            var parsedName = FileName.From(name, strict);

            return(parsedName == null ? null : new FileResource(folder, parsedName));
        }
Exemple #18
0
 public FolderLink Then(FolderResource folder) =>
 new FolderLink(Root, Resource.Then(folder));
Exemple #19
0
        //
        // Factory
        //

        public static FileResource From(FolderResource folder, FileName name)
        {
            return(new FileResource(folder, name));
        }
Exemple #20
0
        public static FileResource From(string folder, string name, bool strict = true)
        {
            var parsedFolder = FolderResource.From(folder, strict);

            return(parsedFolder == null ? null : From(parsedFolder, name, strict));
        }
Exemple #21
0
 public Many <FolderResource> ReadFolders(FolderResource subfolder, bool recursive = false)
 {
     return(ReadFolderLinksCore(subfolder, recursive).Select(folderLink => folderLink.RelativeTo(Link)).ToMany());
 }
Exemple #22
0
 public static FileResource From(string folder, FileName name) =>
 From(FolderResource.From(folder), name);
Exemple #23
0
 public static FileResource From(FolderResource folder, FileName name) =>
 new FileResource(folder, name);
Exemple #24
0
 public static HttpResource From(FolderResource folder) =>
 From(folder.ToString(altSlash: true));
Exemple #25
0
 public IFolder Then(FolderResource folder)
 {
     return(new LocalFolder(Link.Then(folder)));
 }
Exemple #26
0
 FileResource(FolderResource folder, FileName name)
 {
     Folder = folder;
     Name   = name;
 }
Exemple #27
0
 public Many <FolderLink> ReadFolderLinks(FolderResource subfolder, bool recursive = false)
 {
     return(ReadFolderLinksCore(subfolder, recursive).ToMany());
 }
Exemple #28
0
 public FileResource RelativeTo(FolderResource folder) =>
 new FileResource(Folder.RelativeTo(folder), Name);
Exemple #29
0
        public static bool TryFrom(LinkText root, string resource, out FolderLink link)
        {
            link = From(root, FolderResource.From(resource));

            return(true);
        }
Exemple #30
0
 public FileResource RelativeTo(FolderResource folder)
 {
     return(new FileResource(Folder.RelativeTo(folder), Name));
 }