public DirectoryModel RenameDirectory(DirectoryModel dirToChange, string newname)
        {
            if (dirToChange == null)
                return null;


            var hash = (DecodeHashToPath(dirToChange.Hash, false));
            ElfinderFile fi = DB.GetModelByHash(hash);

            if (fi == null)
                return null;

            fi.Name = newname;
            fi.Mtime = System.DateTime.Now;

            DB.UpdateModel(fi);

            var parent = DB.GetModel(fi.Parent_id);
            return createDirectoryModel(fi, EncodePathToHash(fi.Content),
                EncodePathToHash(parent.Content));

        }
        public bool DeleteDirectory(DirectoryModel directoryToRemove)
        {
            if (directoryToRemove == null)
                return false;

            string hash = DecodeHashToPath(directoryToRemove.Hash, false);

            ElfinderFile source = DB.GetModelByHash(hash);
            if (source == null)
                return false;

            using (var context = new FileManagerContext())
            {

                int childcount = context.ElfinderFiles.Count(x => x.Parent_id == source.Id && x.IsDelete != true);
                //子目录不为空
                if (childcount > 0)
                {
                    return false;
                    throw new Exception("所删除目录不为空");
                }
            }

            DB.DeleteModel(source);

            return true;
        }
        public DirectoryModel CreateDirectory(DirectoryModel inDir, string name)
        {
            if (inDir == null)
                return null;

            string path = DecodeHashToPath(inDir.Hash, false);


            var model = DB.GetModelByHash(path);


            using (var context = new FileManagerContext())
            {
                bool exist = context.ElfinderFiles.Count(x => x.Name == name && x.Parent_id == model.Id) > 0;
                if (exist)
                    return null;
            }

            try
            {
                var createdDirectory = new ElfinderFile
                {
                    Name = name,
                    Parent_id = model.Id,// int.Parse(DecodeHashToPath(inDir.Hash, false)),
                    Mime = "directory",
                    Read = true,
                    Write = true,
                    Width = 0,
                    Content = this._key.CreateFileKey("noname"),
                    Locked = false,
                    Height = 0,
                    Hidden = false,
                    Mtime = DateTime.Now,
                    Size = 0
                };
                using (var context = new FileManagerContext())
                {
                    context.ElfinderFiles.Add(createdDirectory);
                    context.SaveChanges();
                }

                return createDirectoryModel(createdDirectory, EncodePathToHash(createdDirectory.Content),
                    inDir.Hash);
            }
            catch
            {
                throw;
            }
        }
        public FileModel CreateFile(DirectoryModel inDir, string name)
        {
            if (inDir == null)
                return null;

            string path = DecodeHashToPath(inDir.Hash, false);


            var model = DB.GetModelByHash(path);

            using (var context = new FileManagerContext())
            {
                bool exist = context.ElfinderFiles.Count(x => x.Name == name && x.Parent_id == model.Id) > 0;
                if (exist)
                    return null;
            }

            try
            {

                var filekey = this._key.CreateFileKey(name);

                this._uploadFile.SaveFile(new byte[0], filekey);

                var createdFile = new ElfinderFile
                {
                    Name = name,
                    Parent_id = model.Id,
                    Mime = name.Substring(name.LastIndexOf('.') + 1),
                    Read = true,
                    Write = true,
                    Width = 0,
                    Content = filekey,
                    Locked = false,
                    Height = 0,
                    Hidden = false,
                    Mtime = DateTime.Now,
                    Size = 0
                };
                using (var context = new FileManagerContext())
                {
                    context.ElfinderFiles.Add(createdFile);
                    context.SaveChanges();
                }

                return createFileModel(createdFile, inDir.Hash);
            }
            catch
            {
                throw new Exception();
                return null;
            }
        }
        public IEnumerable<FileModel> GetFiles(DirectoryModel rootDirectory)
        {
            if (rootDirectory == null)
                return new List<FileModel>();

            string dirPath = DecodeHashToPath(rootDirectory.Hash, false);



            var dirs = DB.GetChildDirFilesByHash(hash: dirPath);


            IEnumerable<FileModel> filesModels =
                dirs.Select(x => { return createFileModel(x, rootDirectory.Hash); }).Where(x => x != null);
            return filesModels;
        }
 public string GetPathToRoot(DirectoryModel startDir)
 {
     var sb = new StringBuilder(startDir.Name);
     DirectoryModel rootDir = GetRootDirectory();
     DirectoryModel currDir = startDir;
     while (currDir != null && currDir.Hash != rootDir.Hash)
     {
         if (currDir.ParentHash == null)
             break;
         DirectoryModel 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 DirectoryModel DuplicateDirectory(DirectoryModel directoryToDuplicate)
        {
            if (directoryToDuplicate == null)
                return null;

            string path = DecodeHashToPath(directoryToDuplicate.Hash);
            var 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
                var 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 IEnumerable<FileModel> GetFiles(DirectoryModel rootDirectory)
        {
            if (rootDirectory == null)
                return new List<FileModel>();

            string dirPath = DecodeHashToPath(rootDirectory.Hash);
            string[] files = Directory.GetFiles(dirPath);
            IEnumerable<FileModel> filesModels = files.Select(x =>
            {
                FileInfo fi = getValidFileInfo(x);
                if (fi == null)
                    return null;
                return createFileModel(fi, rootDirectory.Hash);
            }).Where(x => x != null);
            return filesModels;
        }
        public bool DeleteDirectory(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 DirectoryModel CopyDirectory(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);
            var 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
                var 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 DirectoryModel RenameDirectory(DirectoryModel dirToChange, string newname)
        {
            if (dirToChange == null)
                return null;

            string path = DecodeHashToPath(dirToChange.Hash);
            var 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);
                var newDirInfo = new DirectoryInfo(newPath);
                return createDirectoryModel(newDirInfo, EncodePathToHash(newDirInfo.FullName),
                    EncodePathToHash(parentDir));
            }
            catch
            {
                return null;
            }
        }
        public FileModel CreateFile(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 (FileStream createdFile = File.Create(path))
                {
                }
                var fi = new FileInfo(path);
                return createFileModel(fi, inDir.Hash);
            }
            catch
            {
                return null;
            }
        }
        public DirectoryModel CreateDirectory(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
            {
                DirectoryInfo createdDirectory = Directory.CreateDirectory(path);
                return createDirectoryModel(createdDirectory, EncodePathToHash(path), inDir.Hash);
            }
            catch
            {
                return null;
            }
        }
        public DirectoryModel CopyDirectory(DirectoryModel directoryToCopy, string destinationDirectory, bool cut)
        {
            if (directoryToCopy == null)
                return null;
            //if (!Directory.Exists(destinationDirectory))
            //    return null;

            string hash = DecodeHashToPath(directoryToCopy.Hash, false);

            ElfinderFile source = DB.GetModelByHash(hash);
            if (source == null)
                return null;

            var destid = (destinationDirectory);
            ElfinderFile dest = DB.GetModelByHash(destid);

            if (dest == null)
                return null;


            if (cut)
            {
                source.Parent_id = dest.Id;
                //context.ElfinderFiles.Attach(source);
                //context.Entry(source).State = EntityState.Modified;

                DB.UpdateModel(source);
            }
            else
            {
                source.Parent_id = dest.Id;
                //context.ElfinderFiles.Add(source);
                source.Content = this._key.CreateFileKey(source.Name);
                DB.AddModel(source);
            }

            //context.SaveChanges();

            var parent = DB.GetModel(source.Parent_id);

            return createDirectoryModel(source, EncodePathToHash(source.Content),
                EncodePathToHash(parent.Content));

        }
        public IEnumerable<DirectoryModel> GetSubdirectoriesFlat(DirectoryModel rootDirectory, int? maxDepth = null)
        {
            if (rootDirectory == null)
                return new List<DirectoryModel>();

            string dirPath = DecodeHashToPath(rootDirectory.Hash, false);
            var subDirs = new List<DirectoryModel>();
            //try
            //{
            getSubDirs(rootDirectory.Hash, rootDirectory.Hash, subDirs, maxDepth ?? _config.MaxTreeLevel, 0);
            //}
            //catch
            //{
            //    throw;
            //    return new List<DirectoryModel>();
            //}
            return subDirs;
        }
        public DirectoryModel DuplicateDirectory(DirectoryModel directoryToDuplicate)
        {
            if (directoryToDuplicate == null)
                return null;
            string path = DecodeHashToPath(directoryToDuplicate.Hash, false);

            var source = DB.GetModelByHash(path);

            DB.AddModel(source);

            var parent = DB.GetModel(source.Parent_id);

            return createDirectoryModel(source, source.Content, parent.Content);
        }
        public IEnumerable<DirectoryModel> GetSubdirectoriesFlat(DirectoryModel rootDirectory, int? maxDepth = null)
        {
            if (rootDirectory == null)
                return new List<DirectoryModel>();

            string dirPath = DecodeHashToPath(rootDirectory.Hash);
            var subDirs = new List<DirectoryModel>();
            try
            {
                getSubDirs(dirPath, rootDirectory.Hash, subDirs, maxDepth ?? _config.MaxTreeLevel, 0);
            }
            catch
            {
                return new List<DirectoryModel>();
            }
            return subDirs;
        }