public void TestFileObjectSerialization()
        {
            FileObject file = new FileObject();
            file.LocalPath = Directory.GetFiles(".").First();
            file.LoadFromDisk();

            file.Path = "/" + file.Name;

            var xml = file.Serialize();

            var strResult = xml.ToString();

            var roundtrip = new FileObject();
            Assert.IsTrue(roundtrip.Validate(xml));
            roundtrip.Deserialize(xml);

            assertAreEqual(file, roundtrip);
        }
        public CallResult GetFileInfo(string virtualPath)
        {
            logger.Info("Getting FileInfo for {0}", virtualPath);

            if (mountErrorOccurred)
            {
                logger.Error("Mount error found");
                return new ErrorResult(RemoteErrorCode.MountError);
            }

            if (mountErrorOccurred)
            {
                logger.Error("Mount error found");
                return new ErrorResult(RemoteErrorCode.MountError);
            }

            try
            {
                if (!checkPermission(virtualPath))
                {
                    logger.Error("Permission denied");
                    return new ErrorResult(RemoteErrorCode.NotAuthorizedError);
                }

                var localPath = getLocalPath(virtualPath);

                if (localPath.IsNullOrEmpty())
                    throw new FileNotFoundException();

                var file = new FileObject();
                file.LocalPath = localPath;
                file.LoadFromDisk();

                return new ResponseResult(file);

            }
            catch (Exception ex)
            {
                if (ex is FileNotFoundException || ex is DirectoryNotFoundException)
                {
                    logger.Error("Item not found");
                    return new ErrorResult(RemoteErrorCode.ItemNotFoundError);
                }
                else
                {
                    throw ex;
                }
            }
        }
Exemple #3
0
        public virtual void LoadFromDisk(int depth = -1)
        {
            logger.Info("Loading Directory {0}", LocalPath);


            if (!Directory.Exists(this.LocalPath))
            {
                throw new DirectoryNotFoundException(String.Format("Directory '{0}' not found", LocalPath));
            }



            DirectoryInfo info = new DirectoryInfo(LocalPath);

            this.Name       = info.Name;
            this.Created    = info.CreationTimeUtc;
            this.LastEdited = info.LastWriteTimeUtc;


            //  ##  Update files    ##
            try
            {
                foreach (string item in Directory.GetFiles(LocalPath))
                {
                    string fileName = System.IO.Path.GetFileName(item);

                    if (files.ContainsKey(fileName))
                    {
                        files[fileName].LoadFromDisk();
                    }
                    else
                    {
                        FileObject newFile = new FileObject();
                        newFile.Name      = fileName;
                        newFile.LocalPath = item;
                        AddFile(newFile);
                        newFile.LoadFromDisk();
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                files = new Dictionary <string, FileObject>();
            }



            //TODO test if this really works!
            files.Values.Where(x => !File.Exists(LocalPath)).Select(x => files.Remove(x.Name));

            //  ##  Update directories ##
            try
            {
                foreach (string item in Directory.GetDirectories(LocalPath))
                {
                    try
                    {
                        string dirName = ExtendedPath.GetDirectoryName(item);

                        if (!directories.ContainsKey(dirName))
                        {
                            DirectoryObject newDir = new DirectoryObject();

                            newDir.Name      = dirName;
                            newDir.LocalPath = item;

                            if (depth > 1 || depth < 0)
                            {
                                newDir.LoadFromDisk(depth - 1);
                            }

                            AddDirectory(newDir);
                        }
                    }
                    catch (IOException)
                    {
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                directories = new Dictionary <string, DirectoryObject>();
            }


            directories.Values.Where(x => !Directory.Exists(x.LocalPath)).Select(x => directories.Remove(x.Name));
            directories.Values.Select(x => { x.LoadFromDisk(); return(x); });
        }
        public virtual void LoadFromDisk(int depth = -1)
        {
            logger.Info("Loading Directory {0}", LocalPath);

            if (!Directory.Exists(this.LocalPath))
                throw new DirectoryNotFoundException(String.Format("Directory '{0}' not found", LocalPath));

            DirectoryInfo info = new DirectoryInfo(LocalPath);

            this.Name = info.Name;
            this.Created = info.CreationTimeUtc;
            this.LastEdited = info.LastWriteTimeUtc;

            //  ##  Update files    ##
            try
            {
                foreach (string item in Directory.GetFiles(LocalPath))
                {
                    string fileName = System.IO.Path.GetFileName(item);

                    if (files.ContainsKey(fileName))
                    {
                        files[fileName].LoadFromDisk();
                    }
                    else
                    {
                        FileObject newFile = new FileObject();
                        newFile.Name = fileName;
                        newFile.LocalPath = item;
                        AddFile(newFile);
                        newFile.LoadFromDisk();
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                files = new Dictionary<string, FileObject>();
            }

            //TODO test if this really works!
            files.Values.Where(x => !File.Exists(LocalPath)).Select(x => files.Remove(x.Name));

            //  ##  Update directories ##
            try
            {
                foreach (string item in Directory.GetDirectories(LocalPath))
                {
                    try
                    {
                        string dirName = ExtendedPath.GetDirectoryName(item);

                        if (!directories.ContainsKey(dirName))
                        {
                            DirectoryObject newDir = new DirectoryObject();

                            newDir.Name = dirName;
                            newDir.LocalPath = item;

                            if (depth > 1 || depth < 0)
                            {
                                newDir.LoadFromDisk(depth - 1);
                            }

                            AddDirectory(newDir);
                        }
                    }
                    catch (IOException)
                    {
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                directories = new Dictionary<string, DirectoryObject>();
            }

            directories.Values.Where(x => !Directory.Exists(x.LocalPath)).Select(x => directories.Remove(x.Name));
            directories.Values.Select(x => { x.LoadFromDisk(); return x; });
        }