public async Task <string> SaveFile(IFormFile formFile, string relativeParentFolderPath, int parentFolderId) { if (formFile.Length > 0 && FileNameUtility.FileFolderNameIsValid(name: formFile.FileName)) { var relativePath = Path.Combine(relativeParentFolderPath, $"{parentFolderId}-{formFile.FileName}"); var filePath = Path.Combine( _env.ContentRootPath, _configuration["UsersFilesPath"], relativePath); var parentFolderPath = Path.Combine( _env.ContentRootPath, _configuration["UsersFilesPath"], relativeParentFolderPath); if (!Directory.Exists(parentFolderPath)) { Directory.CreateDirectory(parentFolderPath); } using (var stream = File.Create(filePath)) { await formFile.CopyToAsync(stream); } // Returns the relative path return(relativePath); } return(null); }
public async Task <IActionResult> AddFolder([FromBody] AddFolderRequest request) { var userIdString = JWTUtility.GetUserId(User); if (userIdString == null) { return(BadRequest()); } var userId = int.Parse(userIdString); if (request.ParentFolder != null) { // Check if the parent folder does belong to the user if (!await _registerService.DoesFolderBelongToUser( userId, folderId: request.ParentFolder.Value)) { return(BadRequest()); } } // Check if the folder name is valid if (!FileNameUtility.FileFolderNameIsValid(request.Name)) { return(BadRequest(Texts.INVALID_FILE_NAME)); } // Check if the folder does not exist if (await _registerService.DoesFileOrFolderAlreadyExist(userId, name: request.Name, parentFolder: request.ParentFolder)) { return(BadRequest(Texts.FILE_FOLDER_ALREADY_EXISTS)); } var folderAdded = await _registerService.AddFolder(userId, parentFolder : request.ParentFolder, request); if (folderAdded == null) { return(StatusCode(statusCode: 500, value: Texts.ERROR_CREATING_FOLDER)); } return(Ok(folderAdded)); }
public async Task <IActionResult> ChangeRegisterName([FromBody] ChangeRegisterNameRequest request) { var userIdString = JWTUtility.GetUserId(User); if (userIdString == null) { return(BadRequest()); } var userId = int.Parse(userIdString); // Check if the register does belong to the user. if (!await _registerService.DoesRegisterBelongToUser(userId, registerId: request.RegisterId)) { return(BadRequest()); } // Check if the new name is valid. if (!FileNameUtility.FileFolderNameIsValid(request.NewName)) { return(BadRequest(Texts.INVALID_FILE_NAME)); } // Check if the new name already exists in the parent folder. var parentFolderId = await _registerService.GetParentFolderForRegister(request.RegisterId); if (await _registerService.DoesFileOrFolderAlreadyExist(userId, name: request.NewName, parentFolder: parentFolderId)) { return(BadRequest(Texts.FILE_FOLDER_ALREADY_EXISTS)); } var registerChanged = await _registerService.ChangeRegisterName(userId, request); if (registerChanged == null) { return(StatusCode(statusCode: 500, value: Texts.ERROR_MODIFYING_REGISTER)); } return(Ok(registerChanged)); }
public Task <string> CreateFolder(string folderName, string relativeParentFolderPath, int parentFolderId) { if (FileNameUtility.FileFolderNameIsValid(name: folderName)) { var relativePath = Path.Combine(relativeParentFolderPath, $"{parentFolderId}-{folderName}"); var folderPath = Path.Combine( _env.ContentRootPath, _configuration["UsersFilesPath"], relativePath); // To indicates that this method does not create this folder if (Directory.Exists(folderPath)) { return(null); } Directory.CreateDirectory(folderPath); // Returns the relative path return(Task.FromResult(relativePath)); } return(null); }