Beispiel #1
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();
        }
Beispiel #2
0
        /// <summary>
        /// Set datas by file.
        /// </summary>
        /// <param name="path">Path of target file.</param>
        public void From(string path)
        {
            MochaFile file = Load(path);

            Name      = file.Name;
            Extension = file.Extension;
            Stream    = file.Stream;
        }
Beispiel #3
0
        /// <summary>
        /// Return file from path.
        /// </summary>
        /// <param name="path">Path of target file.</param>
        public static MochaFile Load(string path)
        {
            FileInfo fi = new FileInfo(path);

            if (!fi.Exists)
            {
                throw new MochaException("This path does not show a file!");
            }

            MochaFile file = new MochaFile(fi.Name, fi.Extension);

            file.Stream.Bytes = File.ReadAllBytes(path);
            return(file);
        }
Beispiel #4
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));
        }
Beispiel #5
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);
        }
Beispiel #6
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);