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 } }