using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using NHibernate; using NHibernate.Linq; using Sleis.Models; using Sleis.Infrastructure; using Sleis.Service; using Sleis.Utility; using Sleis.Validation; using Sleis.Validation.Attribute; using Sleis.ViewModels; using System.Web; using System.IO; using System.IO.Compression; //using Windsor.Commons.Compression; namespace Sleis.Controllers { [HandleError] public class ReportAttachmentController : BaseController { public IReportAttachmentService ReportAttachmentService { get; set; } public int UploadFileMaxSixe { get; set; } public override void Init() { base.Init(); ArgumentValidationUtility.ThrowOnNull(ReportAttachmentService, "ReportAttachmentService"); } [Authorize] public ActionResult List(int id, int facilityId) { SetSelectedFacility(facilityId); ReportAttachmentView reportAttachmentView = new ReportAttachmentView(); reportAttachmentView.Report = UserService.FacilityData.GetReport(facilityId, id); return View(reportAttachmentView); } [Authorize, HttpGet] public JsonResult Get(int id = 0, int facilityId = 0) { ListView m = new ListView(); m.List = ReportAttachmentService.ReportAttachmentData.GetReportAttachments(id); return Json(m, JsonRequestBehavior.AllowGet); } [Authorize, AppFacilityRoleValidation(AppUserRoleType.FacilityEditor)] public ActionResult Delete(int id, int reportId = 0, int facilityId = 0) { try { ReportAttachmentService.ReportAttachmentData.Delete(id); TempData[Constants.GlobalMessageKey] = new SimpleMessage("The report attachment was deleted successfully."); } catch(Exception ex) { Log.Error("Manage Report Attachment", ex); TempData[Constants.GlobalMessageKey] = new SimpleMessage("Error while deleting data. Please see logs for details", true); } return RedirectToAction("List", "ReportAttachment", new { id = reportId, facilityId = facilityId }); } [Authorize, AppFacilityRoleValidation(AppUserRoleType.FacilityEditor)] public ActionResult Manage(int id, int reportId = 0, int facilityId = 0) { ReportAttachmentEditView model = new ReportAttachmentEditView(); SetIds(reportId, facilityId, model); if (id > 0) { model.Item = ReportAttachmentService.ReportAttachmentData.GetReportAttachment(id); } return View(model); } [Authorize, HttpPost, AppFacilityRoleValidation(AppUserRoleType.FacilityEditor)] public ActionResult Manage(int id, ReportAttachmentEditView model) { if (model.Item.Attachment.Description == null) { ModelState.AddModelError("Item.Attachment.Description", "This field is required"); } if (ModelState.IsValid) { ReportAttachment reportAttachment = model.Item; try { ReportAttachmentService.ReportAttachmentData.Update(reportAttachment); TempData[Constants.GlobalMessageKey] = new SimpleMessage("The report attachment was saved successfully."); return RedirectToAction("List", "ReportAttachment", new { id = model.ReportId, facilityId = model.FacilityId }); } catch (Exception ex) { Log.Error("Manage Report Attachment", ex); TempData[Constants.GlobalMessageKey] = new SimpleMessage("Error while saving data. Please see logs for details", true); } } return View(model); } [Authorize] public ActionResult Download(int id, int reportId = 0, int facilityId = 0) { try { ReportAttachmentDetail detail = ReportAttachmentService.ReportAttachmentDetailData.GetReportAttachmentDetail(id); detail.Content = CompressionUtility.Decompress(detail.Content); return File(detail.Content, detail.Attachment.MimeType, detail.Attachment.Name); } catch (Exception ex) { Log.Error("Download Report Attachment", ex); TempData[Constants.GlobalMessageKey] = new SimpleMessage("Error while downloading Report Attachment. Please see logs for details", true); return RedirectToAction("List", new { id = reportId, facilityId = facilityId }); } } [Authorize] public ActionResult Upload(int id, int reportId = 0, int facilityId = 0) { ReportAttachmentDetailView model = new ReportAttachmentDetailView(); SetIds(reportId, facilityId, model); model.Item.Attachment.ReportId = ReportAttachmentService.ReportAttachmentData.GetReportAttachmentReportId(reportId); ViewData["ValidFileExt"] = DocValidTypes; return View(model); } [Authorize, HttpPost] public ActionResult Upload(int id, ReportAttachmentDetailView model, HttpPostedFileBase file) { if (string.IsNullOrWhiteSpace(model.Item.Attachment.Description)) { ModelState.AddModelError("Item.Attachment.Description", "This field is required"); } if (file == null) { ModelState.AddModelError("Item.Attachment.Name", "This field is required"); } if (ModelState.IsValid) { ActionResult result =RedirectToAction("List", new { id = model.ReportId, facilityId = model.FacilityId }); try { string ext = Path.GetExtension(file.FileName).ToLower(); if (!DocValidTypes.Contains(ext.Substring(ext.IndexOf('.')+1))) { TempData[Constants.GlobalMessageKey] = new SimpleMessage("Invalid file extension. Supported file types include doc, docx, pdf, jpg, jpeg, png, csv, xls, xlsx, ppt, and pptx.", true); return result; } if (file.ContentLength >= UploadFileMaxSixe) { TempData[Constants.GlobalMessageKey] = new SimpleMessage(string.Format("Upload file size {0} exceeds allowed max size {1}",file.ContentLength,UploadFileMaxSixe) , true); return result; } string fileName = file.FileName.Replace("-", string.Empty); byte[] buffer = new byte[file.ContentLength]; file.InputStream.Read(buffer, 0, file.ContentLength); model.Item.Content = CompressionUtility.Compress(buffer); model.Item.Attachment.Name = fileName; model.Item.Attachment.MimeType = MimeExtenstionUtility.GetContentTypeByExtenstion(ext); ReportAttachmentService.Save(model.Item); TempData[Constants.GlobalMessageKey] = new SimpleMessage("The report attachment was uploaded successfully."); return result; } catch (Exception ex) { Log.Error("Upload Report Attachment", ex); TempData[Constants.GlobalMessageKey] = new SimpleMessage("Error while saving report attachment content. Please see logs for details", true); return result; } } ViewData["ValidFileExt"] = DocValidTypes; return View(model); } private void SetIds(int reportId, int facilityId, ItemViewBase model) { model.ReportId = reportId ; model.FacilityId = facilityId ; } } }