//working for relative and absolute public static Response MoveFile(string curPath, string newPath) { var response = ResponseFormation.GetResponse(); string curDirPath = PathHelpers.GetDirFromPath(curPath); string curFilename = PathHelpers.GetFilenameFromPath(curPath); string newDirPath = PathHelpers.GetDirFromPath(newPath); string newFilename = PathHelpers.GetFilenameFromPath(newPath); var curDir = FileTree.GetDirectory(curDirPath); if (curDir == null) { response.Message = string.Format(ResponseMessages.DirectoryNotFound, curDirPath); } else { var file = FileTree.GetFile(curDir, curFilename); if (file == null) { response.Message = string.Format(ResponseMessages.FileNotFound, curFilename, curDirPath); } else { var newDir = FileTree.GetDirectory(newDirPath); if (newDir == null) { response.Message = string.Format(ResponseMessages.DirectoryNotFound, newDirPath); } else { FileTree.RemoveFile(curDir, file); FileTree.AddFile(newDir, file); file.Name = newFilename; response.IsSuccess = true; response.Message = "Successfully Moved"; FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); } } } return(response); }
//working for relative and absolute private static Response CreateFile(DirectoryNode directory, string filename, bool createLocally) { var response = ResponseFormation.GetResponse(); //query filetree to find out which server has least storage occupied var ipSpacePairs = FileTree.GetIPSpacePairs(); string ipPort = null; if (ipSpacePairs.Count == 0) { ipPort = State.LocalEndPoint.ToString(); } else { ipPort = ipSpacePairs.OrderBy(x => x.Value).First().Key; } if (createLocally || State.LocalEndPoint.ToString().Equals(ipPort)) { FileNode file = new FileNode(filename, FileTree.GetNewImplicitName(), 0); File.WriteAllText(Path.Combine(State.GetRootDirectory().FullName, file.ImplicitName), ""); file.IPAddresses.Add(State.LocalEndPoint.ToString()); FileTree.AddFile(directory, file); FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); //FileTree.AddInLocalFiles(file); response.Message = "Successfully Created"; } else { //forward response.Command = Command.forwarded; response.Message = ipPort; } response.IsSuccess = true; return(response); }