public static AttachmentViewData Create(AttachmentInfo attachmentInfo, UserContext context, bool isOwner) { var r = AttachmentSecurityTools.ComputeRequestStr(context.DistrictId.Value, context.SchoolLocalId.Value, context.UserId, attachmentInfo.Attachment.Id); var model = Create(attachmentInfo, isOwner); model.PublicUrl = $"/Attachment/PublicAttachment?r={Uri.EscapeDataString(r)}"; return(model); }
public ActionResult PublicAttachment(string r, bool?needsDownload, int?width, int?height) { if (string.IsNullOrWhiteSpace(r)) { Response.StatusCode = 400; Response.StatusDescription = HttpWorkerRequest.GetStatusDescription(Response.StatusCode); return(null); } MasterLocator = ServiceLocatorFactory.CreateMasterSysAdmin(); Guid districtId; int schoolId; Guid userId; int attachmentId; if (!AttachmentSecurityTools.TryParseAndVerifyRequestStr(r, out districtId, out schoolId, out userId, out attachmentId)) { Response.StatusCode = 400; Response.StatusDescription = HttpWorkerRequest.GetStatusDescription(Response.StatusCode); return(null); } var user = MasterLocator.UserService.GetById(userId); SchoolLocator = ServiceLocatorFactory.CreateSchoolLocator(new SchoolUser { DistrictRef = districtId, SchoolRef = schoolId, UserRef = user.SchoolUsers.First(x => x.SchoolRef == schoolId).UserRef, School = MasterLocator.SchoolService.GetById(districtId, schoolId), User = new User { Login = string.Empty, LoginInfo = new UserLoginInfo { SisToken = string.Empty, SisTokenExpires = null }, DistrictRef = districtId, District = MasterLocator.DistrictService.GetByIdOrNull(districtId) } }); var attContentInfo = SchoolLocator.AttachementService.GetAttachmentContent(attachmentId); if (attContentInfo == null) { Response.StatusCode = 404; Response.StatusDescription = HttpWorkerRequest.GetStatusDescription(Response.StatusCode); return(null); } var attName = attContentInfo.Attachment.Name; var content = attContentInfo.Content; var contentTypeName = MimeHelper.GetContentTypeByName(attName); if (MimeHelper.GetTypeByName(attName) == MimeHelper.AttachmenType.Picture && width.HasValue && height.HasValue) { content = ImageUtils.Scale(content, width.Value, height.Value); } if (needsDownload.HasValue && !needsDownload.Value) { Response.AddHeader(CONTENT_DISPOSITION, string.Format(HEADER_FORMAT, attName)); return(File(content, contentTypeName)); } return(File(content, contentTypeName, attName)); }