示例#1
0
        public IActionResult Upload(SubmissionViewModel newSub)
        {
            var tasks = _tasksService.GetTasks();

            ViewBag.Tasks = tasks;

            newSub.Id            = new Guid();
            newSub.Owner         = HttpContext.User.Identity.Name;
            newSub.TimeSubmitted = DateTime.Now;

            if (string.IsNullOrEmpty(newSub.FileName))
            {
                _logger.LogError("Title is empty or NULL");
                return(View("Error", new ErrorViewModel()
                {
                    Message = "Title cannot be empty"
                }));
            }
            else
            {
                TempData["message"] = "Task created successfully";
                _submissionService.AddSubmission(newSub);
                return(View());
            }
        }
        //[Authorize]
        public IActionResult Create(IFormFile file, SubmissionViewModel data, string id)
        {
            try
            {
                string urlEnc = Encryption.SymmetricDecrypt(id);
                Guid   decId  = Guid.Parse(urlEnc);

                var memId = _membersService.GetMember(User.Identity.Name);

                if (file != null)
                {
                    data.task = _tasksService.GetTask(decId);

                    if (data.task.deadline > DateTime.Now)
                    {
                        string uniqueFilename;

                        if (System.IO.Path.GetExtension(file.FileName) == ".pdf" && file.Length < 1048576)
                        {
                            byte[] whiteList = new byte[] { 37, 80, 68, 70 };
                            if (file != null)
                            {
                                MemoryStream ms = new MemoryStream();

                                using (var f = file.OpenReadStream())
                                {
                                    byte[] buffer = new byte[4];
                                    f.Read(buffer, 0, 4);

                                    for (int i = 0; i < whiteList.Length; i++)
                                    {
                                        if (whiteList[i] == buffer[i])
                                        {
                                        }
                                        else
                                        {
                                            ModelState.AddModelError("file", "Invalid file");
                                            _logger.LogError("Invalid file |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name);
                                            return(View());
                                        }
                                    }

                                    f.Position = 0;

                                    uniqueFilename = Guid.NewGuid() + Path.GetExtension(file.FileName);
                                    data.file      = uniqueFilename;

                                    string absolutePath = @"ValuableFiles\" + uniqueFilename;

                                    try
                                    {
                                        file.CopyTo(ms);
                                        var encFile = Encryption.HybridEncrypt(ms, memId.PublicKey);
                                        data.signature = Encryption.SignData(ms, memId.PrivateKey);
                                        System.IO.File.WriteAllBytes(absolutePath, encFile.ToArray());
                                        f.Close();
                                    }
                                    catch (Exception ex)
                                    {
                                        _logger.LogError(ex, "Error happend while saving file |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name);
                                        return(View("Error", new ErrorViewModel()
                                        {
                                            Message = "Error while saving the file. Try again later"
                                        }));
                                    }
                                }
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("file", "File is not valid or size is greater than 10Mb");
                            _logger.LogError("File is not valid or size is greater than 10Mb |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name);
                            return(View());
                        }

                        data.email = HttpContext.User.Identity.Name;

                        _submissionService.AddSubmission(data);

                        TempData["message"] = "Document submitted successfully";
                        _logger.LogInformation("File uploaded successfully | ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name);
                        return(View());
                    }
                    else
                    {
                        TempData["error"] = "Deadline date overdue";
                        _logger.LogError("Deadline date overdue |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name);
                        return(View());
                    }
                }
                else
                {
                    ModelState.AddModelError("file", "Check your input. Operation failed");
                    return(View(data));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name);
                return(RedirectToAction("Error"));
            }
        }
        public IActionResult Upload(IFormFile file, SubmissionViewModel data, string idEnc)
        {
            string idDec = Encryption.SymmetricDecrypt(idEnc);
            Guid   id    = Guid.Parse(idDec);

            SubmissionViewModel myModel = new SubmissionViewModel();

            myModel.TaskId = idEnc;

            data.Task  = _taskService.GetTask(id);
            data.Email = User.Identity.Name;

            var member = _memService.GetMember(data.Email);

            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());

            IPAddress[] addr = ipEntry.AddressList;

            if (data.Task.Deadline > DateTime.Now)
            {
                if (ModelState.IsValid)
                {
                    if (file != null)
                    {
                        string uniqueFilename;
                        if (System.IO.Path.GetExtension(file.FileName) == ".pdf" && file.Length < 1048576)
                        {
                            //137 80 78 71 13 10 26 10
                            byte[] whiteList = new byte[] { 37, 80, 68, 70 };
                            if (file != null)
                            {
                                MemoryStream userFile = new MemoryStream();
                                using (var f = file.OpenReadStream())
                                {
                                    byte[] buffer = new byte[4];
                                    f.Read(buffer, 0, 4);

                                    for (int i = 0; i < whiteList.Length; i++)
                                    {
                                        if (whiteList[i] == buffer[i])
                                        {
                                        }
                                        else
                                        {
                                            _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nFile is not valid and accapteable");
                                            ModelState.AddModelError("file", "file is not valid and accapteable");
                                            return(View());
                                        }
                                    }

                                    f.Position = 0;

                                    //uploading the file
                                    uniqueFilename = Guid.NewGuid() + Path.GetExtension(file.FileName);
                                    data.File      = uniqueFilename;

                                    string absolutePath = @"ValuableFiles\" + uniqueFilename;
                                    try
                                    {
                                        file.CopyTo(userFile);
                                        var encryptedFileMemoryStream = Encryption.HybridEncrypt(userFile, member.PublicKey);
                                        System.IO.File.WriteAllBytes(absolutePath, encryptedFileMemoryStream.ToArray());
                                        data.Signature = Encryption.SignData(encryptedFileMemoryStream, member.PrivateKey);
                                        f.Close();
                                    }
                                    catch (Exception e)
                                    {
                                        _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nError happend while saving file.");
                                        return(View("Error", new ErrorViewModel()
                                        {
                                            Message = "Error while saving the file. Try again later"
                                        }));
                                    }
                                }
                            }
                            _subService.AddSubmission(data);
                            _logger.LogInformation("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nAssignment submitted successfully.");
                            TempData["message"] = "Assignment submitted successfully";
                            return(View(myModel));
                        }
                        else
                        {
                            _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nFile is not valid and acceptable or size is greater than 10Mb");
                            ModelState.AddModelError("", "File is not valid and acceptable or size is greater than 10Mb");
                            return(View(myModel));
                        }
                    }
                    else
                    {
                        _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nNo file was selected");
                        ModelState.AddModelError("", "No file was selected");
                        return(View(myModel));
                    }
                }
                else
                {
                    _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nOperation failed due to input error.");
                    ModelState.AddModelError("", "Check your input. Operation failed");
                    return(View(myModel));
                }
            }
            else
            {
                _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nDeadline expired");
                ModelState.AddModelError("", "Sorry, deadline has been expired.");
                return(View(myModel));
            }
        }