/// <summary> /// Processes the specified file and returns the data to the output stream. /// </summary> /// <param name="attachmentGuid">Attachment guid</param> protected void ProcessFile(Guid attachmentGuid) { AttachmentInfo atInfo = null; bool requiresData = true; // Check if it is necessary to load the file data if (useClientCache && IsLiveSite && AllowClientCache) { // If possibly cached by client, do not load data (may not be sent) string ifModifiedString = Request.Headers["If-Modified-Since"]; if (ifModifiedString != null) { requiresData = false; } } // If output data available from cache, do not require loading the data byte[] cachedData = GetCachedOutputData(); if (cachedData != null) { requiresData = false; } // Get AttachmentInfo object if (!IsLiveSite) { // Not livesite mode - get latest version if (node != null) { atInfo = DocumentHelper.GetAttachment(node, attachmentGuid, TreeProvider, true); } else { atInfo = DocumentHelper.GetAttachment(attachmentGuid, TreeProvider, CurrentSiteName); } } else { if (!requiresData || AttachmentManager.StoreFilesInFileSystem(CurrentSiteName)) { // Do not require data from DB - Not necessary or available from file system atInfo = AttachmentManager.GetAttachmentInfoWithoutBinary(attachmentGuid, CurrentSiteName); } else { // Require data from DB - Stored in DB atInfo = AttachmentManager.GetAttachmentInfo(attachmentGuid, CurrentSiteName); } // If attachment not found, if (allowLatestVersion && ((atInfo == null) || (latestForHistoryId > 0) || (atInfo.AttachmentDocumentID == latestForDocumentId))) { // Get latest version if (node != null) { atInfo = DocumentHelper.GetAttachment(node, attachmentGuid, TreeProvider, true); } else { atInfo = DocumentHelper.GetAttachment(attachmentGuid, TreeProvider, CurrentSiteName); } // If not attachment for the required document, do not return if ((atInfo.AttachmentDocumentID != latestForDocumentId) && (latestForHistoryId == 0)) { atInfo = null; } else { mIsLatestVersion = true; } } } if (atInfo != null) { // Temporary attachment is always latest version if (atInfo.AttachmentFormGUID != Guid.Empty) { mIsLatestVersion = true; } bool checkPublishedFiles = AttachmentManager.CheckPublishedFiles(CurrentSiteName); bool checkFilesPermissions = AttachmentManager.CheckFilesPermissions(CurrentSiteName); // Get the document node if ((node == null) && (checkPublishedFiles || checkFilesPermissions)) { // Try to get data from cache using (CachedSection <TreeNode> cs = new CachedSection <TreeNode>(ref node, CacheMinutes, !allowLatestVersion, null, "getfilenodebydocumentid", atInfo.AttachmentDocumentID)) { if (cs.LoadData) { // Get the document node = TreeProvider.SelectSingleDocument(atInfo.AttachmentDocumentID, false); // Cache the document CacheNode(cs, node); } } } bool secured = false; if ((node != null) && checkFilesPermissions) { secured = (node.IsSecuredNode == 1); // Check secured pages if (secured) { URLRewriter.CheckSecuredAreas(CurrentSiteName, false, ViewMode); } if (node.RequiresSSL == 1) { URLRewriter.RequestSecurePage(false, node.RequiresSSL, ViewMode, CurrentSiteName); } // Check permissions bool checkPermissions = false; switch (URLRewriter.CheckPagePermissions(CurrentSiteName)) { case PageLocationEnum.All: checkPermissions = true; break; case PageLocationEnum.SecuredAreas: checkPermissions = secured; break; } // Check the read permission for the page if (checkPermissions) { if (CurrentUser.IsAuthorizedPerDocument(node, NodePermissionsEnum.Read) == AuthorizationResultEnum.Denied) { URLHelper.Redirect(URLRewriter.AccessDeniedPageURL(CurrentSiteName)); } } } bool resizeImage = (ImageHelper.IsImage(atInfo.AttachmentExtension) && AttachmentManager.CanResizeImage(atInfo, Width, Height, MaxSideSize)); // If the file should be redirected, redirect the file if (!mIsLatestVersion && IsLiveSite && SettingsKeyProvider.GetBoolValue(CurrentSiteName + ".CMSRedirectFilesToDisk")) { if (AttachmentManager.StoreFilesInFileSystem(CurrentSiteName)) { string path = null; if (!resizeImage) { path = AttachmentManager.GetFilePhysicalURL(CurrentSiteName, atInfo.AttachmentGUID.ToString(), atInfo.AttachmentExtension); } else { int[] newDim = ImageHelper.EnsureImageDimensions(Width, Height, MaxSideSize, atInfo.AttachmentImageWidth, atInfo.AttachmentImageHeight); path = AttachmentManager.GetFilePhysicalURL(CurrentSiteName, atInfo.AttachmentGUID.ToString(), atInfo.AttachmentExtension, newDim[0], newDim[1]); } // If path is valid, redirect if (path != null) { // Check if file exists string filePath = Server.MapPath(path); if (File.Exists(filePath)) { outputFile = NewOutputFile(); outputFile.IsSecured = secured; outputFile.RedirectTo = path; outputFile.Attachment = atInfo; } } } } // Get the data if ((outputFile == null) || (outputFile.Attachment == null)) { outputFile = NewOutputFile(atInfo, null); outputFile.Width = Width; outputFile.Height = Height; outputFile.MaxSideSize = MaxSideSize; outputFile.SiteName = CurrentSiteName; outputFile.Resized = resizeImage; // Load the data if required if (requiresData) { // Try to get the physical file, if not latest version if (!mIsLatestVersion) { EnsurePhysicalFile(outputFile); } bool loadData = string.IsNullOrEmpty(outputFile.PhysicalFile); // Load data if necessary if (loadData) { if (atInfo.AttachmentBinary != null) { // Load from the attachment outputFile.LoadData(atInfo.AttachmentBinary, AttachmentManager); } else { // Load from the disk byte[] data = AttachmentManager.GetFile(atInfo, CurrentSiteName); outputFile.LoadData(data, AttachmentManager); } // Save data to the cache, if not latest version if (!mIsLatestVersion && (CacheMinutes > 0)) { SaveOutputDataToCache(outputFile.OutputData, GetOutputDataDependency(outputFile.Attachment)); } } } else if (cachedData != null) { // Load the cached data if available outputFile.OutputData = cachedData; } } if (outputFile != null) { outputFile.IsSecured = secured; // Add node data if (node != null) { outputFile.AliasPath = node.NodeAliasPath; outputFile.CultureCode = node.DocumentCulture; outputFile.FileNode = node; // Set the file validity if (IsLiveSite && !mIsLatestVersion && checkPublishedFiles) { outputFile.ValidFrom = ValidationHelper.GetDateTime(node.GetValue("DocumentPublishFrom"), DateTime.MinValue); outputFile.ValidTo = ValidationHelper.GetDateTime(node.GetValue("DocumentPublishTo"), DateTime.MaxValue); // Set the published flag outputFile.IsPublished = node.IsPublished; } } } } }
/// <summary> /// Processes the specified document node. /// </summary> /// <param name="treeNode">Document node to process</param> /// <param name="columnName">Column name</param> /// <param name="processedFileName">File name</param> protected void ProcessNode(TreeNode treeNode, string columnName, string processedFileName) { if (treeNode != null) { // Check if latest or live site version is required bool latest = !IsLiveSite; if (allowLatestVersion && ((treeNode.DocumentID == latestForDocumentId) || (treeNode.DocumentCheckedOutVersionHistoryID == latestForHistoryId))) { latest = true; } // If not published, return no content if (!latest && !treeNode.IsPublished) { outputFile = NewOutputFile(null, null); outputFile.AliasPath = treeNode.NodeAliasPath; outputFile.CultureCode = treeNode.DocumentCulture; if (IsLiveSite && AttachmentManager.CheckPublishedFiles(CurrentSiteName)) { outputFile.IsPublished = treeNode.IsPublished; } outputFile.FileNode = treeNode; outputFile.Height = Height; outputFile.Width = Width; outputFile.MaxSideSize = MaxSideSize; } else { // Get valid site name if link if (treeNode.IsLink) { TreeNode origNode = TreeProvider.GetOriginalNode(treeNode); if (origNode != null) { SiteInfo si = SiteInfoProvider.GetSiteInfo(origNode.NodeSiteID); if (si != null) { CurrentSiteName = si.SiteName; } } } // Process the node // Get from specific column if (String.IsNullOrEmpty(columnName) && String.IsNullOrEmpty(processedFileName) && treeNode.NodeClassName.Equals("CMS.File", StringComparison.InvariantCultureIgnoreCase)) { columnName = "FileAttachment"; } if (!String.IsNullOrEmpty(columnName)) { // File document type or specified by column Guid attachmentGuid = ValidationHelper.GetGuid(treeNode.GetValue(columnName), Guid.Empty); if (attachmentGuid != Guid.Empty) { ProcessFile(attachmentGuid); } } else { // Get by file name if (processedFileName == null) { // CMS.File - Get Guid attachmentGuid = ValidationHelper.GetGuid(treeNode.GetValue("FileAttachment"), Guid.Empty); if (attachmentGuid != Guid.Empty) { ProcessFile(attachmentGuid); } } else { // Other document types, get the attachment by file name AttachmentInfo ai = null; if (latest) { // Not livesite mode - get latest version ai = DocumentHelper.GetAttachment(treeNode, processedFileName, TreeProvider, false); } else { // Live site mode, get directly from database ai = AttachmentManager.GetAttachmentInfo(treeNode.DocumentID, processedFileName, false); } if (ai != null) { ProcessFile(ai.AttachmentGUID); } } } } } }
/// <summary> /// Processes the specified version of the file and returns the data to the output stream. /// </summary> /// <param name="attachmentGuid">Attachment GUID</param> /// <param name="versionHistoryId">Document version history ID</param> protected void ProcessFile(Guid attachmentGuid, int versionHistoryId) { AttachmentInfo atInfo = GetFile(attachmentGuid, versionHistoryId); if (atInfo != null) { // If attachment is image, try resize byte[] mFile = atInfo.AttachmentBinary; if (mFile != null) { string mimetype = null; if (ImageHelper.IsImage(atInfo.AttachmentExtension)) { if (AttachmentManager.CanResizeImage(atInfo, Width, Height, MaxSideSize)) { // Do not search thumbnail on the disk mFile = AttachmentManager.GetImageThumbnail(atInfo, CurrentSiteName, Width, Height, MaxSideSize, false); mimetype = "image/jpeg"; } } if (mFile != null) { outputFile = NewOutputFile(atInfo, mFile); } else { outputFile = NewOutputFile(); } outputFile.Height = Height; outputFile.Width = Width; outputFile.MaxSideSize = MaxSideSize; outputFile.MimeType = mimetype; } // Get the file document if (node == null) { node = TreeProvider.SelectSingleDocument(atInfo.AttachmentDocumentID); } if (node != null) { // Check secured area SiteInfo si = SiteInfoProvider.GetSiteInfo(node.NodeSiteID); if (si != null) { if (pi == null) { pi = PageInfoProvider.GetPageInfo(si.SiteName, node.NodeAliasPath, node.DocumentCulture, node.DocumentUrlPath, false); } if (pi != null) { URLRewriter.RequestSecurePage(pi, false, ViewMode, CurrentSiteName); URLRewriter.CheckSecuredAreas(CurrentSiteName, pi, false, ViewMode); } } // Check the permissions for the document if ((CurrentUser.IsAuthorizedPerDocument(node, NodePermissionsEnum.Read) == AuthorizationResultEnum.Allowed) || (node.NodeOwner == CurrentUser.UserID)) { if (outputFile == null) { outputFile = NewOutputFile(); } outputFile.AliasPath = node.NodeAliasPath; outputFile.CultureCode = node.DocumentCulture; if (IsLiveSite && AttachmentManager.CheckPublishedFiles(CurrentSiteName)) { outputFile.IsPublished = node.IsPublished; } outputFile.FileNode = node; } else { outputFile = null; } } } }