Esempio n. 1
0
        private void AddImage(SubmittedFile file)
        {
            var image = _fileRepository.CreateNewImage(file.FileName, Convert.FromBase64String(file.Data));

            file.Tags.ForEach(image.AddTag);
            // autoTagger.TagImage(image).Wait();
        }
Esempio n. 2
0
        public ActionResult Submit(TaskSubmission taskSubmission, long id)
        {
            if (ModelState.IsValid)
            {
                string userId     = User.Identity.GetUserId();
                var    student    = db.Students.Find(userId);
                var    classTask  = student.enrollment.ClassTasks.ToList().Find(p => p.Id == id);
                var    submission = classTask.TaskSubmissions.ToList().Find(p => p.Student.Id == student.Id);
                if (submission != null)
                {
                    ModelState.AddModelError("", "You have already submitted this task.");
                    return(View(taskSubmission));
                }
                if (classTask.submittingOption == "BlckBook" && (classTask.dueDate.Date == DateTime.Today && DateTime.Today.TimeOfDay > classTask.dueTime.TimeOfDay) || (DateTime.Today > classTask.dueDate.Date))
                {
                    ModelState.AddModelError("", "You cannot submit online anymore, please see your teacher.");
                    return(View(taskSubmission));
                }
                if (taskSubmission.file != null)
                {
                    var           fileName      = Path.GetFileName(taskSubmission.file.FileName);
                    SubmittedFile submittedFile = new SubmittedFile()
                    {
                        FileName       = fileName,
                        Extension      = Path.GetExtension(fileName),
                        Id             = taskSubmission.Id,
                        TaskSubmission = taskSubmission
                    };
                    taskSubmission.SubmittedFile = submittedFile;
                }

                taskSubmission.Student        = student;
                taskSubmission.ClassTask      = classTask;
                taskSubmission.submissionDate = DateTime.Now;
                db.TaskSubmissions.Add(taskSubmission);
                db.SaveChanges();
                if (taskSubmission.file != null)
                {
                    var fileName = Path.GetFileName(taskSubmission.file.FileName);
                    var path     = Path.Combine(Server.MapPath("~/App_Data/SubmittedFiles/"), taskSubmission.SubmittedFile.Id + fileName);
                    taskSubmission.file.SaveAs(path);
                }
                return(RedirectToAction("Index"));
            }
            return(View(taskSubmission));
        }
Esempio n. 3
0
        public async Task <IActionResult> Upload(Contribution contribution, IFormFile file,
                                                 ContributionStatus contributionStatus, int contributionId, bool[] privacy)
        {
            if (privacy.Length == 3)
            {
                var topic = await _context.Submissions.FirstOrDefaultAsync(t => t.Id == contribution.SubmissionId);

                if (topic.SubmissionDeadline_2 >= DateTime.Now)
                {
                    var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);


                    if (ModelState.IsValid)
                    {
                        var user = await _context.Users.FindAsync(userId);

                        var coordinatorIds = await _context.UserRoles.Where(u => u.RoleId == "Coordinator")
                                             .Select(u => u.UserId)
                                             .ToListAsync();

                        var coordinators = await _context.Users.Where(u => u.DepartmentId == user.DepartmentId &&
                                                                      coordinatorIds.Contains(u.Id)).ToListAsync();

                        var existContribution = await _context.Contributions.FirstOrDefaultAsync(c => c.ContributorId == userId &&
                                                                                                 c.SubmissionId == contribution.SubmissionId);



                        if (existContribution == null)
                        {
                            contribution.Status = ContributionStatus.Pending;

                            _context.Add(contribution);
                            await _context.SaveChangesAsync();

                            existContribution = contribution;
                        }

                        else
                        {
                            existContribution.Status = ContributionStatus.Pending;

                            _context.Update(existContribution);
                            await _context.SaveChangesAsync();
                        }

                        if (file.Length > 0)
                        {
                            FileType?fileType;
                            string   fileExtension = Path.GetExtension(file.FileName).ToLower();

                            switch (fileExtension)
                            {
                            case ".doc":
                            case ".docx": fileType = FileType.Document; break;

                            case ".jpg":
                            case ".png": fileType = FileType.Image; break;

                            default: fileType = null; break;
                            }

                            if (fileType != null)
                            {
                                try
                                {
                                    var path = Path.Combine(_Global.PATH_TOPIC, existContribution.SubmissionId.ToString(), user.Number);
                                    if (!Directory.Exists(path))
                                    {
                                        Directory.CreateDirectory(path);
                                    }
                                    // Upload file, create file
                                    var contributionDate = DateTime.Now;
                                    path             = Path.Combine(path, String.Format("{0}.{1:yyyy-MM-dd.ss-mm-HH}{2}", user.Number, contributionDate, fileExtension));
                                    using var stream = new FileStream(path, FileMode.Create);
                                    file.CopyTo(stream);
                                    var newFile = new SubmittedFile();
                                    newFile.ContributionId = existContribution.Id;
                                    newFile.URL            = path;
                                    newFile.Type           = (FileType)fileType;
                                    _context.Add(newFile);
                                    await _context.SaveChangesAsync();

                                    if (coordinators.Count() > 0)
                                    {
                                        foreach (var coordinator in coordinators)
                                        {
                                            var contributionFullname = $"{existContribution.Contributor.FirstName} {existContribution.Contributor.LastName}";
                                            var coordinatorFullName  = $"{coordinator.FirstName} {coordinator.LastName}";

                                            MailboxAddress from = new MailboxAddress("FGW Management System", "*****@*****.**");
                                            MailboxAddress to   = new MailboxAddress(coordinatorFullName, coordinator.Email);

                                            BodyBuilder bodyBuilder = new BodyBuilder();
                                            bodyBuilder.TextBody = $"Hello Coordinator,\n\n" +
                                                                   $"Your student was submited thier contribution with the title is {existContribution.Submission.Title},\n\n" +
                                                                   $"This is contribution by {contributionFullname},\n\n" +
                                                                   $"Please review and give their feedback soon as possible {contributionFullname},\n\n" +
                                                                   $"Thank you for checking this notification,\n\n" +
                                                                   $"Best regards.";

                                            MimeMessage message = new MimeMessage();
                                            message.From.Add(from);
                                            message.To.Add(to);
                                            message.Subject = $"Contribution for {existContribution.Submission.Title} Status";
                                            message.Body    = bodyBuilder.ToMessageBody();

                                            SmtpClient client = new SmtpClient();

                                            client.Connect("smtp.gmail.com", 465, true);
                                            client.Authenticate("huynhminhthong1912", "pqdquwmvialvcchv");

                                            client.Send(message);
                                            client.Disconnect(true);
                                            client.Dispose();
                                        }
                                    }
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                TempData["PrivacyError"] = "Please accept all our privacy to submit your contribution.";
            }
            return(RedirectToAction(nameof(Details), new { id = contribution.SubmissionId }));
        }
Esempio n. 4
0
        public async Task <IActionResult> UploadFile(Contribution contribution, IFormFile file)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);


            if (ModelState.IsValid)

            {
                var user = await _context.Users.FindAsync(userId);

                var existContribution = await _context.Contribution.Include(c => c.Topic)
                                        .Include(c => c.Contributor)
                                        .FirstOrDefaultAsync(c => c.ContributorId == userId &&
                                                             c.TopicId == contribution.TopicId);

                var Topic = await _context.Topic.FindAsync(contribution.TopicId);

                if (DateTime.Now <= Topic.Deadline_2)
                {
                    if ((DateTime.Now <= Topic.Deadline_1) || (DateTime.Now > Topic.Deadline_1 && existContribution != null))
                    {
                        if (existContribution == null)
                        {
                            contribution.ContributorId = userId;
                            contribution.Status        = ContributionStatus.Pending;

                            _context.Add(contribution);
                            await _context.SaveChangesAsync();

                            existContribution = contribution;
                        }

                        else
                        {
                            existContribution.Status = ContributionStatus.Pending;

                            _context.Update(existContribution);
                            await _context.SaveChangesAsync();
                        }

                        if (file.Length > 0)
                        {
                            FileType?fileType;
                            string   fileExtension = Path.GetExtension(file.FileName).ToLower();

                            switch (fileExtension)
                            {
                            case ".doc":
                            case ".docx": fileType = FileType.Document; break;

                            case ".jpg":
                            case ".png": fileType = FileType.Image; break;

                            default: fileType = null; break;
                            }

                            if (fileType != null)
                            {
                                var path = Path.Combine(_Global.PATH_TOPIC, existContribution.TopicId.ToString(), user.Number);

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

                                // Upload file
                                path = Path.Combine(path, String.Format("{0}.{1:yyyy-MM-dd.ss-mm-HH}{2}", user.Number, DateTime.Now, fileExtension));
                                var stream = new FileStream(path, FileMode.Create);
                                file.CopyTo(stream);

                                var newFile = new SubmittedFile();
                                newFile.ContributionId = existContribution.Id;
                                newFile.URL            = path;
                                newFile.Type           = (FileType)fileType;

                                _context.Add(newFile);
                                await _context.SaveChangesAsync();

                                var topic = await _context.Topic.FindAsync(existContribution.TopicId);

                                var contributorFullname = $"{user.FirstName} {user.LastName}";

                                MailboxAddress from = new MailboxAddress("iMarketing System", "*****@*****.**");
                                MailboxAddress to   = new MailboxAddress(contributorFullname, user.Email);

                                BodyBuilder bodyBuilder = new BodyBuilder();
                                bodyBuilder.TextBody = $"Hello {contributorFullname}, \n\n" +
                                                       $"Your contribution for {topic.Title} is uploaded successfully.\n\n" +
                                                       $"Thank you for your contribution,\n\n" +
                                                       $"Best regards,";

                                MimeMessage message = new MimeMessage();
                                message.From.Add(from);
                                message.To.Add(to);
                                message.Subject = $"contribution for {topic.Title} Status";
                                message.Body    = bodyBuilder.ToMessageBody();

                                SmtpClient client = new SmtpClient();
                                client.Connect("smtp.gmail.com", 465, true);
                                client.Authenticate("systememail8000", "abcABC@123");

                                client.Send(message);
                                client.Disconnect(true);
                                client.Dispose();
                            }
                        }
                    }
                }
            }
            return(RedirectToAction(nameof(Details), new { id = contribution.TopicId }));
        }
Esempio n. 5
0
        public async Task <IActionResult> UpLoadFile(Contribution contribution, IFormFile file, bool isAcceptTerms = false)
        {
            if (isAcceptTerms)
            {
                if (ModelState.IsValid)
                {
                    var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
                    var user   = await _context.Users.FindAsync(userId);

                    var existContribution = await _context.Contribution.FirstOrDefaultAsync(c => c.ContributorId == userId && c.TopicId == contribution.TopicId);

                    var topic = await _context.Topic.FindAsync(contribution.TopicId);

                    if (topic.Deadline_2 < DateTime.Now)
                    {
                        return(RedirectToAction(nameof(Details), new { id = contribution.TopicId, error = "Deadline 2 is over." }));
                    }
                    if (topic.Deadline_1 < DateTime.Now)
                    {
                        if (existContribution == null)
                        {
                            return(RedirectToAction(nameof(Details), new { id = contribution.TopicId, error = "Deadline 1 is over." }));
                        }
                    }


                    if (file.Length > 0)
                    {
                        if (existContribution == null)
                        {
                            contribution.ContributorId = userId;

                            contribution.Status = ContributionStatus.Pending;

                            _context.Add(contribution);
                            await _context.SaveChangesAsync();

                            existContribution = contribution;
                        }

                        else
                        {
                            existContribution.Status = ContributionStatus.Pending;

                            _context.Update(existContribution);
                            await _context.SaveChangesAsync();
                        }
                        FileType?fileType;
                        string   fileExtension = Path.GetExtension(file.FileName).ToLower();

                        switch (fileExtension)
                        {
                        case ".doc":
                        case ".docx": fileType = FileType.Document; break;

                        case ".jpg":
                        case ".png": fileType = FileType.Image; break;

                        default: fileType = null; break;
                        }

                        if (fileType != null)
                        {
                            var path = Path.Combine(_Global.PATH_TOPIC, existContribution.TopicId.ToString(), user.Number);

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

                            // Upload file
                            path             = Path.Combine(path, String.Format("{0}.{1:yyyy-MM-dd.ss-mm-HH}{2}", user.Number, DateTime.Now, fileExtension));
                            using var stream = new FileStream(path, FileMode.Create);
                            file.CopyTo(stream);

                            var newFile = new SubmittedFile();
                            newFile.ContributionId = existContribution.Id;
                            newFile.URL            = path;
                            newFile.Type           = (FileType)fileType;

                            _context.Add(newFile);
                            await _context.SaveChangesAsync();
                        }
                    }
                }
            }
            else
            {
                return(RedirectToAction(nameof(Details), new { id = contribution.TopicId, error = "You must accept Terms." }));
            }

            return(RedirectToAction(nameof(Details), new { id = contribution.TopicId }));
        }
Esempio n. 6
0
 private bool HashIsNew(SubmittedFile file)
 => _fileRepository
 .FetchFiles(new FileQuery {
     ImageHash = new FileHash(_datahasher.Hash(file.Data), null)
 })
 .Count() == 0;