public FileModel SetTextFileContent(FileModel fileToModify, string content)
        {


            var fileKey = DecodeHashToPath(fileToModify.Hash, false);

            var fi = DB.GetModelByHash(fileKey);

            if (fi == null)
                return null;

            var bytes = Encoding.Default.GetBytes(content);

            this._uploadFile.UpdateFile(bytes, fileKey);

            fi.Size = bytes.Length;

            DB.UpdateModel(fi);

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


            return createFileModel(fi, EncodePathToHash(parent.Content));
        }
        public FileModel DuplicateFile(FileModel fileToDuplicate)
        {
            if (fileToDuplicate == null)
                return null;

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


            var source = DB.GetModelByHash(path);

            DB.AddModel(source);

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

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

        }
        public string GetTextFileContent(FileModel fileToGet)
        {
            var fileKey = DecodeHashToPath(fileToGet.Hash, false);

            var url = this._key.GetFileUrl(fileKey);

            System.Net.WebClient net = new System.Net.WebClient();

            var file = net.DownloadData(url);


            return Encoding.Default.GetString(file);

        }
        public bool DeleteThumbnailFor(FileModel deleteThumbnailForFile)
        {

            //this._uploadFile.DeleteFile()
            //throw new NotImplementedException();

            var id = deleteThumbnailForFile.Hash;



            return true;
        }
        public FileModel CopyFile(FileModel fileToCopy, string destinationDirectory, bool cut)
        {
            if (fileToCopy == null)
                return null;
            string hash = DecodeHashToPath(fileToCopy.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 createFileModel(source, EncodePathToHash(parent.Content));

        }
        public FileModel RenameFile(FileModel fileToChange, string newname)
        {
            if (fileToChange == null)
                return null;


            //int id = int.Parse(fileToChange.Hash);

            var hash = DecodeHashToPath(fileToChange.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 createFileModel(fi, EncodePathToHash(parent.Content));

        }
        public bool DeleteFile(FileModel fileToRemove)
        {
            if (fileToRemove == null)
                return false;

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

            var model = DB.GetModelByHash(path);

            DB.DeleteModel(model);

            return
                true;
        }
        public string GetTextFileContent(FileModel fileToGet)
        {
            if (fileToGet == null)
                return null;

            string path = DecodeHashToPath(fileToGet.Hash);
            try
            {
                return File.ReadAllText(path);
            }
            catch
            {
                return null;
            }
        }
        public FileModel SetTextFileContent(FileModel fileToModify, string content)
        {
            if (fileToModify == null)
                return null;

            string path = DecodeHashToPath(fileToModify.Hash);
            if (!File.Exists(path))
                return null;

            try
            {
                File.WriteAllText(path, content);
                return createFileModel(new FileInfo(path), fileToModify.ParentHash);
            }
            catch
            {
                return null;
            }
        }
        public FileModel DuplicateFile(FileModel fileToDuplicate)
        {
            if (fileToDuplicate == null)
                return null;

            string path = DecodeHashToPath(fileToDuplicate.Hash);

            var fi = new FileInfo(path);
            // compose final file path
            string destDir = fi.DirectoryName;
            if (destDir.Last() != Path.DirectorySeparatorChar)
                destDir += Path.DirectorySeparatorChar;
            string newPath = destDir + string.Format(_config.DuplicateFilePattern, fi.Name, fi.Extension);
            // new file should be here
            if (File.Exists(newPath))
                return null;

            try
            {
                File.Copy(path, newPath);

                var newFileInfo = new FileInfo(newPath);
                return createFileModel(newFileInfo, EncodePathToHash(destDir));
            }
            catch
            {
                return null;
            }
        }
        public FileModel CopyFile(FileModel fileToCopy, string destinationDirectory,
            bool cut)
        {
            if (fileToCopy == null || string.IsNullOrWhiteSpace(destinationDirectory))
                return null;
            if (!Directory.Exists(destinationDirectory))
                return null;

            string path = DecodeHashToPath(fileToCopy.Hash);
            var fi = new FileInfo(path);
            // compose final directory
            string destDir = destinationDirectory;
            if (destDir.Last() != Path.DirectorySeparatorChar)
                destDir += Path.DirectorySeparatorChar;
            string newPath = destDir + fi.Name;

            if (File.Exists(newPath))
                return null;

            try
            {
                // check for cut or copy
                if (cut)
                    File.Move(path, newPath);
                else
                    File.Copy(path, newPath);

                var newFileInfo = new FileInfo(newPath);
                return createFileModel(newFileInfo, EncodePathToHash(destDir));
            }
            catch
            {
                return null;
            }
        }
        public bool DeleteThumbnailFor(FileModel deleteThumbnailForFile)
        {
            if (deleteThumbnailForFile == null)
                return false;
            // get file extension and compose thumbnail path
            var fi = new FileInfo(deleteThumbnailForFile.Name);
            string thumbnailPath = _config.LocalFSThumbsDirectoryPath + Path.DirectorySeparatorChar
                                   + deleteThumbnailForFile.Hash + fi.Extension;
            if (!File.Exists(thumbnailPath))
                return false;

            try
            {
                File.Delete(thumbnailPath);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteFile(FileModel fileToDelete)
        {
            if (fileToDelete == null)
                return false;

            string path = DecodeHashToPath(fileToDelete.Hash);
            if (!File.Exists(path))
                return false;

            try
            {
                File.Delete(path);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public FileModel RenameFile(FileModel fileToChange, string newname)
        {
            if (fileToChange == null)
                return null;

            string path = DecodeHashToPath(fileToChange.Hash);
            var fi = new FileInfo(path);

            string parentDir = fi.Directory.FullName;
            if (parentDir.Last() != Path.DirectorySeparatorChar)
                parentDir += Path.DirectorySeparatorChar;
            string newPath = parentDir + newname;

            if (File.Exists(newPath))
                return null;
            try
            {
                File.Move(path, newPath);
                var newFileInfo = new FileInfo(newPath);
                return createFileModel(newFileInfo, EncodePathToHash(parentDir));
            }
            catch
            {
                return null;
            }
        }