/// <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(); }
/// <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); }
/// <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()); }
/// <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(); } }
/// <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); }
/// <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); }