コード例 #1
0
        [HttpPost] // Authorize
        public async Task <IActionResult> Post(string resource, Guid referenceId, ICollection <IFormFile> file)
        {
            foreach (var f in file)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await f.CopyToAsync(memoryStream);

                    var create = new Domain.File
                    {
                        Resource    = resource,
                        ReferenceId = referenceId,
                        Name        = f.FileName,
                        FileType    = f.ContentType,
                        ContentType = f.ContentType,
                        Size        = f.Length,
                        Content     = new Domain.FileContent(memoryStream.ToArray())
                    };

                    context.Add(create);
                    await context.SaveChangesAsync();
                }
            }

            return(Accepted());
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("UserID,UserName,UserEmail,UserPassword,UserRole")] AccountModel accountModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(accountModel);
                await _context.SaveChangesAsync();

                ClaimsIdentity identity       = null;
                bool           isAuthenticate = false;

                identity = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.Name, accountModel.UserName),
                    new Claim(ClaimTypes.Role, accountModel.UserRole)
                }, CookieAuthenticationDefaults.AuthenticationScheme);
                isAuthenticate = true;
                if (isAuthenticate)
                {
                    var principal = new ClaimsPrincipal(identity);
                    var login     = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
                    return(RedirectToAction("Index", "Home"));
                }
                return(View());
            }
            return(View(accountModel));
        }
コード例 #3
0
ファイル: UserService.cs プロジェクト: Heiner07/Drive-API
        public async Task <AuthenticatedUser> RegisterUser(RegisterUserRequest request)
        {
            string encryptedPassword = EncrypterUtility.StringToSHA256String(value: request.Password);

            var userToRegister = new UserDrive
            {
                Name     = request.Name,
                Lastname = request.Lastname,
                Username = request.Username,
                Email    = request.Email,
                Password = encryptedPassword,
            };

            _context.UserDrive.Add(userToRegister);
            var entriesWritten = await _context.SaveChangesAsync();

            if (entriesWritten > 0)
            {
                return(await AuthenticateUser(request : new AuthenticationRequest
                {
                    EmailOrUsername = userToRegister.Email,
                    Password = request.Password,
                }
                                              ));
            }

            return(null);
        }
コード例 #4
0
ファイル: RegisterService.cs プロジェクト: Heiner07/Drive-API
        public async Task <Register> AddFile(int userId, int?parentFolder, AddFileRequest request)
        {
            string relativeFilePath;

            if (parentFolder.HasValue)
            {
                var folder = await _context.Register.Where(
                    register => register.Id == parentFolder.Value).AsNoTracking().FirstOrDefaultAsync();

                if (folder == null)
                {
                    return(null);
                }

                relativeFilePath = await _userFilesService.SaveFile(
                    formFile : request.File,
                    relativeParentFolderPath : folder.PathOrUrl,
                    parentFolderId : folder.Id
                    );
            }
            else
            {
                // IMPORTANT: When the parentFolder is null, the relativeParentFolderPath will be the user id
                // and the parentFolderId (only for the relativeFilePath) will be 0.
                // For example: If the user id is 5, the file path will be "5/0-fileName.extension"
                relativeFilePath = await _userFilesService.SaveFile(
                    formFile : request.File,
                    relativeParentFolderPath : userId.ToString(),
                    parentFolderId : 0
                    );
            }

            if (relativeFilePath == null)
            {
                return(null);
            }

            var fileToAdd = _context.Register.Add(new Register
            {
                Name             = request.File.FileName,
                PathOrUrl        = relativeFilePath,
                FileExtension    = Path.GetExtension(path: relativeFilePath),
                Size             = (int?)request.File.Length,
                UploadDate       = DateTime.Now,
                LastModifiedDate = DateTime.Now,
                IsFolder         = false,
                ParentFolder     = parentFolder,
                Author           = userId,
            });

            var entriesWritten = await _context.SaveChangesAsync();

            if (entriesWritten > 0)
            {
                return(fileToAdd.Entity);
            }

            return(null);
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("TaskID,TaskName,TaskDescription,TaskDeadline,TaskCourse,TaskTeacher")] TaskModel taskModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(taskModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(taskModel));
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("CourseID,CourseName,CourseDescription,CourseSchedule,CourseTeacher")] CourseModel courseModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(courseModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(courseModel));
        }
コード例 #7
0
        public async Task <IActionResult> CreateContent([Bind("ContentID,CourseID,ContentName,ContentFile")] ContentModel contentModel)
        {
            if (ModelState.IsValid)
            {
                //Save to File
                string wwwRootPath = _hostEnvironment.WebRootPath;
                string fileName    = Path.GetFileNameWithoutExtension(contentModel.ContentFile.FileName);
                string extension   = Path.GetExtension(contentModel.ContentFile.FileName);
                contentModel.ContentDescription = fileName = fileName + extension;
                //contentModel.ContentDescription = DateTime.Now.ToString("dddd, dd MMMM yyyy");
                string path = Path.Combine(wwwRootPath + "/File", fileName);
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await contentModel.ContentFile.CopyToAsync(fileStream);
                }
                //Insert record
                _context.Add(contentModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction("ContentList", new { courseID = contentModel.CourseID }));
            }
            return(View(contentModel));
        }
コード例 #8
0
        public async Task <IActionResult> CreateSubmit([Bind("SubmitID,TaskID,SubmitName,ContentFile,SubmitTime,SubmitScore")] SubmitModel submitModel)
        {
            if (ModelState.IsValid)
            {
                //Save to File
                string wwwRootPath = _hostEnvironment.WebRootPath;
                string fileName    = Path.GetFileNameWithoutExtension(submitModel.ContentFile.FileName);
                string extension   = Path.GetExtension(submitModel.ContentFile.FileName);
                //contentModel.ContentDescription = fileName = fileName + DateTime.Now.ToString("yymmssffff") + extension;
                submitModel.SubmitDescription = fileName = fileName + extension;
                submitModel.SubmitTime        = DateTime.Now.ToString("dd MMMM yyyy");
                string path = Path.Combine(wwwRootPath + "/File", fileName);
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await submitModel.ContentFile.CopyToAsync(fileStream);
                }
                //Insert record
                _context.Add(submitModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction("SubmitList", new { TaskID = submitModel.TaskID }));
            }
            return(View(submitModel));
        }