/// <summary> /// 新增群組成員 /// </summary> /// <param name="User">使用者</param> /// <param name="Database">資料庫</param> public void AddMember(User User,SharpDiskDbContext Database) { if (IsMember(User, Database)) { throw new ApiInvalidOperationException("重複成員"); } UserGroup newdata = new Models.UserGroup() { User = User, Group = this }; Database.UserGroup.Add(newdata); Database.SaveChanges(); }
/// <summary> /// 移除群組成員 /// </summary> /// <param name="User"></param> /// <param name="Database"></param> public void RemoveMember(User User,SharpDiskDbContext Database) { if (OwnerId == User.Id) throw new ApiInvalidOperationException("目標不可為擁有者"); if (!IsMember(User, Database)) throw new ApiInvalidOperationException("目標必須為成員"); UserGroup data = new UserGroup() { User = User, Group = this }; Database.UserGroup.Add(data); Database.SaveChanges(); }
/// <summary> /// 在指定的<see cref="SharpDiskDbContext"/>中移除此群組 /// </summary> /// <param name="Database">指定資料庫</param> public void Delete(SharpDiskDbContext Database) { //將本群組所有成員退出 Database.UserGroup.RemoveRange(from t in Database.UserGroup where t.GroupId == this.Id select t); //清除所有檔案節點有授權本節點存取之權利 var fileNodes = from t in Database.FileNode where t.GroupId == this.Id select t; foreach(var fileNode in fileNodes) { fileNode.Group = null;//清除群組 fileNode.GroupAuthority = FileNodeAuthority.Null;//清除群組權限 } Database.Group.Remove(this); Database.SaveChanges(); }
/// <summary> /// 在指定的<see cref="SharpDiskDbContext"/>中移除此使用者 /// </summary> /// <param name="Database">指定資料庫</param> public void Delete(SharpDiskDbContext Database) { #region DeleteGroup //退出所有加入的群組 Database.UserGroup.RemoveRange(from t in Database.UserGroup where t.UserId == this.Id select t); Database.SaveChanges(); //刪除所有擁有的群組 var ownGroup = from t in Database.Group where t.OwnerId == this.Id select t; foreach (var group in ownGroup) { group.Delete(Database); } #endregion #region 刪除擁有的檔案節點 GetRootFileNode(Database).Delete(Database); #endregion Database.User.Remove(this); Database.SaveChanges(); }
/// <summary> /// 在指定的<see cref="SharpDiskDbContext"/>中移除此檔案節點 /// </summary> /// <param name="Database">指定資料庫</param> public void Delete(SharpDiskDbContext Database) { if (this.ParentId == null) {//Root Node throw new ApiInvalidOperationException("使用者根節點不可刪除"); } if (IsFile) { //檔案節點為終點,直接刪除與檔案實體 File.Delete(Startup.FilesDirPath + this.Id); } else { //目錄節點必須要刪除子節點才可自我清除 var child = from t in Database.FileNode where t.ParentId == this.Id select t; foreach (var node in child) node.Delete(Database); } Database.FileNode.Remove(this); Database.SaveChanges(); }
/// <summary> /// 建立檔案節點 /// </summary> /// <param name="File">檔案</param> /// <param name="Database">資料庫</param> /// <returns></returns> public async Task<FileNode> CreateChildrenAsync(IFormFile File, SharpDiskDbContext Database) { FileNode newFile = new FileNode(); #region 空間檢查 if (//使用者空間不足 Owner.SpaceSize.HasValue &&//有空間限制 Owner.GetTotalUsedSpace(Database) + File.Length > Owner.SpaceSize) { throw new LackOfSpaceException($"目前使用者空間不足"); } //檢查目錄鍊容量是否足夠 var ParentChain = this.GetParentChain(Database); ParentChain.Insert(0, this); var TooSmail = ParentChain.Where(x => x.DirMaxSize.HasValue && x.Size + File.Length > x.DirMaxSize ); if (TooSmail.Count() > 0) { throw new LackOfSpaceException($"達到父系目錄限制大小:{string.Join(",", TooSmail.Select(x => x.Name))}"); } DateTime now = DateTime.Now; foreach (var node in ParentChain) { node.Size += File.Length; node.Datetime = now; } #endregion #region 儲存檔案 try { using (FileStream fileStream = System.IO.File.Create(Startup.FilesDirPath + newFile.Id)) { await File.OpenReadStream().CopyToAsync(fileStream); await fileStream.FlushAsync(); } } catch (Exception e) { throw new UnknowException(e.Message); } #endregion #region FileNode建構 newFile.IsFile = true; newFile.Owner = Owner; newFile.GroupId = this.GroupId;//上傳後的檔案權限繼承 newFile.GroupAuthority = this.GroupAuthority; newFile.OtherAuthority = this.OtherAuthority; newFile.Parent = this; newFile.Size = File.Length; newFile.ContentType = File.ContentType; newFile.Ext = File.FileName.Split('.').LastOrDefault(); newFile.Datetime = DateTime.Now; newFile.Name = File.FileName.Substring(0, File.FileName.LastIndexOf(".")); #endregion Database.FileNode.Add(newFile); Database.SaveChanges(); return newFile; }