public IHttpActionResult UploadFile() { string userId = HttpContext.Current.User.Identity.Name; //Header에 포함되어 있는 정보 using (var db = new WebDiskDBEntities()) { if (HttpContext.Current.Request.Files.Count > 0) //파일개수가 0이상이면 { HttpFileCollection files = HttpContext.Current.Request.Files; #region 일 등록하기 for (int i = 0; i < files.Count; i++) { var newFile = new FileManage(); HttpPostedFile file = files[i]; if (file.ContentLength > 0)//업로드한 파일의 크기를 가져옴 { string folderId = HttpContext.Current.Request.Form["folderId"]; var parentFolder = db.FolderManage.Where(x => x.FolderId == folderId).SingleOrDefault(); string fileName = Path.GetFileName(file.FileName); string serverPath = @"C:\WebDisk"; string realPath = parentFolder.RealPath; string fullPath = Path.Combine(serverPath, userId, realPath); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } file.SaveAs(Path.Combine(fullPath, fileName)); newFile.FileId = GenerateUniqueID.FileID();//고유 파일 아이디 생성 newFile.FolderId = folderId; newFile.CreatedDate = DateTime.Now; newFile.LastModified = DateTime.Now; newFile.LastAccessed = DateTime.Now; newFile.Type = Enum.GetName(typeof(Manage.Enums.ContentType), Manage.Enums.ContentType.File).ToLower(); newFile.Starred = false; newFile.Trashed = false; newFile.OwnerId = userId; newFile.FileName = Path.GetFileNameWithoutExtension(file.FileName); newFile.FileExtension = Path.GetExtension(file.FileName).TrimStart('.'); //확장자 앞에 있는 '.'제거 newFile.FileSize = (file.ContentLength / 1024); //kb단위로 저장 newFile.RealPath = realPath; newFile.ServerPath = serverPath; #region 업로드된 파일의 마임 타입을 지정한다. Manage.Enums.MimeType isMimeType = Manage.Enums.MimeType.Unknown; switch (newFile.FileExtension.ToLower()) { case "jpg": case "jpeg": case "png": case "gif": case "bmp": isMimeType = Manage.Enums.MimeType.Image; break; case "doc": case "docx": case "xls": case "xlsx": case "pdf": isMimeType = Manage.Enums.MimeType.Document; break; } newFile.MimeType = (byte)isMimeType; #endregion db.FileManage.Add(newFile); db.SaveChanges(); #region 업로드된 파일의 썸네일 생성하기 string rv_FilePreview = Manage.Utils.CreatePreview.CheckFileMap(Path.Combine(fullPath, fileName), newFile.FileId); if (rv_FilePreview.Contains("오류")) { throw new Exception(rv_FilePreview + " << 오류발생했다!!!!"); //1: 원래 생겨야하는 썸네일 등의 이미지를 '파일 썸네일 생성 오류안내' 이미지로 대체한다. //2: 여기에 후속 대체 기능을 추가해야 한다. } else { } #endregion } } #endregion } } return(Ok()); }
/// <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 } }