示例#1
0
        public IActionResult Login(LoginModel loginModel)
        {
            if (ModelState.IsValid)
            {
                User user = _hostingCore.GetUserByEmail(loginModel.Email);

                if (user != null)
                {
                    if (loginModel.Password.Equals(user.Password))
                    {
                        Authenticate(user);

                        return(RedirectToAction("Index", "Hosting"));
                    }

                    ModelState.AddModelError("", "Entered password is wrong");
                }
                else
                {
                    ModelState.AddModelError("", "There is no user with this email");
                }
            }

            return(View(loginModel));
        }
        public IActionResult UserPage(long userId)
        {
            User user;

            if (userId == 0)
            {
                user = _hostingCore.GetUserByEmail(User.Identity.Name);
            }
            else
            {
                user = _hostingCore.GetUserById(userId);
            }

            if (user == null)
            {
                return(NotFound());
            }

            var userFiles = new UserFilesModel
            {
                User  = user,
                Files = _hostingCore.GetUserFiles(user.Id)
            };

            return(View(userFiles));
        }
        public async Task <IActionResult> Upload(FileLoadModel loadModel)
        {
            if (ModelState.IsValid)
            {
                IFormFile uploadedFile = loadModel.UploadedFile;

                User user = _hostingCore.GetUserByEmail(User.Identity.Name);

                if (user != null && uploadedFile != null)
                {
                    if (uploadedFile.FileName.Length > 100)
                    {
                        ModelState.AddModelError("", "Max file name length is 100 symbols");

                        return(View(loadModel));
                    }

                    IEnumerable <HostingFile> userFiles = _hostingCore.GetUserFiles(user.Id);

                    var selectedFiles = from userFile in userFiles
                                        where userFile.Name == uploadedFile.FileName
                                        select userFile;

                    if (selectedFiles?.Count() > 0)
                    {
                        ModelState.AddModelError("", "You already uploaded a file with this name");

                        return(View(loadModel));
                    }

                    var link = $"Files/{user.Id}/";

                    var file = new HostingFile
                    {
                        AuthorId    = user.Id,
                        Name        = uploadedFile.FileName,
                        Size        = uploadedFile.Length,
                        Category    = loadModel.Category,
                        Description = loadModel.Description,
                        Link        = link
                    };

                    _hostingCore.InsertFile(file);

                    userFiles = _hostingCore.GetUserFiles(user.Id);

                    selectedFiles = from userFile in userFiles
                                    where userFile.Name == uploadedFile.FileName
                                    select userFile;

                    file = selectedFiles.FirstOrDefault();

                    file.Link += file.Id;

                    _hostingCore.UpdateFile(file.Id, file);

                    var path = _appEnvironment.WebRootPath + $"/Files/{user.Id}/";

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    path += file.Id;

                    using (var fileStream = new FileStream(path, FileMode.Create))
                    {
                        await uploadedFile.CopyToAsync(fileStream);
                    }

                    return(RedirectToAction("UserPage", "Hosting", new { userId = file.AuthorId }));
                }

                return(View(loadModel));
            }

            ModelState.AddModelError("", "An error occurred during uploading");
            return(View(loadModel));
        }