Exemple #1
0
        /// <summary>
        /// Returns all directories.
        /// </summary>
        /// <param name="path">Path of directory.</param>
        public MochaCollectionResult <MochaDirectory> GetDirectories(MochaPath path)
        {
            var directories = new List <MochaDirectory>();

            if (!ExistsDisk(path.Path) && !ExistsDirectory(path))
            {
                return(new MochaCollectionResult <MochaDirectory>(directories));
            }

            var directoryRange = Database.GetXElement(
                $"FileSystem/{path.Path}").Elements().Where(
                x => x.Attribute("Type").Value == "Directory");

            for (int index = 0; index < directoryRange.Count(); index++)
            {
                MochaDirectory directory = GetDirectory($"{path.Path}/{directoryRange.ElementAt(index).Name.LocalName}");
                if (directory == null)
                {
                    continue;
                }

                directories.Add(directory);
            }

            return(new MochaCollectionResult <MochaDirectory>(directories));
        }
Exemple #2
0
        /// <summary>
        /// Add file.
        /// </summary>
        /// <param name="file">File to add.</param>
        /// <param name="path">Path of directory to add.</param>
        public void AddFile(MochaFile file, MochaPath path)
        {
            var parts = path.Path.Split('/');

            if (parts.Length == 1)
            {
                throw new MochaException("Files is cannot add in disks directly!");
            }
            if (!ExistsDisk(parts[0]))
            {
                throw new MochaException("Disk not found!");
            }
            if (!ExistsDirectory(path))
            {
                throw new MochaException("Directory not found!");
            }
            if (ExistsFile($"{path}/{file.FullName}"))
            {
                throw new MochaException("This file already exists!");
            }
            Database.OnChanging(this, new EventArgs());

            var originalname = path.Name();

            path = path.ParentPath();
            var element = Database.GetXElement($"FileSystem/{path.Path}").Elements().Where(x =>
                                                                                           x.Attribute("Type").Value == "Directory").First();

            var xFile = new XElement(file.FullName, Convert.ToBase64String(file.Stream.Bytes));

            xFile.Add(new XAttribute("Type", "File"));
            xFile.Add(new XAttribute("Description", file.Description));
            element.Add(xFile);
            Database.Save();
        }
Exemple #3
0
        /// <summary>
        /// Returns whether there is a file with the specified path.
        /// </summary>
        public bool ExistsFile(MochaPath path)
        {
            var originalname = path.Name();

            path = path.ParentPath();
            return(Database.GetXElement($"FileSystem/{path.Path}").Elements().Where(x =>
                                                                                    x.Attribute("Type").Value == "File" && x.Name.LocalName == originalname).Count() > 0);
        }
Exemple #4
0
        /// <summary>
        /// Return directory xml element by path.
        /// </summary>
        /// <param name="path">Path of xml element.</param>
        internal XElement GetDirectoryElement(MochaPath path)
        {
            var originalname = path.Name();

            path = path.ParentPath();
            var elements = Database.GetXElement($"FileSystem/{path.Path}").Elements().Where(x =>
                                                                                            x.Attribute("Type").Value == "Directory");

            return(elements.Count() == 0 ? null : elements.First());
        }
Exemple #5
0
        /// <summary>
        /// Add directory.
        /// </summary>
        /// <param name="directory">Directory to add.</param>
        /// <param name="path">Path to add.</param>
        public void AddDirectory(MochaDirectory directory, MochaPath path)
        {
            var parts = path.Path.Split('/');

            if (!ExistsDisk(parts[0]))
            {
                throw new MochaException("Disk not found!");
            }
            if (parts.Length != 1 && !ExistsDirectory(path.Path))
            {
                throw new MochaException("Directory not found!");
            }
            if (ExistsDirectory($"{path.Path}/{directory.Name}"))
            {
                throw new MochaException("This directory already exists!");
            }
            Database.OnChanging(this, new EventArgs());

            var originalname = path.Name();

            path = path.ParentPath();
            var element = Database.GetXElement(parts.Length == 1 ? "FileSystem" :
                                               $"FileSystem/{path.Path}").Elements().Where(x =>
                                                                                           (x.Attribute("Type").Value == "Disk" || x.Attribute("Type").Value == "Directory") &&
                                                                                           x.Name.LocalName == originalname).First();

            if (element == null)
            {
                return;
            }

            var xDirectory = new XElement(directory.Name);

            xDirectory.Add(new XAttribute("Type", "Directory"));
            xDirectory.Add(new XAttribute("Description", directory.Description));
            element.Add(xDirectory);

            for (int index = 0; index < directory.Files.Count; index++)
            {
                AddFile(directory.Files[index], path);
            }

            for (int index = 0; index < directory.Directories.Count; index++)
            {
                AddDirectory(directory.Directories[index], path);
            }

            if (directory.Files.Count == 0 || directory.Directories.Count == 0)
            {
                Database.Save();
            }
        }
Exemple #6
0
        /// <summary>
        /// Remove directory. Returns true if directory is exists and removed.
        /// </summary>
        /// <param name="path">Path of directory to remove.</param>
        public bool RemoveDirectory(MochaPath path)
        {
            Database.OnChanging(this, new EventArgs());

            var element = GetDirectoryElement(path);

            if (element == null)
            {
                return(false);
            }

            element.Remove();
            Database.Save();
            return(true);
        }
Exemple #7
0
        /// <summary>
        /// Remove file. Returns true if file is exists and removed.
        /// </summary>
        /// <param name="path">Path of file to remove.</param>
        public bool RemoveFile(MochaPath path)
        {
            if (!ExistsFile(path))
            {
                return(false);
            }
            Database.OnChanging(this, new EventArgs());

            var originalname = path.Name();

            path = path.ParentPath();
            Database.GetXElement($"FileSystem/{path.Path}").Elements().Where(x =>
                                                                             x.Attribute("Type").Value == "File" && x.Name.LocalName == originalname).First().Remove();

            Database.Save();
            return(true);
        }
Exemple #8
0
        /// <summary>
        /// Returns directory by path.
        /// </summary>
        /// <param name="path">Path of directory.</param>
        public MochaDirectory GetDirectory(MochaPath path)
        {
            if (!ExistsDirectory(path))
            {
                return(null);
            }

            var originalpath     = path.Path;
            var directoryElement = GetDirectoryElement(path);
            var directory        = new MochaDirectory(directoryElement.Name.LocalName);

            directory.Description = directoryElement.Attribute("Description").Value;
            directory.Files.AddRange(GetFiles(originalpath));
            directory.Directories.AddRange(GetDirectories(originalpath));

            return(directory);
        }
Exemple #9
0
        /// <summary>
        /// Returns all files.
        /// </summary>
        /// <param name="path">Path of directory.</param>
        public MochaCollectionResult <MochaFile> GetFiles(MochaPath path)
        {
            var files = new List <MochaFile>();

            if (!ExistsDirectory(path))
            {
                return(new MochaCollectionResult <MochaFile>(files));
            }

            var fileRange = Database.GetXElement($"FileSystem/{path.Path}").Elements().Where(
                x => x.Attribute("Type").Value == "File");

            for (int index = 0; index < fileRange.Count(); index++)
            {
                MochaFile file = GetFile($"{path.Path}/{fileRange.ElementAt(index).Name.LocalName}");
                files.Add(file);
            }

            return(new MochaCollectionResult <MochaFile>(files));
        }
Exemple #10
0
        /// <summary>
        /// Returns file by path.
        /// </summary>
        /// <param name="path">path of file.</param>
        public MochaFile GetFile(MochaPath path)
        {
            if (!ExistsFile(path))
            {
                return(null);
            }

            var originalname = path.Name();

            path = path.ParentPath();
            var fileElement = Database.GetXElement($"FileSystem/{path.Path}").Elements().Where(x =>
                                                                                               x.Attribute("Type").Value == "File" && x.Name.LocalName == originalname).First();

            var nameParts = fileElement.Name.LocalName.Split('.');
            var file      = new MochaFile(nameParts.First(), nameParts.Length == 1 ? string.Empty : nameParts.Last());

            file.Description  = fileElement.Attribute("Description").Value;
            file.Stream.Bytes = Convert.FromBase64String(fileElement.Value);

            return(file);
        }
 /// <summary>
 /// Read all files.
 /// </summary>
 /// <param name="fs">Target filesystem.</param>
 /// <param name="path">Path of directory.</param>
 /// <param name="query">Query for filtering.</param>
 public static MochaReader <MochaFile> ReadFiles(this MochaFileSystem fs, MochaPath path, Func <MochaFile, bool> query) =>
 new MochaReader <MochaFile>(fs.GetFiles(path).Where(query));
 /// <summary>
 /// Read all files.
 /// </summary>
 /// <param name="fs">Target filesystem.</param>
 /// <param name="path">Path of directory.</param>
 public static MochaReader <MochaFile> ReadFiles(this MochaFileSystem fs, MochaPath path) =>
 new MochaReader <MochaFile>(fs.GetFiles(path));
 /// <summary>
 /// Returns all files.
 /// </summary>
 /// <param name="fs">Target filesystem.</param>
 /// <param name="path">Path of directory.</param>
 /// <param name="query">Query for filtering.</param>
 public static MochaCollectionResult <MochaFile> GetFiles(this MochaFileSystem fs, MochaPath path, Func <MochaFile, bool> query) =>
 new MochaCollectionResult <MochaFile>(fs.GetFiles(path).Where(query));
 /// <summary>
 /// Read all directories.
 /// </summary>
 /// <param name="fs">Target filesystem.</param>
 /// <param name="path">Path of directory.</param>
 /// <param name="query">Query for filtering.</param>
 public static MochaReader <MochaDirectory> ReadDirectories(this MochaFileSystem fs, MochaPath path, Func <MochaDirectory, bool> query) =>
 new MochaReader <MochaDirectory>(fs.GetDirectories(path).Where(query));
 /// <summary>
 /// Read all directories.
 /// </summary>
 /// <param name="path">Path of directory.</param>
 public static MochaReader <MochaDirectory> ReadDirectories(this MochaFileSystem fs, MochaPath path) =>
 new MochaReader <MochaDirectory>(fs.GetDirectories(path));
Exemple #16
0
 /// <summary>
 /// Create new empty directory.
 /// </summary>
 /// <param name="path">Path to add.</param>
 /// <param name="name">Name of directory.</param>
 public void CreateDirectory(MochaPath path, string name) =>
 AddDirectory(new MochaDirectory(name), path);
Exemple #17
0
 /// <summary>
 /// Upload file.
 /// </summary>
 /// <param name="path">Path of file.</param>
 /// <param name="virtualPath">FileSystem path of file.</param>
 public void UploadFile(string path, MochaPath virtualPath) =>
 AddFile(MochaFile.Load(path), virtualPath);
Exemple #18
0
 /// <summary>
 /// Returns whether there is a directory with the specified path.
 /// </summary>
 public bool ExistsDirectory(MochaPath path)
 {
     return(GetDirectoryElement(path) != null);
 }