写在前面
本篇文章将新建文件夹的逻辑也进行一下修改。
系列文章
[EF]vs15+ef6+mysql code first方式
[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册
[实战]MVC5+EF6+MySql企业网盘实战(3)——验证码
[实战]MVC5+EF6+MySql企业网盘实战(4)——上传头像
[实战]MVC5+EF6+MySql企业网盘实战(5)——登录界面,头像等比例压缩
[实战]MVC5+EF6+MySql企业网盘实战(5)——页面模板
[实战]MVC5+EF6+MySql企业网盘实战(5)——ajax方式注册
[实战]MVC5+EF6+MySql企业网盘实战(6)——ajax方式登录
[实战]MVC5+EF6+MySql企业网盘实战(7)——文件上传
[实战]MVC5+EF6+MySql企业网盘实战(8)——文件下载、删除
[实战]MVC5+EF6+MySql企业网盘实战(9)——编辑文件名
[实战]MVC5+EF6+MySql企业网盘实战(10)——新建文件夹
[实战]MVC5+EF6+MySql企业网盘实战(11)——新建文件夹2
[实战]MVC5+EF6+MySql企业网盘实战(12)——新建文件夹和上传文件
[实战]MVC5+EF6+MySql企业网盘实战(13)——编辑文件夹
[实战]MVC5+EF6+MySql企业网盘实战(14)——逻辑重构
[实战]MVC5+EF6+MySql企业网盘实战(15)——逻辑重构2
[实战]MVC5+EF6+MySql企业网盘实战(16)——逻辑重构3
新建文件夹
前面对网盘的逻辑进行了一下重构,发现越来越简单了。
public JsonResult CreateFolder() { UserInfo userInfo = Session["user"] as UserInfo; if (userInfo == null) { RedirectToAction("Login", "UserInfo"); } string folderPath = Server.UrlDecode(Request.Params["folderPath"]); string folderName = Request.Params["folderName"]; if (string.IsNullOrEmpty(folderName)) { throw new ArgumentNullException("文件夹名称不能为空"); } //检查文件夹是否已经存在 Expression<Func<MyFile, bool>> where = null; if (string.IsNullOrEmpty(folderPath)) { where = x => x.User.Id == userInfo.Id && x.IsFolder == 1 && x.IsDelete == false && x.FolderPath == "/NetDisk/"; } else { where = x => x.User.Id == userInfo.Id && x.IsFolder == 1 && x.IsDelete == false && x.FolderPath == folderPath; } var count = _myFileServiceRepository.FindAll(where).Count(); userInfo = _userInfoServiceRepository.Find(x => x.Id == userInfo.Id); if (count > 0) { //如果不存在,则新建,否则进行自动重命名 folderName = folderName + "(" + (count + 1).ToString() + ")"; } if (string.IsNullOrEmpty(folderPath)) { folderPath = "/NetDisk/"; } MyFile folder = new MyFile() { FolderPath = folderPath.TrimEnd('/') + "/", FileName = folderName, CreateDt = DateTime.Now, User = userInfo, FileExt = string.Empty, FileIcon = "/Content/Images/FolderType.png", FileMd5 = string.Empty, FileSize = 0, IsDelete = false, ModifyDt = DateTime.Now, IsFolder = 1 }; try { _myFileServiceRepository.Add(folder); _myFileServiceRepository.SaveChanges(); } catch (Exception) { return new JsonResult() { Data = new JavaScriptSerializer().Serialize(new { code = 500, msg = "创建失败" }) }; } return new JsonResult() { Data = new JavaScriptSerializer().Serialize(new { code = 200, folder = folder }) }; }
上传文件的地方需要修改一下,对文件的路径中存在中文会出现乱码的情况,做一下修改。
[HttpPost] public void UploadFile(string filePath) { UserInfo userInfo = Session["user"] as UserInfo; //保证路径格式一直以/结束 filePath = filePath.TrimEnd('/') + "/"; if (userInfo == null) { RedirectToAction("Login", "UserInfo"); } filePath = Server.UrlDecode(filePath == "/" ? "/NetDisk/" : filePath); var files = Request.Files; MyFile myFile = null; if (files.Count > 0) { var file = files[0]; string fileName = file.FileName; Stream inputStream = file.InputStream; string fileSaveFolder = Server.MapPath("~/NetDisk"); //如果目标不存在,则创建 if (!Directory.Exists(fileSaveFolder)) { Directory.CreateDirectory(fileSaveFolder); } byte[] buffer = new byte[inputStream.Length]; //判断是否已经超出个人网盘大小 var myFiles = _myFileServiceRepository.FindAll(x => x.User.Id == userInfo.Id && x.IsDelete == false); int myDiskSize = 0; if (myFiles.Count() > 0) { myDiskSize = myFiles.Sum(x => x.FileSize); } //如果已经超出网盘大小,则给出提示 if (myDiskSize + buffer.Length > userInfo.NetDiskSize) { AlertMsg("对不起,您的网盘空间不足,请清理后再次上传,或联系管理员进行扩容。", ""); return; } inputStream.Read(buffer, 0, buffer.Length); string strFileMd5 = MD5Helper.GetMD5FromFile(buffer); string fileSavePath = Path.Combine(fileSaveFolder, filePath); //名称格式一律以md5命名。 string strNewName = strFileMd5 + Path.GetExtension(file.FileName); fileSavePath = Path.Combine(fileSaveFolder, strNewName); //如果文件不存在,则保存物理文件。 if (!System.IO.File.Exists(fileSavePath)) { file.SaveAs(fileSavePath); } //判断在数据库中同一路径下,md5相同,名称相同的文件是否存在,不存在则直接插入,否则对文件进行自动重命名保存。 var fileIsExist = _myFileServiceRepository.Find(x => x.FileMd5 == strFileMd5 && x.User.Id == userInfo.Id && x.FileName == fileName); //如果找到的文件对象不为空,并且是删除状态,则修改删除状态,不再新增文件信息,否则添加。 //查询导航属性 var currentUser = _userInfoServiceRepository.Find(x => x.Id == userInfo.Id); if (fileIsExist != null) { myFile = fileIsExist; //文件被逻辑删除了,则更新状态,否则重命名后新增文件。 if (fileIsExist.IsDelete) { fileIsExist.IsDelete = false; _myFileServiceRepository.Update(fileIsExist); } else { //查找该用户下类似文件名的文件并且没有被逻辑删除的个数 var count = _myFileServiceRepository.FindAll(x => x.User.Id == userInfo.Id && x.IsDelete == false && x.FileName.Contains(fileName)).Count(); fileName = count > 0 ? Path.GetFileNameWithoutExtension(fileName) + "(" + (count + 1).ToString() + ")" + Path.GetExtension(fileName) : fileName; myFile = new MyFile() { FileMd5 = strFileMd5, ModifyDt = DateTime.Now, IsDelete = false, FileSize = buffer.Length, FolderPath = filePath, FileExt = Path.GetExtension(fileSavePath), CreateDt = DateTime.Now, FileName = fileName, FileIcon = GetFileIcon(Path.GetExtension(fileSavePath)), User = currentUser, IsFolder = 0 }; //保存数据库 _myFileServiceRepository.Add(myFile); } } else { //新增文件信息 myFile = new MyFile() { FileMd5 = strFileMd5, ModifyDt = DateTime.Now, IsDelete = false, FileSize = buffer.Length, FolderPath = filePath, FileExt = Path.GetExtension(fileSavePath), CreateDt = DateTime.Now, FileName = fileName, FileIcon = GetFileIcon(Path.GetExtension(fileSavePath)), User = currentUser, IsFolder = 0 }; //保存数据库 _myFileServiceRepository.Add(myFile); } _myFileServiceRepository.SaveChanges(); string json = new JavaScriptSerializer().Serialize(myFile); AlertMsg("上传成功", json); } }
总结
今天练完车在路上好好想了一下,发现按照这个思路走下去,网盘功能实现起来更简单一些。