/// <summary> /// Process the posted data before it is saved by the shared code. This allows checking the file attachments /// and making sure they meet the requirements of the SlkSettings. /// </summary> public bool ProcessPostedData(LearningSession session, HttpRequest request, Dictionary <string, HttpPostedFile> files) { // Save initial value of FinalPoints m_initialTotalPoints = session.TotalPoints; // Check the files for validity and fill in the output collection HttpFileCollection attachedFiles = Request.Files; int numFilesAttached = attachedFiles.Count; if (numFilesAttached > 0) { // Check if posted data meets requirements in SlkSettings. Additionally, check if there are files // that refer to files that do not exist (i.e. have 0 length). int maxKb = SlkStore.Settings.MaxAttachmentKilobytes; ICollection <string> attachmentTypes = SlkStore.Settings.ApprovedAttachmentTypes; List <string> invalidFileAttachments = new List <string>(numFilesAttached); List <string> invalidFileSize = new List <string>(numFilesAttached); List <string> filesDontExist = new List <string>(numFilesAttached); // Keep track of whether there is at least bool hasInvalidFileAttachment = false; bool hasInvalidFileSize = false; bool fileExists = true; // Go through posted files and test if they meet requirements foreach (string fileKey in attachedFiles) { HttpPostedFile file = attachedFiles[fileKey]; bool fileIsValid = true; // is this file a valid file attachment? string filename = file.FileName; // If the filename is empty, the file wasn't actually attached. if (!String.IsNullOrEmpty(filename)) { if (file.ContentLength == 0) { filesDontExist.Add(filename); fileExists = false; fileIsValid = false; } else if ((file.ContentLength / 1024) > maxKb) { invalidFileSize.Add(filename); hasInvalidFileSize = true; fileIsValid = false; } if (!attachmentTypes.Contains(Path.GetExtension(filename))) { invalidFileAttachments.Add(filename); hasInvalidFileAttachment = true; fileIsValid = false; } } // else: The file was valid on a previous posting, so consider it valid here if (fileIsValid) { // Add it to the returned list of valid files files.Add(fileKey, file); } } // if any of the posted files are invalid, then we need to write the message if (hasInvalidFileSize || hasInvalidFileAttachment || !fileExists) { StringBuilder message = new StringBuilder(1000); if (hasInvalidFileAttachment) { message.Append(ResHelper.GetMessage(SlkFrameset.CON_InvalidFileExtensionMsgHtml)); message.Append("<br><br><ul>"); foreach (string filename in invalidFileAttachments) { message.AppendFormat("<li>{0}</li>", SlkUtilities.GetHtmlEncodedText(filename)); } message.Append("</ul>"); } if (hasInvalidFileSize) { message.AppendFormat(ResHelper.GetMessage(SlkFrameset.CON_MaxFileSizeExceededMsgHtml, Convert.ToString(maxKb, CultureInfo.CurrentCulture.NumberFormat))); message.Append("<br><br><ul>"); foreach (string filename in invalidFileSize) { message.AppendFormat("<li>{0}</li>", SlkUtilities.GetHtmlEncodedText(filename)); } message.Append("</ul>"); } if (!fileExists) { message.AppendFormat(SlkFrameset.CON_FilesDontExistHtml); message.Append("<br><br><ul>"); foreach (string filename in filesDontExist) { message.AppendFormat("<li>{0}</li>", SlkUtilities.GetHtmlEncodedText(filename)); } message.Append("</ul>"); } // If one of the cases that relates to SLK settings is true, then tell user that there are settings // that affect the error if (hasInvalidFileSize || hasInvalidFileAttachment) { message.AppendFormat(CultureInfo.InvariantCulture, "{0}<br><br>", ResHelper.GetMessage(SlkFrameset.CON_InvalidFileAttachmentMsgHtml)); } message.Append(FramesetResources.CON_FileAttachmentErrorEndHtml); // Add information for the 'Continue' link JScriptString js = new JScriptString(ResHelper.FormatInvariant("API_GetFramesetManager().DoChoice(\"{0}\");", FramesetUtil.GetStringInvariant(session.CurrentActivityId))); message.AppendFormat(CultureInfo.InvariantCulture, "<br><br><a href='{0}' >{1}</a>", js.ToJavascriptProtocol(), HttpUtility.HtmlEncode(FramesetResources.HID_ReloadCurrentContent)); RegisterError(SlkFrameset.CON_InvalidFileAttachmentTitleHtml, message.ToString(), false); } } return(true); }