public ActionResult <string> Register([FromBody] RegisterRequestModel registerRequestModel) { if (_databaseService.Users.FirstOrDefault(t => t.Username == registerRequestModel.Username) != null) { throw new UsernameDuplicatedException("Username duplicated."); } // 创建用户 User user = new User(); user.Username = registerRequestModel.Username; user.Password = BCrypt.Net.BCrypt.HashPassword(registerRequestModel.Password); user.Nickname = registerRequestModel.Nickname; user.Status = 0; _databaseService.Users.Add(user); _databaseService.SaveChanges(); // 创建用户组信息 GroupToUser groupToUser = new GroupToUser() { GroupId = Group.GroupID.DEFAULT, UserId = user.Id }; _databaseService.GroupsToUsersRelation.Add(groupToUser); // 创建用户目录 var groupDirectory = EntityFile.CreateDirectoryRecord(user.Username, "/users", $"/users/{user.Username}", user); _databaseService.Files.Add(groupDirectory); _databaseService.SaveChanges(); return(Ok(new RegisterResultModel(user))); }
private IActionResult RequestUploadFolder(FileUploadRequestModel requestModel, User loginUser) { // 建立文件夹 string folder = requestModel.Path; List <string> folderPath = new List <string>(); folderPath.Add(folder); while (folder != null && folder != "/" && folder != "\\") { var s = Path.GetDirectoryName(folder); folderPath.Add(folder = s); } for (int i = folderPath.Count - 1; i > 0; i--) { // a/b var s = folderPath[i].Replace("\\", "/"); // a/b/c var n = folderPath[i - 1].Replace("\\", "/"); var t = _databaseService.Files.FirstOrDefault(t => t.Path == n && t.Status == EntityFile.FileStatus.Confirmed ); if (t == null) { var tmp = EntityFile.CreateDirectoryRecord(Path.GetFileName(n), s, n, loginUser); _databaseService.Files.Add(tmp); } else if (t.Type != "text/directory") { throw new FileNameDuplicatedException(); } } _databaseService.SaveChanges(); var ret = _databaseService.Files.FirstOrDefault(t => t.Type == "text/directory" && t.Path == requestModel.Path ); if (ret == null) { throw new UnexpectedException(); } SetApiResultStatus(ApiResultStatus.StorageUploadSkip); return(Ok(new FileUploadRequestResultModel(ret, null, _tencentCosManagement))); }
public IActionResult ConfirmUpload([FromBody] FileUploadConfirmRequestModel requestModel) { if (!(HttpContext.Items["actor"] is User loginUser)) { throw new UnexpectedException(); } requestModel.Guid = requestModel.Guid.ToLower(); // 检查被确认文件是否存在0 var file = _databaseService.Files.FirstOrDefault(s => s.Id == requestModel.Id && s.Guid == requestModel.Guid && s.Status == Models.Entities.File.FileStatus.Pending && s.User == loginUser); if (file == null) { throw new ConfirmingFileNotFoundException(); } // 检查被确认的文件是否正常 var efile = _databaseService.Files.FirstOrDefault(s => s.Id != file.Id && // 不是同一个文件 s.Path == file.Path && // 但是是同一个路径 s.Status == EntityFile.FileStatus.Confirmed // 并且是已经确认上传的内容 ); if (efile != null) { throw new ConfirmingFileNotFoundException(); } // 确认上传 file.Status = Models.Entities.File.FileStatus.Confirmed; // 建立文件夹 string folder = file.Folder; List <string> folderPath = new List <string>(); folderPath.Add(folder); while (folder != null && folder != "/" && folder != "\\") { var s = Path.GetDirectoryName(folder); folderPath.Add(folder = s); } for (int i = folderPath.Count - 1; i > 0; i--) { // a/b var s = folderPath[i].Replace("\\", "/"); // a/b/c var n = folderPath[i - 1].Replace("\\", "/"); var t = _databaseService.Files.FirstOrDefault(t => t.Path == n && t.Status == EntityFile.FileStatus.Confirmed ); if (t == null) { var tmp = EntityFile.CreateDirectoryRecord(Path.GetFileName(n), s, n, loginUser); _databaseService.Files.Add(tmp); } else if (t.Type != "text/directory") { throw new FileNameDuplicatedException(); } } // TODO 确认保存后删除无用记录 _databaseService.SaveChanges(); var fileRepositoryRecord = new FileRepository() { Guid = file.Guid, Md5 = file.Md5, Size = file.Size, User = loginUser, }; _databaseService.FileRepository.Add(fileRepositoryRecord); _databaseService.SaveChanges(); return(Ok(file.ToVo())); }
public IActionResult AddGroup([FromBody] GroupCreateRequestModel addGroupRequestModel) { //use groupname to identify group,because the id is invisible to user if (_databaseService.Groups.FirstOrDefault(t => t.GroupName == addGroupRequestModel.GroupName) != null) { throw new GroupnameDuplicatedException("Groupname duplicated."); } //initialize new group and save it to database Group group = new Group(); group.GroupName = addGroupRequestModel.GroupName; _databaseService.Groups.Add(group); //obtain the user var user = HttpContext.Items["actor"] as User; //initialize grouptouser and save it to database GroupToUser groupToUser = new GroupToUser(); groupToUser.Group = group; groupToUser.GroupId = group.Id; groupToUser.User = user; groupToUser.UserId = user.Id; _databaseService.GroupsToUsersRelation.Add(groupToUser); // initial group permission to the new group _databaseService.UserToPermissionRelation.Add(new UserToPermission() { User = user, UserId = user.Id, Permission = PermissionBank.GroupOperationPermission(group.GroupName, "member", "add") }); _databaseService.UserToPermissionRelation.Add(new UserToPermission() { User = user, UserId = user.Id, Permission = PermissionBank.GroupOperationPermission(group.GroupName, "member", "remove") }); _databaseService.UserToPermissionRelation.Add(new UserToPermission() { User = user, UserId = user.Id, Permission = PermissionBank.GroupOperationPermission(group.GroupName, "", "delete") }); _databaseService.GroupsToPermissionsRelation.Add(new GroupToPermission() { Group = group, GroupId = group.Id, Permission = PermissionBank.GroupOperationPermission(group.GroupName, "member", "list") }); //find the grouptouser in the database //below is how to input parameters when the entity has composite key values: //"The ordering of composite key values is as defined in the EDM, which is in turn as defined in the designer, by the Code First fluent API, or by the DataMember attribute." // var groupToUser_db = _databaseService.GroupsToUsersRelation.Find(groupToUser.GroupId, groupToUser.UserId); //waiting for adding permissions for the group if (!_databaseService.Files.Any(s => s.Path == $"/groups/{group.GroupName}")) { var groupDirectory = EntityFile.CreateDirectoryRecord(group.GroupName, "/groups", $"/groups/{group.GroupName}", user); _databaseService.Files.Add(groupDirectory); } _databaseService.SaveChanges(); return(Ok(new GroupCreateResultModel(group))); }
private IActionResult RequestUploadFile(FileUploadRequestModel requestModel, User loginUser) { var ofile = _databaseService.Files. FirstOrDefault(s => s.Md5 == requestModel.Md5.ToLower() && // 哈希相等 s.Size == requestModel.Size && // 文件大小相等 s.Status == Models.Entities.File.FileStatus.Confirmed // 文件已上传完 ); var file = new EntityFile(); if (ofile != null) { file.Path = requestModel.Path; file.Type = requestModel.Type; file.Folder = Path.GetDirectoryName(requestModel.Path) ?? ""; file.Name = Path.GetFileName(requestModel.Path) ?? ""; file.Guid = ofile.Guid; file.StorageName = ofile.StorageName; file.User = loginUser; file.Status = Models.Entities.File.FileStatus.Confirmed; file.Size = ofile.Size; file.Md5 = ofile.Md5.ToLower(); } else { file.Path = requestModel.Path; file.Type = requestModel.Type; file.Folder = Path.GetDirectoryName(requestModel.Path) ?? ""; file.Name = Path.GetFileName(requestModel.Path) ?? ""; if (string.IsNullOrEmpty(file.Name)) { throw new FileNameIsEmptyException("The file name is empty."); } file.Guid = Guid.NewGuid().ToString().ToLower(); // TODO 检查 Guid 是否有重复 file.StorageName = $"{file.Guid[0]}{file.Guid[1]}/{file.Guid[2]}{file.Guid[3]}/{file.Guid}{Path.GetExtension(requestModel.Path)}"; file.StorageName = Path.Join(_tencentCosManagement.Prefix, file.StorageName).Replace("\\", "/"); file.User = loginUser; file.Status = Models.Entities.File.FileStatus.Pending; file.Size = requestModel.Size; file.Md5 = requestModel.Md5.ToLower(); } // file.Path = file.Folder.Replace("\\", "/"); file.Folder = file.Folder.Replace("\\", "/"); file.GetPermission(); _databaseService.Files.Add(file); Dictionary <string, object> token; if (file.Status == Models.Entities.File.FileStatus.Pending) { try { token = _tencentCos.GetUploadToken(file); } catch (Exception e) { _logger.LogError(e, e.Message, e.Data); throw new UnexpectedException(e.Message); } SetApiResultStatus(ApiResultStatus.StorageUploadContinue); } else { token = null; SetApiResultStatus(ApiResultStatus.StorageUploadSkip); } _databaseService.SaveChanges(); return(Ok(new FileUploadRequestResultModel(file, token, _tencentCosManagement))); }