public IHttpActionResult ChangeTrashedStatus(string folderId, [FromBody] bool trashed) { FolderManage trashedFolder = null; using (var db = new WebDiskDBEntities()) { string rootId = db.FolderManage.Where(x => x.ParentId == null).SingleOrDefault().FolderId; #region 루트 폴더는 상태 처리 예외 if (folderId != rootId) { trashedFolder = db.FolderManage.Where(x => x.FolderId == folderId).SingleOrDefault(); trashedFolder.Trashed = trashed; if (trashedFolder.Trashed == true) { trashedFolder.Starred = false; } trashedFolder.LastModified = DateTime.Now; db.SaveChanges(); } #endregion } return(Ok(trashedFolder)); }
public IHttpActionResult MoveFolder(string folderId, [FromBody] string targetFolderId) { using (var db = new WebDiskDBEntities()) { string rootId = db.FolderManage.Where(x => x.ParentId == null).SingleOrDefault().FolderId; if (folderId != rootId) { FolderManage sourceFolder = db.FolderManage.Where(x => x.FolderId == folderId).SingleOrDefault(); FolderManage targetFolder = db.FolderManage.Where(x => x.FolderId == targetFolderId).SingleOrDefault(); if (string.IsNullOrEmpty(targetFolder.OwnerId)) { targetFolder.OwnerId = sourceFolder.OwnerId; } string sourPath = Path.Combine(sourceFolder.ServerPath, sourceFolder.OwnerId, sourceFolder.RealPath); string tarPath = Path.Combine(targetFolder.ServerPath, targetFolder.OwnerId, targetFolder.RealPath, sourceFolder.FolderName); //루트로 옮길경우 여기서 에러남 if (!Directory.Exists(tarPath)) { Directory.CreateDirectory(tarPath); } MoveFolderRecursive(folderId, tarPath); Directory.Delete(sourPath); sourceFolder.RealPath = Path.Combine(targetFolder.RealPath, sourceFolder.FolderName); sourceFolder.ParentId = targetFolder.FolderId; db.SaveChanges(); } } return(Ok(new { msg = "OK" })); }
/// <summary> /// 폴더의 folderId를 Parameter로 받아 폴더 및 하위 폴더와 파일을 찾아 DB에서 삭제한다. /// 호출자는 DeleteFolderForever, DeleteFolderAutoForever 메소드 /// </summary> /// <param name="folderId">해당 폴더 아이디</param> public void DeleteFolder(string folderId) { using (var db = new WebDiskDBEntities()) { List <FolderManage> subfolders = db.FolderManage.Where(x => x.Trashed == true && x.ParentId == folderId).ToList(); List <FileManage> subFiles = db.FileManage.Where(x => x.Trashed == true && x.FolderId == folderId).ToList(); FolderManage currentFolder = db.FolderManage.Where(x => x.Trashed == true && x.FolderId == folderId).SingleOrDefault(); string rootId = db.FolderManage.Where(x => x.ParentId == null).SingleOrDefault().FolderId; if (currentFolder != null && rootId != currentFolder.FolderId) { string folderPath = Path.Combine(currentFolder.ServerPath, currentFolder.OwnerId, currentFolder.RealPath); #region 하위 파일 삭제 //하위 파일 if (subFiles.Count > 0) { for (int i = 0; i < subFiles.Count; i++) { if (subFiles[i] != null) { string fileName = subFiles[i].FileName + '.' + subFiles[i].FileExtension; string filePath = Path.Combine(subFiles[i].ServerPath, subFiles[i].OwnerId, subFiles[i].RealPath, fileName); if (File.Exists(filePath)) { File.Delete(filePath); } db.FileManage.Remove(subFiles[i]); db.SaveChanges(); } } } #endregion if (subfolders.Count > 0) { #region 하위 폴더 삭제하기 for (int i = 0; i < subfolders.Count; i++) { if (subfolders[i] != null) { //재귀함수 호출 DeleteFolder(subfolders[i].FolderId); } } #endregion } #region 현재 폴더 삭제하기 Directory.Delete(folderPath); db.FolderManage.Remove(currentFolder); db.SaveChanges(); #endregion } } }
public IHttpActionResult GetFolderTreePath() { FolderTree folderTree = new FolderTree(); using (var db = new WebDiskDBEntities()) { //루트 폴더 가져오기 FolderManage rootFolder = db.FolderManage.Where(x => x.ParentId == null).SingleOrDefault(); folderTree.FolderId = rootFolder.FolderId; folderTree.FolderName = rootFolder.FolderName; folderTree.Subfoldercnt = db.FolderManage.Where(x => x.ParentId == rootFolder.FolderId && x.Trashed == false).Count(); folderTree.Path = rootFolder.RealPath; folderTree.children = GetFolderTree(rootFolder.FolderId); //GetFolderTree 재귀 메서드 호출 } return(Ok(folderTree)); }
public IHttpActionResult MoveFileToTargetFolder(string fileId, [FromBody] string targetFolderId) { FileManage sourceFile = null; string userId = HttpContext.Current.User.Identity.Name; using (var db = new WebDiskDBEntities()) { sourceFile = db.FileManage.Where(x => x.FileId == fileId).SingleOrDefault(); if (sourceFile != null) { FolderManage targetFolder = db.FolderManage.Where(x => x.FolderId == targetFolderId).SingleOrDefault(); string fullFileName = sourceFile.FileName + "." + sourceFile.FileExtension; string commonPath = Path.Combine(sourceFile.ServerPath, sourceFile.OwnerId); string sourceFilePath = Path.Combine(commonPath, sourceFile.RealPath, fullFileName); string targetFilePath = Path.Combine(commonPath, targetFolder.RealPath, fullFileName); if (sourceFilePath != targetFilePath) { if (File.Exists(targetFilePath)) //만약 타겟 폴더에 똑같은 파일이 있으면 { File.Delete(sourceFilePath); //기존 폴더에 있는 파일은 지운다. } else { File.Move(sourceFilePath, targetFilePath); //만약에 타겟폴더에 해당 파일이 없으면 이동시킨다. } //DB에 저장하기 sourceFile.FolderId = targetFolderId; sourceFile.RealPath = targetFolder.RealPath; db.SaveChanges(); } } else { return(NotFound()); } } return(Ok(sourceFile)); }
/// <summary> /// 폴더의 path를 생성하는 재귀메소드 /// </summary> /// <param name="folderId"></param> /// <param name="routes"></param> /// <returns></returns> public List <FolderInfo> getPath(string folderId, List <FolderInfo> routes = null) { FolderManage currentFolder = null; FolderManage parentFolder = null; List <FolderInfo> routeList; if (routes != null) { routeList = routes.ToList(); } else { routeList = new List <FolderInfo>(); } using (var db = new WebDiskDBEntities()) { currentFolder = db.FolderManage.Where(x => x.FolderId == folderId).SingleOrDefault(); if (routeList.Count == 0) { routeList.Add(new FolderInfo { folderId = currentFolder.FolderId, folderName = currentFolder.FolderName }); } parentFolder = db.FolderManage.Where(x => x.FolderId == currentFolder.ParentId).SingleOrDefault(); if (parentFolder == null) { return(routeList); } routeList.Add(new FolderInfo { folderId = parentFolder.FolderId, folderName = parentFolder.FolderName }); }; return(getPath(parentFolder.FolderId, routeList)); }
public IHttpActionResult RenameFolder(string folderId, [FromBody] string newFolderName) { FolderManage renamedfolder = null; string userId = HttpContext.Current.User.Identity.Name; using (var db = new WebDiskDBEntities()) { renamedfolder = db.FolderManage.Where(x => x.FolderId == folderId && x.OwnerId == userId).SingleOrDefault(); string serverPath = renamedfolder.ServerPath; //서버 path string realPath = renamedfolder.RealPath; // 원본 폴더의 realPath string parentPath = realPath.Substring(0, realPath.LastIndexOf('\\')); //부모 폴더의 real path string tarFolderPath = Path.Combine(parentPath, newFolderName); //타겟 폴더의 real path string souFolderFullPath = Path.Combine(serverPath, realPath); //원본 폴더의 전체 path string tarFolderFullPath = Path.Combine(serverPath, tarFolderPath); //타겟 폴더의 전체 path if (renamedfolder != null) { if (!Directory.Exists(tarFolderFullPath)) { Directory.Move(souFolderFullPath, tarFolderFullPath); //RenameFolderRecursive renamedfolder.FolderName = newFolderName; renamedfolder.RealPath = tarFolderPath; renamedfolder.LastModified = DateTime.Now; db.SaveChanges(); } else { return(Ok(new { msg = "같은 이름의 폴더가 존재합니다." })); } } else { return(NotFound()); } } return(Ok(renamedfolder)); }
/// <summary> /// 해당 폴더의 하위 폴더와 파일 path얻기 /// </summary> /// <param name="folderId">해당 폴더 아이디</param> /// <param name="fileList">파일 path가 담겨져 있는 string 리스트</param> /// <returns></returns> public List <string> GetFileList(string folderId, List <string> fileList) { using (var db = new WebDiskDBEntities()) { FolderManage folder = db.FolderManage.Where(x => x.FolderId == folderId && x.OwnerId == HttpContext.Current.User.Identity.Name).SingleOrDefault(); string sourcePath = Path.Combine(folder.ServerPath, folder.OwnerId, folder.RealPath); string folderName = folder.FolderName; #region 하위 파일 path 추가 List <FileManage> subFiles = db.FileManage.Where(x => x.FolderId == folderId && x.OwnerId == HttpContext.Current.User.Identity.Name).ToList(); foreach (var subFile in subFiles) { string subFileName = subFile.FileName + "." + subFile.FileExtension; string subFilePath = Path.Combine(subFile.ServerPath, subFile.OwnerId, subFile.RealPath, subFileName); fileList.Add(subFilePath); } #endregion #region 하위 폴더 path 추가 List <FolderManage> subFolders = db.FolderManage.Where(x => x.ParentId == folderId).ToList(); foreach (var subFolder in subFolders) { GetFileList(subFolder.FolderId, fileList); } #endregion //if (!Directory.Exists(sourcePath)) //{ // response.StatusCode = HttpStatusCode.NotFound; // response.ReasonPhrase = string.Format("Folder not found: {0} .", folderName); // throw new HttpResponseException(response); //} } return(fileList); }
public IHttpActionResult GetDisk(string folderId = null) { string currentUserId = HttpContext.Current.User.Identity.Name; Disk disk = new Disk(); Folder folder = new Folder(); List <FolderManage> subFolderList = null; List <FileManage> subFileList = null; using (var db = new WebDiskDBEntities()) { if (folderId == null) { subFolderList = db.FolderManage.Where(x => x.OwnerId == currentUserId && x.ParentId == null && x.Trashed == false).OrderByDescending(o => o.CreatedDate).ToList(); subFileList = db.FileManage.Where(x => x.OwnerId == currentUserId && x.FolderId == null && x.Trashed == false).OrderByDescending(o => o.CreatedDate).ToList(); } else { FolderManage currentFolder = db.FolderManage.Where(x => x.OwnerId == currentUserId && x.FolderId == folderId && x.Trashed == false).SingleOrDefault(); if (currentFolder != null) { subFolderList = db.FolderManage.Where(x => x.OwnerId == currentUserId && x.ParentId == folderId && x.Trashed == false).OrderByDescending(o => o.CreatedDate).ToList(); subFileList = db.FileManage.Where(x => x.OwnerId == currentUserId && x.FolderId == folderId && x.Trashed == false).OrderByDescending(o => o.CreatedDate).ToList(); } else { return(NotFound()); } } folder.subFolders = subFolderList; folder.subFiles = subFileList; disk.Folder = folder; disk.FolderId = folderId; return(Ok(disk)); } }
public IHttpActionResult CreateFolder(FolderManage folder) { FolderManage newFolder = new FolderManage(); string userId = HttpContext.Current.User.Identity.Name; using (var db = new WebDiskDBEntities()) { string folderName = folder.FolderName; string parentId = folder.ParentId; FolderManage parentFolder = db.FolderManage.Where(x => x.FolderId == parentId).SingleOrDefault(); string serverPath = @"C:\WebDisk"; string parentRealPath = parentFolder.RealPath; string folderFullpath = Path.Combine(serverPath, "8d7g9s", parentRealPath); if (!Directory.Exists(folderFullpath)) { Directory.CreateDirectory(folderFullpath); } #region 새로운 폴더 생성 newFolder.FolderId = GenerateUniqueID.FolderID(); //고유 폴더 아이디 생성 newFolder.ParentId = parentId; newFolder.Type = Enum.GetName(typeof(Manage.Enums.ContentType), Manage.Enums.ContentType.Folder).ToLower(); newFolder.ServerPath = serverPath; newFolder.RealPath = Path.Combine(parentRealPath, folderName); newFolder.CreatedDate = DateTime.Now; newFolder.LastAccessed = DateTime.Now; newFolder.LastModified = DateTime.Now; newFolder.FolderName = folderName; newFolder.Starred = false; newFolder.Trashed = false; newFolder.OwnerId = "8d7g9s"; #endregion db.FolderManage.Add(newFolder); db.SaveChanges(); } return(Ok(newFolder)); }
/// <summary> /// 하위 폴더와 파일까지 복사하는 재귀 메소드 /// 호출자는 CopyFolder /// </summary> /// <param name="folderId">해당 폴더의 아이디</param> /// <param name="targetFolderId">타겟 폴더 아이디</param> public void CopyFolderRecursive(string folderId, string targetFolderId) { using (var db = new WebDiskDBEntities()) { FolderManage sourceFolder = db.FolderManage.Where(x => x.FolderId == folderId).SingleOrDefault(); FolderManage targetFolder = db.FolderManage.Where(x => x.FolderId == targetFolderId).SingleOrDefault(); if (string.IsNullOrEmpty(targetFolder.OwnerId)) { targetFolder.OwnerId = sourceFolder.OwnerId; } string sourPath = Path.Combine(sourceFolder.ServerPath, sourceFolder.OwnerId, sourceFolder.RealPath); string tarPath = Path.Combine(targetFolder.ServerPath, targetFolder.OwnerId, targetFolder.RealPath, sourceFolder.FolderName); if (!Directory.Exists(tarPath)) { Directory.CreateDirectory(tarPath); } FolderManage copiedFolder = new FolderManage() { FolderId = GenerateUniqueID.FolderID(), FolderName = sourceFolder.FolderName, Type = Enum.GetName(typeof(Manage.Enums.ContentType), Manage.Enums.ContentType.Folder).ToLower(), CreatedDate = DateTime.Now, LastModified = DateTime.Now, LastAccessed = DateTime.Now, OwnerId = sourceFolder.OwnerId, ParentId = targetFolder.FolderId, Starred = false, Trashed = false, ServerPath = sourceFolder.ServerPath, RealPath = Path.Combine(targetFolder.RealPath, sourceFolder.FolderName) }; db.FolderManage.Add(copiedFolder); db.SaveChanges(); #region 하위 파일 복사하기 List <FileManage> subFiles = db.FileManage.Where(x => x.FolderId == folderId).ToList(); foreach (var subFile in subFiles) { string fileName = subFile.FileName + '.' + subFile.FileExtension; string filePath = Path.Combine(subFile.ServerPath, subFile.OwnerId, subFile.RealPath, fileName); string tarFilePath = Path.Combine(tarPath, fileName); if (!File.Exists(tarFilePath)) { File.Copy(filePath, tarFilePath); } FileManage copiedFile = new FileManage() { FileId = GenerateUniqueID.FileID(), FileName = subFile.FileName, Type = Enum.GetName(typeof(Manage.Enums.ContentType), Manage.Enums.ContentType.File).ToLower(), FileExtension = subFile.FileExtension, FileSize = subFile.FileSize, CreatedDate = DateTime.Now, LastModified = DateTime.Now, LastAccessed = DateTime.Now, OwnerId = subFile.OwnerId, Starred = false, Trashed = false, FolderId = copiedFolder.FolderId, RealPath = copiedFolder.RealPath, ServerPath = subFile.ServerPath }; db.FileManage.Add(copiedFile); db.SaveChanges(); } #endregion #region 하위 폴더 복사하기 List <FolderManage> subFolders = db.FolderManage.Where(x => x.ParentId == folderId).ToList(); foreach (var subFolder in subFolders) { CopyFolderRecursive(subFolder.FolderId, copiedFolder.FolderId); } #endregion } }
//新增插入数据 private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.Label == null) { return; } var newValue = e.Label; if (string.IsNullOrEmpty(newValue)) { newValue = e.Node.Text; } var item = e.Node.Tag as FolderManage; var restInfo1 = e.Node.Tag as RestInfo; if (isAddApi) { try { if (e.Node.Parent.Tag is FolderManage pFolder) { var restInfo = new RestInfo { Name = newValue, FolderId = pFolder.ID }; DbContext.DbClient.Insertable(restInfo).ExecuteCommand(); } } catch (Exception) { isAddApi = false; } isAddApi = false; } else { if (_isReName) { if (item != null) { item.FolderName = newValue; DbContext.DbClient.Updateable(item).ExecuteCommand(); } if (restInfo1 != null) { restInfo1.Name = newValue; DbContext.DbClient.Updateable(restInfo1).ExecuteCommand(); } _isReName = false; } else { if (item != null) { var folder = new FolderManage { FolderName = newValue, PID = item.ID, GroupId = item.GroupId }; DbContext.DbClient.Insertable(folder).ExecuteCommand(); } } } treeView1.LabelEdit = false; BindTree(); }