/// <summary> /// Downloads prepare. /// 客户端提交需要更新的文件Id,以及本地版本号 /// 服务端判断是否需要更新、删除 /// 更新则直接找到最新版本,通知客户端完全下载 /// </summary> /// <returns></returns> public JsonResult DownloadPrepare(DownloadPrepareModel model) { OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(model.oauth_token); NetDiskUser user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.UserId == pair.UserId); DownloadPrepareViewModel viewModel = new DownloadPrepareViewModel(); if (user == null) { viewModel.ret = -1; viewModel.msg = "cannot find the user specified!"; } else { viewModel.ret = 0; File file = db.Files.SingleOrDefault(f => f.Id == model.Id); if (file == null) { viewModel.IsExits = false; } else { File lastVersion = db.Files.Where(f => f.FilePath == file.FilePath && f.FileName == file.FileName).OrderByDescending(f => f.Id).FirstOrDefault(); viewModel.IsExits = true; viewModel.IsDeleted = lastVersion.IsDeleted; viewModel.LastId = lastVersion.Id; viewModel.LastDFSPath = lastVersion.DFSPath; viewModel.LastReversion = lastVersion.Reversion; viewModel.LastHash = lastVersion.Hash; } } return(Json(viewModel, JsonRequestBehavior.AllowGet)); }
public ActionResult Authorize(Models.Authorize2Model model) { var user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.Email == model.Email && u.Password == model.Password); if (user == null) { ModelState.AddModelError("", "密码不正确"); } else //密码正确,把临时token换成正式的token,并存入缓存,与当前用户相关 { string token = "test_token_ok"; string tokenSecret = "test_token_secret_ok"; OauthTokenPair tokenpair = new OauthTokenPair(); tokenpair.oauth_token = token; tokenpair.oauth_token_secret = tokenSecret; tokenpair.UserId = user.UserId; TempToken2OfficialMap map = new TempToken2OfficialMap { oauth_temp_token = model.TempToken, oauth_token = token }; unitOfWork.TokenReposity.Insert(tokenpair); unitOfWork.TokenReposity.InsertMap(map); return(RedirectToAction("AuthorizeCompleted", new{ verifyCode = 123 })); } //验证数据,并跳转到AuthorizeCompleted return(View(model)); }
//(1)客户端将上次同步版本号发送给RPC //(2)RPC查询出数据库中该用户需要同步的目录下所有版本大于客户端上次同步版本号的文件与文件夹。 //(3)将查询出的文件与文件夹按照版本号升序排列,如果文件夹和文件版本号相同,文件夹要在文件之前。如果文件和文件或者文件夹和文件夹版本相同按照ID大小排序。 //(4)将这些数据转化成Json格式返回给客户端。 /// <summary> /// 获取更新列表 /// </summary> /// <returns></returns> public JsonResult GetUpdateList(GetUpdateListModel model) { OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(model.oauth_token); NetDiskUser user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.UserId == pair.UserId); GetUpdateListViewModel viewModel = new GetUpdateListViewModel(); if (user == null) { viewModel.ret = -1; viewModel.msg = "cannot find the user specified!"; } else { List <File> fileList = db.Files.Where(f => f.Id > model.lastSyncId && f.Owner.UserId == user.UserId).ToList(); fileList.AddRange(db.Files.Where(f => f.Id == model.lastSyncId && f.IsDeleted && f.Owner.UserId == user.UserId)); fileList = fileList.OrderBy(f => f.Id).ToList(); //id从小到大,顺序更新 viewModel.Files = new List <FileLiteModel>(); foreach (var item in fileList) { FileLiteModel lite = new FileLiteModel { Hash = item.Hash, Id = item.Id, FullPath = item.FilePath + item.FileName }; viewModel.Files.Add(lite); } viewModel.ret = 0; } return(Json(viewModel, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Uploads prepare. /// 此时会把文件记录到临时表,DFS为空,reversion从file表中获取,该文件ID不代表在File中的ID /// </summary> /// <returns></returns> public JsonResult UploadPrepare(UploadPrepareModel model) { //FileName OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(model.oauth_token); NetDiskUser user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.UserId == pair.UserId); db.NetdiskUsers.Attach(user); FileUncomplete fileUncomplete = new FileUncomplete { FileName = model.FileName.GetFileName(), FilePath = model.FileName.GetFilePath(), FileSize = model.FileSize, Hash = model.Hash, IsCompleted = false, Owner = user }; UploadPrepareViewModel viewmodel = new UploadPrepareViewModel(); //以该hash的的相同路径的最新版本作为当前版本 File thisVersion = db.Files.Where(f => f.FileName == fileUncomplete.FileName && f.FilePath == fileUncomplete.FilePath && f.Hash == model.Hash && !f.IsDeleted).OrderByDescending(f => f.Id).FirstOrDefault(); if (thisVersion != null) { viewmodel.IsExitsRemote = true; } else { viewmodel.IsExitsRemote = false; } if (viewmodel.IsExitsRemote) { viewmodel.RemoteId = thisVersion.Id; viewmodel.RemoteHash = thisVersion.Hash; viewmodel.RemoteReversion = thisVersion.Reversion; } else { try { db.FileUncomplete.Add(fileUncomplete); db.SaveChanges(); viewmodel.ret = 0; viewmodel.Id = fileUncomplete.Id; viewmodel.hash = fileUncomplete.Hash; viewmodel.DFSPath = "\\" + fileUncomplete.Hash; } catch (System.Exception ex) { viewmodel.ret = -1; viewmodel.msg = "save file to FileUncomplete failed!err info:" + ex.Message; } } return(Json(viewmodel, JsonRequestBehavior.AllowGet)); }
public static int CheckToken(string oauth_token) { OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(oauth_token); if (pair != null && oauth_token == pair.oauth_token) { return(pair.UserId); } return(-1); }
/// <summary> /// Accesses the token. /// </summary> /// <returns></returns> public JsonResult AccessToken(Models.AccessTokenModel model) { TempToken2OfficialMap map = unitOfWork.TokenReposity.AccessToken(model.oauth_token); if (map != null) { OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(map.oauth_token); return(Json(pair, JsonRequestBehavior.AllowGet)); } return(Json(new{ err = "err" }, JsonRequestBehavior.AllowGet)); }
//(1)删除文件类似于一个特殊的文件修改操作。Client只向RPC服务器发送一次请求,包括要删除的文件ID。 //(2)RPC接收该请求后,先检查数据块,如果文件不存在或已删除,则直接取消操作。 //(3)如果文件存在且未删除且和本地版本相同,新增删除版本号,并通知其他Client同步删除该文件。 //(4)删除文件要求只能删除版本小于本地文件的版本,也就是说如果服务器文件版本大于本地文件版本,删除文件操作将取消。 public JsonResult DeleteFile(RPCDeleteModel model) { OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(model.oauth_token); NetDiskUser user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.UserId == pair.UserId); RPCDeleteViewModel viewModel = new RPCDeleteViewModel(); if (user == null) { viewModel.ret = -1; viewModel.msg = "cannot find the user specified!"; } else { File fileSpecified = db.Files.Where(f => f.Id == model.Id).SingleOrDefault(); if (fileSpecified == null) { viewModel.ret = 0; viewModel.msg = "指定版本不存在,直接删除"; viewModel.IsConficted = true; //让本地重命名 } else { File last = db.Files.Where(f => f.FilePath == fileSpecified.FilePath && f.FileName == fileSpecified.FileName).OrderByDescending(f => f.Id).FirstOrDefault(); if (fileSpecified.Reversion == last.Reversion && !last.IsDeleted) { last.IsDeleted = true; db.SaveChanges(); viewModel.IsConficted = false; } else { viewModel.IsConficted = true; } } } return(Json(viewModel, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Uploads complete. /// 把临时记录删除,并记录到File表 /// 如果有冲突,即该版本号已经存在,则返回冲突消息 /// 同时发布更新消息(更新消息发布到以该用户UserId为channelId的channel上),所有以该帐号登录的客户端都要订阅该Channel /// 客户端收到该请求的返回之后应该更新本地的文件ID与版本号 /// </summary> /// <returns></returns> public JsonResult UploadComplete(UploadCompleteModel model) { OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(model.oauth_token); NetDiskUser user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.UserId == pair.UserId); db.NetdiskUsers.Attach(user); FileUncomplete fileUncomplete = db.FileUncomplete.SingleOrDefault(f => f.Id == model.Id); uploadCompleteViewModel viewModel = new uploadCompleteViewModel(); if (fileUncomplete == null) { viewModel.ret = -1; viewModel.msg = "unComplete recorde is miss"; } File file = new File { CreateTime = DateTime.Now, DFSPath = '\\' + fileUncomplete.Hash, Hash = fileUncomplete.Hash, FileName = fileUncomplete.FileName, FilePath = fileUncomplete.FilePath, FileSize = fileUncomplete.FileSize, FileType = 1, IsDeleted = false, LastModifyTime = DateTime.Now, Owner = user, Reversion = 0 }; if (db.Files.Count(a => a.FilePath == file.FilePath && a.FileName == file.FileName && !a.IsDeleted) > 0) { //已经有文件,返回冲突消息 file.FileName = file.FileName + "_conflict_copy"; viewModel.Conflicted = true; viewModel.NewFilename = file.FilePath + file.FileName; } else { viewModel.Conflicted = false; try { File last = db.Files.Where(f => f.FilePath == file.FilePath && f.FileName == file.FileName).OrderByDescending(f => f.Reversion).FirstOrDefault(); if (last != null) { file.Reversion = last.Reversion + 1; } else { file.Reversion = 0; } db.Files.Add(file); db.SaveChanges(); viewModel.Id = file.Id; viewModel.ret = 0; viewModel.NewFilename = file.FilePath + file.FileName; viewModel.Reversion = file.Reversion; db.FileUncomplete.Remove(fileUncomplete); db.SaveChanges(); } catch (System.Exception ex) { viewModel.ret = -1; viewModel.msg = "save db failed,err info:" + ex.Message; } } return(Json(viewModel, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 记录修改数据到fileuncomplete表 /// 返回文件id等,客户端收到之后应该完整上传 /// </summary> /// <returns></returns> public JsonResult ModifyPrepare(ModifyPrepareModel model) { //根据客户端提供的路径查找最新版,看版本Id是否过期,如果过期则返回过期,客户端提示版本冲突,创建新文件重新提交 OauthTokenPair pair = unitOfWork.TokenReposity.GetTokenPair(model.oauth_token); NetDiskUser user = unitOfWork.UserRepositiry.dbSet.SingleOrDefault(u => u.UserId == pair.UserId); ModifyPrepareViewModel viewModel = new ModifyPrepareViewModel(); if (user == null) { viewModel.ret = -1; viewModel.msg = "cannot find the user in cache, relogin may help"; return(Json(viewModel, JsonRequestBehavior.AllowGet)); } db.NetdiskUsers.Attach(user); FileUncomplete fileUncomplete = new FileUncomplete { FileName = model.LocalPath.GetFileName(), FilePath = model.LocalPath.GetFilePath(), FileSize = model.NewFileSize, Hash = model.NewHash, IsCompleted = false, Owner = user }; File lastversion = db.Files.Where(f => f.FileName == fileUncomplete.FileName && f.FilePath == fileUncomplete.FilePath).OrderByDescending(f => f.Id).FirstOrDefault(); if (lastversion == null) { viewModel.ret = -1; viewModel.msg = "can not find the file specified"; return(Json(viewModel, JsonRequestBehavior.AllowGet)); } else { if (model.LocalId < lastversion.Id) //过期 { viewModel.IsOutOfData = true; } else if (model.LocalId == lastversion.Id && lastversion.IsDeleted) //过期,最后一个版本已经被删除 { viewModel.IsOutOfData = true; viewModel.IsDeleted = true; } else { viewModel.IsOutOfData = false; viewModel.IsDeleted = false; try { fileUncomplete.Reversion = lastversion.Reversion + 1; db.FileUncomplete.Add(fileUncomplete); db.SaveChanges(); viewModel.ret = 0; viewModel.Id = fileUncomplete.Id; viewModel.DFSPath = "\\" + fileUncomplete.Hash; } catch (System.Exception ex) { viewModel.ret = -1; viewModel.msg = "save file to FileUncomplete failed!err info:" + ex.Message; } } } return(Json(viewModel, JsonRequestBehavior.AllowGet)); }
public bool Delete(OauthTokenPair tokenPair) { return(this.context.Remove(tokenPair.oauth_token)); }
public bool Insert(OauthTokenPair tokenPair) { this.context.Remove(tokenPair.oauth_token); return(this.context.Store(StoreMode.Add, tokenPair.oauth_token, tokenPair)); }