protected void Page_Load(object sender, EventArgs e) { //Get the user's current program role currentProgramRole = Utilities.GetProgramRoleFromSession(Session); //Get the file PK from the query string if (!string.IsNullOrWhiteSpace(Request.QueryString["UserFileUploadPK"])) { int.TryParse(Request.QueryString["UserFileUploadPK"], out filePK); } else if (!string.IsNullOrWhiteSpace(Request.QueryString["ReportCatalogPK"])) { int.TryParse(Request.QueryString["ReportCatalogPK"], out reportCatalogPK); } //Get the file information from the database if (filePK > 0) { using (PyramidContext context = new PyramidContext()) { //Get the file record currentFile = context.UserFileUpload.AsNoTracking() .Where(ufu => ufu.UserFileUploadPK == filePK) .FirstOrDefault(); //Check to see if the file record exists if (currentFile == null) { //The file record doesn't exist, set to a default currentFile = new UserFileUpload(); } } } else { currentFile = new UserFileUpload(); } if (reportCatalogPK > 0) { using (PyramidContext context = new PyramidContext()) { //Get the report catalog record currentReportCatalog = context.ReportCatalog.AsNoTracking() .Where(rc => rc.ReportCatalogPK == reportCatalogPK) .FirstOrDefault(); //Check to see if the report catalog record exists if (currentReportCatalog == null) { //The report catalog record doesn't exist, set to a default currentReportCatalog = new ReportCatalog(); } } } else { currentReportCatalog = new ReportCatalog(); } //Don't allow users to view files from other programs if (currentFile.UserFileUploadPK > 0) { if (currentFile.TypeCodeFK == (int)Utilities.FileTypeFKs.STATE_WIDE && currentProgramRole.StateFK.Value != currentFile.StateFK.Value) { //This is a state-wide file and the user is not logged in under that state lblMessage.Text = "No file found..."; } else if (currentFile.TypeCodeFK == (int)Utilities.FileTypeFKs.HUB_WIDE && currentProgramRole.HubFK.Value != currentFile.HubFK.Value) { //This is a hub-wide file and the user is not logged in under that hub lblMessage.Text = "No file found..."; } else if (currentFile.TypeCodeFK == (int)Utilities.FileTypeFKs.PROGRAM_WIDE && !currentProgramRole.ProgramFKs.Contains(currentFile.ProgramFK.Value)) { //This is a program-wide file and the user is not allowed to see that cohort lblMessage.Text = "No file found..."; } else if (currentFile.TypeCodeFK == (int)Utilities.FileTypeFKs.COHORT_WIDE && !currentProgramRole.CohortFKs.Contains(currentFile.CohortFK.Value)) { //This is a cohort-wide file and the user is not allowed to see that cohort lblMessage.Text = "No file found..."; } else { //Get the file URL from Azure storage string fileLink = Utilities.GetFileLinkFromAzureStorage(currentFile.FileName, currentFile.FileName.Contains(".pdf"), Utilities.ConstantAzureStorageContainerName.UPLOADED_FILES.ToString()); //Redirect the user to the file link Response.Redirect(fileLink); } } else if (!string.IsNullOrWhiteSpace(currentReportCatalog.DocumentationLink)) { //Get the file path string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/'); string filePath = currentReportCatalog.DocumentationLink.Replace("~", baseUrl); //Redirect the user to the file link Response.Redirect(filePath); } else { lblMessage.Text = "No file found..."; } }
/// <summary> /// This method executes when the user clicks the save button for the UserFileUploads /// and it saves the UserFileUpload information to the database /// </summary> /// <param name="sender">The submitUserFileUpload submit user control</param> /// <param name="e">The Click event</param> protected void submitFileUpload_Click(object sender, EventArgs e) { //Allow editors and hub data viewers to add files if (currentProgramRole.AllowedToEdit.Value || currentProgramRole.RoleFK.Value == (int)Utilities.ProgramRoleFKs.HUB_DATA_VIEWER) { //Get the file to upload UploadedFile file = bucUploadFile.UploadedFiles[0]; if (file.ContentLength > 0 && file.IsValid) { //Get the actual file name string actualFileName = Path.GetFileNameWithoutExtension(file.FileName) + "-" + Path.GetRandomFileName().Substring(0, 6) + Path.GetExtension(file.FileName); //Get the display file name string displayFileName = Path.GetFileNameWithoutExtension(file.FileName); //Get the file type string fileExtension = Path.GetExtension(file.FileName).ToLower(); string fileType; switch (fileExtension) { case ".pdf": fileType = "pdf"; break; case ".doc": case ".docx": fileType = "word"; break; case ".ppt": case ".pptx": fileType = "powerpoint"; break; case ".xls": case ".xlsx": fileType = "excel"; break; case ".jpeg": case ".jpg": case ".png": fileType = "image"; break; default: fileType = "alt"; break; } //Upload the file to Azure storage string filePath = Utilities.UploadFileToAzureStorage(file.FileBytes, actualFileName, Utilities.ConstantAzureStorageContainerName.UPLOADED_FILES.ToString()); if (!String.IsNullOrWhiteSpace(filePath)) { PyramidUser currentUser = null; // Validate the user password using (var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>()) { //Try to get the user currentUser = manager.FindByName(User.Identity.Name); } using (PyramidContext context = new PyramidContext()) { //Create the database object for the file UserFileUpload currentUserFileUpload = new UserFileUpload(); currentUserFileUpload.CreateDate = DateTime.Now; currentUserFileUpload.Creator = User.Identity.Name; currentUserFileUpload.UploadedBy = currentUser.FirstName + " " + currentUser.LastName; currentUserFileUpload.Description = txtFileDescription.Value.ToString(); currentUserFileUpload.FileType = fileType; currentUserFileUpload.DisplayFileName = displayFileName; currentUserFileUpload.FileName = actualFileName; currentUserFileUpload.FilePath = filePath; currentUserFileUpload.TypeCodeFK = Convert.ToInt32(ddFileType.Value); //Set the proper FKs if (currentUserFileUpload.TypeCodeFK == (int)Utilities.FileTypeFKs.PROGRAM_WIDE) { currentUserFileUpload.ProgramFK = Convert.ToInt32(ddProgram.Value); currentUserFileUpload.HubFK = null; currentUserFileUpload.StateFK = null; currentUserFileUpload.CohortFK = null; } else if (currentUserFileUpload.TypeCodeFK == (int)Utilities.FileTypeFKs.HUB_WIDE) { currentUserFileUpload.ProgramFK = null; currentUserFileUpload.HubFK = Convert.ToInt32(ddHub.Value); currentUserFileUpload.StateFK = null; currentUserFileUpload.CohortFK = null; } else if (currentUserFileUpload.TypeCodeFK == (int)Utilities.FileTypeFKs.STATE_WIDE) { currentUserFileUpload.ProgramFK = null; currentUserFileUpload.HubFK = null; currentUserFileUpload.StateFK = Convert.ToInt32(ddState.Value); currentUserFileUpload.CohortFK = null; } else if (currentUserFileUpload.TypeCodeFK == (int)Utilities.FileTypeFKs.COHORT_WIDE) { currentUserFileUpload.ProgramFK = null; currentUserFileUpload.HubFK = null; currentUserFileUpload.StateFK = null; currentUserFileUpload.CohortFK = Convert.ToInt32(ddCohort.Value); } //Save to the database context.UserFileUpload.Add(currentUserFileUpload); context.SaveChanges(); //Redirect the user back to this page with a message Response.Redirect("/Pages/UploadedFiles.aspx?messageType=UploadSuccess"); } } else { msgSys.ShowMessageToUser("danger", "Upload Failed", "The file failed to upload properly, please try again.", 10000); } } else { msgSys.ShowMessageToUser("danger", "Error", "No valid file was selected to be uploaded!", 120000); } } else { msgSys.ShowMessageToUser("danger", "Error", "You are not authorized to make changes!", 120000); } }