Ejemplo n.º 1
0
        public ActionResult AddAttachment(string title, AddAttachmentViewModel formData)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                return(RedirectToAction(nameof(TicketsController.Index)));
            }

            if (!ModelState.IsValid)
            {
                return(View());
            }

            if (formData.FileUpload != null)
            {
                var fileExtension = Path.GetExtension(formData.FileUpload.FileName).ToLower();

                if (!AllowedExtenions.Contains(fileExtension))
                {
                    ModelState.AddModelError("", "File extension is not allowed");
                    return(View());
                }
            }

            var ticket = Context.Tickets.FirstOrDefault(p => p.Title == title);

            if (ticket == null)
            {
                return(RedirectToAction(nameof(TicketsController.Index)));
            }

            Attachment attachment;

            attachment          = new Attachment();
            attachment.MediaUrl = UploadFile(formData.FileUpload);
            attachment.TicketId = ticket.Id;
            attachment.UserId   = User.Identity.GetUserId();
            attachment.UserName = User.Identity.GetUserName();
            Context.Attachments.Add(attachment);
            Context.SaveChanges();

            if (attachment.UserId != ticket.AssignedToId)
            {
                var userEmail = ticket.AssignedTo.UserName;

                EmailService emailNotification = new EmailService();
                emailNotification.Send(userEmail, "New Attachment has been added to a ticket assigned to you.", "Ticket Update");
            }

            if (ticket.UserNotification != null)
            {
                var userEmails   = ticket.UserNotification.Select(p => p.UserName);
                var JoinedEmails = String.Join(", ", userEmails.ToArray());


                EmailService emailNotification = new EmailService();
                emailNotification.Send(JoinedEmails, "A new Attachment has been added to the ticket.", "Ticket Update");
            }

            return(Redirect(Request.UrlReferrer.PathAndQuery));
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> UploadAttachment([FromBody] AddAttachmentViewModel info)
        {
            #region Parameter validation

            if (info == null)
            {
                info = new AddAttachmentViewModel();
                Validate(info);
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            #endregion

            var students = UnitOfWork.RepositoryStudent.Search();

            // Find the first match request.
            var student = await students.Where(x => x.Id == info.StudentId).FirstOrDefaultAsync();

            // Check null
            if (student == null)
            {
                return(ResponseMessage(
                           Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.StudentNotFound)));
            }

            var document      = info.Document;
            var fileExtension = Path.GetExtension(document.Name);
            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.Replace(".", "");
            }

            var name         = document.Name.Split('.');
            var documentName = name[0];

            var attachment = new Database.Models.Entities.Attachment
            {
                StudentId = info.StudentId,
                Name      = documentName,
                Type      = fileExtension,
                Content   = document.Buffer,
                Status    = MasterItemStatus.Active
            };

            attachment = UnitOfWork.RepositoryAttachment.Insert(attachment);

            //await UnitOfWork.CommitAsync();

            return(Ok(attachment));
        }
Ejemplo n.º 3
0
        private string ProcessUploadedAttachment(AddAttachmentViewModel attach)
        {
            string uniqueFileName = null;

            if (attach.File != null)
            {
                string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "AttachedFiles");
                uniqueFileName = Guid.NewGuid().ToString() + "-" + attach.File.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    attach.File.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
Ejemplo n.º 4
0
        public IActionResult AddAttachment(AddAttachmentViewModel model)
        {
            if (ModelState.IsValid)
            {
                string             uniqueFileName = ProcessUploadedAttachment(model);
                EmployeeAttachment newAttachment  = new EmployeeAttachment
                {
                    FileName   = model.FileName,
                    EmployeeId = model.EmployeeId,
                    FileId     = model.FileId,

                    Notes    = model.Notes,
                    Type     = model.Type,
                    FileSize = model.FileSize,
                    URLOnly  = uniqueFileName
                };
                _employeeRepository.AddEmpAttachment(newAttachment);

                var successMessage = "Attachment Info Created Successfully. Name: " + newAttachment.EmployeeId;
                TempData["successAlert"] = successMessage;
                return(View(successMessage, ""));
            }
            return(View());
        }
        public ActionResult CreateAttachment(int id, AddAttachmentViewModel _Attachment, long?SessionID = null)
        {
            if (CurrentUser != null)
            {
                if (sessionService.IsSessionSentToJudge(Convert.ToInt32(SessionID)))
                {
                    return(JavaScript("$(document).trigger('FileAttachment:SentToJudge');"));
                }

                if (ModelState.IsValid || _Attachment.FolderID.ToString() != "00000000-0000-0000-0000-000000000000")
                {
                    var AddDocumentStatus = AttachmentsSaveStatus.Folder_Has_Documents;
                    if (_Attachment.FolderID.ToString() != "00000000-0000-0000-0000-000000000000")
                    {
                        AddDocumentStatus = AttachmentService.AddDocument(id, null, new Base.Views.vw_Documents
                        {
                            FolderID      = _Attachment.FolderID == Guid.Empty ? (Guid?)null : _Attachment.FolderID,
                            DocumentTitle = _Attachment.Name,
                            TypeID        = (int)_Attachment.AttachmentType,
                            FileName      = _Attachment.FileName,
                            FileData      = System.IO.File.ReadAllBytes(_Attachment.filePath)
                            ,
                            CurentUserName = CurrentUser.UserName
                        });
                    }
                    else
                    {
                        AddDocumentStatus = AttachmentService.AddDocument(id, SessionID, new Base.Views.vw_Documents
                        {
                            FolderID      = _Attachment.FolderID == Guid.Empty ? (Guid?)null : _Attachment.FolderID,
                            DocumentTitle = _Attachment.Name,
                            TypeID        = (int)_Attachment.AttachmentType,
                            FileName      = _Attachment.FileName,
                            FileData      = System.IO.File.ReadAllBytes(_Attachment.filePath)
                            ,
                            CurentUserName = CurrentUser.UserName
                        });
                    }
                    // bool SavedAdd = true;
                    switch (AddDocumentStatus)
                    {
                    case AttachmentsSaveStatus.Saved:
                        return(JavaScript("$(document).trigger('FolderAttachment2:Saved');"));

                    //    return RedirectTo(Url.Action("Index", new { id = _Attachment.CaseID, SessionID = SessionID, SavedAdd = SavedAdd })).WithSuccessMessages(JIC.Base.Resources.Messages.OperationCompletedSuccessfully);
                    // return JavaScript("<script>$('#CaseAttachmentsDiv').trigger('update');</script>");
                    case AttachmentsSaveStatus.Failed:
                        break;

                    case AttachmentsSaveStatus.NumberOfDocumentOverFlow:
                        //   return RedirectTo(Url.Action("Index", new { id = _Attachment.CaseID, SessionID = SessionID, SavedAdd = SavedAdd })).WithErrorMessages("الحافظة ممتلئة");
                        return(JavaScript("$(document).trigger('NumberOfDocumentOverFlow:error');"));
                    }
                }
                else
                {
                    ViewData["SessionEnded"] = false;
                    Response.StatusCode      = 404;
                    //return CaseAttachment(id);
                    return(CPartialView(_Attachment));
                }
                return(Json(new { }));
            }
            else
            {
                ViewData["SessionEnded"] = true;
                return(PartialView());
            }
        }