public Model.DirectoryModel CreateDirectory(Model.DirectoryModel inDir, string name)
        {
            if (inDir == null)
            {
                return(null);
            }
            // compose final path of new directory
            string path = DecodeHashToPath(inDir.Hash);

            if (path.Last() != Path.DirectorySeparatorChar)
            {
                path += Path.DirectorySeparatorChar;
            }
            path += name;
            // check if directory exists
            if (Directory.Exists(path))
            {
                return(null);
            }

            try
            {
                var createdDirectory = Directory.CreateDirectory(path);
                return(createDirectoryModel(createdDirectory, EncodePathToHash(path), inDir.Hash));
            }
            catch
            {
                return(null);
            }
        }
        public string GetPathToRoot(Model.DirectoryModel startDir)
        {
            StringBuilder sb      = new StringBuilder(startDir.Name);
            var           rootDir = GetRootDirectory();
            var           currDir = startDir;

            while (currDir != null && currDir.Hash != rootDir.Hash)
            {
                if (currDir.ParentHash == null)
                {
                    break;
                }
                var parentDir = GetDirectoryByHash(currDir.ParentHash);
                if (parentDir == null)
                {
                    break;
                }
                // add our root dir
                sb.Insert(0, Path.DirectorySeparatorChar + parentDir.Name + Path.DirectorySeparatorChar);
                currDir = parentDir;
            }
            // trim separator from begining
            if (sb[0] == Path.DirectorySeparatorChar)
            {
                sb.Remove(0, 1);
            }
            return(sb.ToString());
        }
        public Model.FileModel CreateFile(Model.DirectoryModel inDir, string name)
        {
            if (inDir == null)
            {
                return(null);
            }
            // compose final path of new directory
            string path = DecodeHashToPath(inDir.Hash);

            if (path.Last() != Path.DirectorySeparatorChar)
            {
                path += Path.DirectorySeparatorChar;
            }
            path += name;
            // check if file exists
            if (File.Exists(path))
            {
                return(null);
            }

            try
            {
                using (var createdFile = File.Create(path)) { }
                FileInfo fi = new FileInfo(path);
                return(createFileModel(fi, inDir.Hash));
            }
            catch
            {
                return(null);
            }
        }
 public DirectoryResponse(Model.DirectoryModel cwd, Model.ObjectModel[] subItems,
                          Model.OptionsModel opts)
 {
     CWD      = cwd;
     SubItems = subItems;
     Options  = opts;
 }
        public Model.DirectoryModel CopyDirectory(Model.DirectoryModel directoryToCopy, string destinationDirectory,
                                                  bool cut)
        {
            if (directoryToCopy == null || string.IsNullOrWhiteSpace(destinationDirectory))
            {
                return(null);
            }
            if (!Directory.Exists(destinationDirectory))
            {
                return(null);
            }

            string        path = DecodeHashToPath(directoryToCopy.Hash);
            DirectoryInfo di   = new DirectoryInfo(path);
            // compose final directory
            string destDir = destinationDirectory;

            if (destDir.Last() != Path.DirectorySeparatorChar)
            {
                destDir += Path.DirectorySeparatorChar;
            }
            string newPath = destDir + di.Name;

            if (Directory.Exists(newPath))
            {
                return(null);
            }

            try
            {
                if (cut)
                {
                    Directory.Move(path, newPath);
                }
                else
                {
                    copyDirectory(path, newPath, true);
                }
                // get new parent dir
                DirectoryInfo newDirInfo = new DirectoryInfo(newPath);
                if (newDirInfo.Parent == null)
                {
                    return(null);
                }
                string parentDir = newDirInfo.Parent.FullName;
                if (parentDir.Last() != Path.DirectorySeparatorChar)
                {
                    parentDir += Path.DirectorySeparatorChar;
                }

                return(createDirectoryModel(newDirInfo, EncodePathToHash(newDirInfo.FullName),
                                            EncodePathToHash(parentDir)));
            }
            catch
            {
                return(null);
            }
        }
		public InitDirectoryResponse( string apiVersion,
			string[] netDrivers, string uploadMaxSize, //TODO: change to int in bytes and handle conversion here
			Model.DirectoryModel cwd, 
			Model.ObjectModel[] subItems,
			Model.OptionsModel opts )
			: base( cwd, subItems, opts )
		{
			Api = apiVersion;
			NetDrivers = netDrivers;
			UploadMaxSize = uploadMaxSize;
		}
        public Model.DirectoryModel DuplicateDirectory(Model.DirectoryModel directoryToDuplicate)
        {
            if (directoryToDuplicate == null)
            {
                return(null);
            }

            string        path = DecodeHashToPath(directoryToDuplicate.Hash);
            DirectoryInfo di   = new DirectoryInfo(path);
            // compose final directory path
            string destDir = di.Parent.FullName;

            if (destDir.Last() != Path.DirectorySeparatorChar)
            {
                destDir += Path.DirectorySeparatorChar;
            }
            string newPath = destDir + string.Format(_config.DuplicateDirectoryPattern, di.Name);

            // new directory shouldn't be here
            if (Directory.Exists(newPath))
            {
                return(null);
            }

            try
            {
                copyDirectory(path, newPath, true);
                // get new parent dir
                DirectoryInfo newDirInfo = new DirectoryInfo(newPath);
                if (newDirInfo.Parent == null)
                {
                    return(null);
                }
                string parentDir = newDirInfo.Parent.FullName;
                if (parentDir.Last() != Path.DirectorySeparatorChar)
                {
                    parentDir += Path.DirectorySeparatorChar;
                }

                return(createDirectoryModel(newDirInfo, EncodePathToHash(newDirInfo.FullName),
                                            EncodePathToHash(parentDir)));
            }
            catch
            {
                return(null);
            }
        }
        public CurrentDirectoryViewModel(FileListViewModel rootModel, Model.DirectoryModel model)
            : base(rootModel, model)
        {
            IsLoaded   = false;
            _rootModel = rootModel;

            _subEntries        = new CollectionViewSource();
            _subEntries.Source = SubEntriesInternal;
            _subEntries.SortDescriptions.Add(new SortDescription("IsDirectory", ListSortDirection.Descending));
            _subEntries.SortDescriptions.Add(new SortDescription("FullName", ListSortDirection.Ascending));

            _refreshCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => true,
                ExecuteDelegate    = x => Refresh()
            };


            #region FileSystemWatcher
            _watcher = new FileSystemWatcherEx(model.EmbeddedDirectoryEntry);

            var handler = (FileSystemEventHandlerEx) delegate(object sender, FileSystemEventArgsEx args)
            {
                if (args.FullPath.Equals(model.FullName))
                {
                    Refresh();
                }
            };
            var renameHandler = (RenameEventHandlerEx) delegate(object sender, RenameEventArgsEx args)
            {
                if (args.OldFullPath.Equals(model.FullName))
                {
                    Refresh();
                }
            };

            _watcher.OnChanged += handler;
            _watcher.OnCreated += handler;
            _watcher.OnDeleted += handler;
            _watcher.OnRenamed += renameHandler;
            #endregion
        }
        public IEnumerable <Model.DirectoryModel> GetSubdirectoriesFlat(Model.DirectoryModel rootDirectory, int?maxDepth = null)
        {
            if (rootDirectory == null)
            {
                return(new List <Model.DirectoryModel>());
            }

            string dirPath = DecodeHashToPath(rootDirectory.Hash);
            List <Model.DirectoryModel> subDirs = new List <Model.DirectoryModel>();

            try
            {
                getSubDirs(dirPath, rootDirectory.Hash, subDirs, maxDepth ?? _config.MaxTreeLevel, 0);
            }
            catch
            {
                return(new List <Model.DirectoryModel>());
            }
            return(subDirs);
        }
        public Model.DirectoryModel RenameDirectory(Model.DirectoryModel dirToChange, string newname)
        {
            if (dirToChange == null)
            {
                return(null);
            }

            string        path = DecodeHashToPath(dirToChange.Hash);
            DirectoryInfo di   = new DirectoryInfo(path);

            if (di.Parent == null)
            {
                return(null);
            }

            string parentDir = di.Parent.FullName;

            if (parentDir.Last() != Path.DirectorySeparatorChar)
            {
                parentDir += Path.DirectorySeparatorChar;
            }
            string newPath = parentDir + newname;

            if (Directory.Exists(newPath))
            {
                return(null);
            }

            try
            {
                Directory.Move(path, newPath);
                DirectoryInfo newDirInfo = new DirectoryInfo(newPath);
                return(createDirectoryModel(newDirInfo, EncodePathToHash(newDirInfo.FullName),
                                            EncodePathToHash(parentDir)));
            }
            catch
            {
                return(null);
            }
        }
        public IEnumerable <Model.FileModel> GetFiles(Model.DirectoryModel rootDirectory)
        {
            if (rootDirectory == null)
            {
                return(new List <Model.FileModel>());
            }

            string dirPath = DecodeHashToPath(rootDirectory.Hash);

            string[] files       = Directory.GetFiles(dirPath);
            var      filesModels = files.Select(x =>
            {
                var fi = getValidFileInfo(x);
                if (fi == null)
                {
                    return(null);
                }
                return(createFileModel(fi, rootDirectory.Hash));
            }).Where(x => x != null);

            return(filesModels);
        }
        public bool DeleteDirectory(Model.DirectoryModel directoryToDelete)
        {
            if (directoryToDelete == null)
            {
                return(false);
            }

            string path = DecodeHashToPath(directoryToDelete.Hash);

            if (!Directory.Exists(path))
            {
                return(false);
            }

            try
            {
                Directory.Delete(path, true);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
 public DirectoryViewModel(Model.DirectoryModel model) : base(model)
 {
 }
 public DirectoryViewModel(RootModelBase rootModel, Model.DirectoryModel model)
     : base(rootModel, model)
 {
 }
 public RenameResponse(string oldDirHash, Model.DirectoryModel renamedDir)
 {
     Added   = new Model.DirectoryModel[] { renamedDir };
     Removed = new string[] { oldDirHash };
 }
        }                                                 //For DummyNode

        public DirectoryTreeItemViewModel(RootModelBase rootModel, HierarchyViewModel parentModel, Model.DirectoryModel model) :
            base(rootModel, parentModel, model)
        {
            HasSubDirectories = EmbeddedDirectoryModel.EmbeddedDirectoryEntry.HasSubFolder;
            if (HasSubDirectories)
            {
                _subDirs.Add(dummyNode);
            }
            setUpBackgroundWorker();

            _refreshCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => true,
                ExecuteDelegate    = x => Refresh()
            };
        }
Ejemplo n.º 17
0
 public MkdirResponse( Model.DirectoryModel createdDirectory )
 {
     Added = new Model.DirectoryModel[] { createdDirectory };
 }
 public MkdirResponse(Model.DirectoryModel createdDirectory)
 {
     Added = new Model.DirectoryModel[] { createdDirectory };
 }
		public RenameResponse( string oldDirHash, Model.DirectoryModel renamedDir )
		{
			Added = new Model.DirectoryModel[] { renamedDir };
			Removed = new string[] { oldDirHash };
		}