protected void rptItems_DataBinding(object sender, EventArgs e) { provider = Collectors.CollectorsBox.FilesProviderCollector.GetProvider(lstProviders.SelectedValue, Tools.DetectCurrentWiki()); if (provider == null || CurrentPage == null) { return; } // Build a DataTable containing the proper information DataTable table = new DataTable("Items"); table.Columns.Add("Name"); table.Columns.Add("Size"); table.Columns.Add("Editable", typeof(bool)); table.Columns.Add("Page"); table.Columns.Add("Link"); table.Columns.Add("CanDelete", typeof(bool)); table.Columns.Add("CanDownload", typeof(bool)); string[] attachments = provider.ListPageAttachments(CurrentPage.FullName); foreach (string s in attachments) { FileDetails details = provider.GetPageAttachmentDetails(CurrentPage.FullName, s); DataRow row = table.NewRow(); string ext = Path.GetExtension(s).ToLowerInvariant(); row["Name"] = s; row["Size"] = Tools.BytesToString(details.Size); row["Editable"] = canUpload && canDelete && (ext == ".jpg" || ext == ".jpeg" || ext == ".png"); row["Page"] = CurrentPage.FullName; if (canDownload) { row["Link"] = "GetFile.aspx?File=" + Tools.UrlEncode(s).Replace("'", "'") + "&AsStreamAttachment=1&Provider=" + provider.GetType().FullName + "&IsPageAttachment=1&Page=" + Tools.UrlEncode(CurrentPage.FullName) + "&NoHit=1"; } else { row["Link"] = ""; } row["CanDelete"] = canDelete; row["CanDownload"] = canDownload; table.Rows.Add(row); } rptItems.DataSource = table; }
private List <TreeElement> BuildFilesSubTree(IFilesStorageProviderV40 provider, string path) { string[] dirs = new string[0]; string[] files = new string[0]; if (chkFilesAttachments.Checked) { // Load page attachments files = provider.ListPageAttachments(currentPage.FullName); } else { // Load files dirs = provider.ListDirectories(path); files = provider.ListFiles(path); } List <TreeElement> result = new List <TreeElement>(100); foreach (string d in dirs) { TreeElement item = new TreeElement(d, Tools.ExtractDirectoryName(d), BuildFilesSubTree(provider, d)); // Do not display empty folders to reduce "noise" if (item.SubItems.Count > 0) { result.Add(item); } } foreach (string f in files) { long size = chkFilesAttachments.Checked ? provider.GetPageAttachmentDetails(currentPage.FullName, f).Size : provider.GetFileDetails(f).Size; TreeElement item = new TreeElement(f, f.Substring(f.LastIndexOf("/") + 1) + " (" + Tools.BytesToString(size) + ")", "javascript:return SelectFile('" + (chkFilesAttachments.Checked ? "(" + Tools.UrlEncode(currentPage.FullName) + ")" : "") + "', '" + f.Replace("'", "\\\\\\'") + "');"); result.Add(item); } return(result); }
protected void Page_Load(object sender, EventArgs e) { string currentWiki = Tools.DetectCurrentWiki(); string filename = Request["File"]; if (string.IsNullOrEmpty(filename)) { Response.Write("No file specified."); return; } // Remove ".." sequences that might be a security issue filename = filename.Replace("..", ""); string page = Request["Page"]; PageContent pageContent = Pages.FindPage(currentWiki, page); bool isPageAttachment = !string.IsNullOrEmpty(page); if (isPageAttachment && pageContent == null) { Response.StatusCode = 404; Response.Write("File not found."); return; } IFilesStorageProviderV40 provider = null; if (!string.IsNullOrEmpty(Request["Provider"])) { provider = Collectors.CollectorsBox.FilesProviderCollector.GetProvider(Request["Provider"], currentWiki); } else { if (isPageAttachment) { provider = FilesAndAttachments.FindPageAttachmentProvider(currentWiki, pageContent.FullName, filename); } else { provider = FilesAndAttachments.FindFileProvider(currentWiki, filename); } } if (provider == null) { Response.StatusCode = 404; Response.Write("File not found."); return; } string size = Request["Size"]; if (string.IsNullOrEmpty(size)) { size = "small"; } size = size.ToLowerInvariant(); // Verify permissions bool canDownload = false; AuthChecker authChecker = new AuthChecker(Collectors.CollectorsBox.GetSettingsProvider(currentWiki)); if (pageContent != null) { canDownload = authChecker.CheckActionForPage(pageContent.FullName, Actions.ForPages.DownloadAttachments, SessionFacade.GetCurrentUsername(), SessionFacade.GetCurrentGroupNames(currentWiki)); } else { string dir = Tools.GetDirectoryName(filename); canDownload = authChecker.CheckActionForDirectory(provider, dir, Actions.ForDirectories.DownloadFiles, SessionFacade.GetCurrentUsername(), SessionFacade.GetCurrentGroupNames(currentWiki)); } if (!canDownload) { Response.StatusCode = 401; } // Contains the image bytes MemoryStream ms = new MemoryStream(1048576); long fileSize = 0; // Load from provider if (string.IsNullOrEmpty(page)) { bool retrieved = false; try { retrieved = provider.RetrieveFile(filename, ms); } catch (ArgumentException ex) { Log.LogEntry("Attempted to create thumb of inexistent file (" + filename + ")\n" + ex.ToString(), EntryType.Warning, Log.SystemUsername, currentWiki); } if (!retrieved) { Response.StatusCode = 404; Response.Write("File not found."); return; } fileSize = provider.GetFileDetails(filename).Size; } else { if (pageContent == null) { Response.StatusCode = 404; Response.Write("Page not found."); return; } bool retrieved = false; try { retrieved = provider.RetrievePageAttachment(pageContent.FullName, filename, ms); } catch (ArgumentException ex) { Log.LogEntry("Attempted to create thumb of inexistent attachment (" + page + "/" + filename + ")\n" + ex.ToString(), EntryType.Warning, Log.SystemUsername, currentWiki); } if (!retrieved) { Response.StatusCode = 404; Response.Write("File not found."); return; } fileSize = provider.GetPageAttachmentDetails(pageContent.FullName, filename).Size; } ms.Seek(0, SeekOrigin.Begin); int rotation = 0; int.TryParse(Request["Rot"], out rotation); // Load the source image System.Drawing.Image source = System.Drawing.Image.FromStream(ms); // Destination bitmap Bitmap result = null; System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb; if (size == "big") { // Big thumb (outer size 200x200) result = new Bitmap(200, 200, pixelFormat); } else if (size == "imgeditprev") { // Image Editor Preview thumb (outer size from Request["dim"], if null 200x200) if (!string.IsNullOrEmpty(Request["Width"]) && !string.IsNullOrEmpty(Request["Height"])) { try { result = new Bitmap( rotation != 90 && rotation != 270 ? int.Parse(Request["Width"]) : int.Parse(Request["Height"]), rotation != 90 && rotation != 270 ? int.Parse(Request["Height"]) : int.Parse(Request["Width"]), pixelFormat); } catch (FormatException) { result = new Bitmap(200, 200, pixelFormat); } } else { result = new Bitmap(200, 200, pixelFormat); } } else { // Small thumb (outer size 48x48) result = new Bitmap(48, 48, pixelFormat); } // Get Graphics object for destination bitmap Graphics g = Graphics.FromImage(result); if (source.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { g.Clear(Color.Transparent); } else { g.Clear(Color.White); } g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; g.TranslateTransform(result.Width / 2, result.Height / 2); g.RotateTransform(rotation); g.TranslateTransform(-result.Width / 2, -result.Height / 2); // Draw bitmap g.DrawImage(source, GetImageRectangle(result.Width, result.Height, rotation != 90 && rotation != 270 ? source.Width : source.Height, rotation != 90 && rotation != 270 ? source.Height : source.Width, rotation == 90 || rotation == 270)); if (!string.IsNullOrEmpty(Request["Info"]) && size == "big") { // Draw image information RectangleF r = new RectangleF(0, 0, result.Width, 20); StringFormat f = new StringFormat(); f.Alignment = StringAlignment.Center; //f.LineAlignment = StringAlignment.Center; GraphicsPath path = new GraphicsPath(); path.AddString(string.Format("{0}x{1} - {2}", source.Width, source.Height, Tools.BytesToString(fileSize)), new FontFamily("Verdana"), 0, 12, new Point(result.Width / 2, 2), f); Pen pen = new Pen(Brushes.Black, 2F); g.DrawPath(pen, path); g.FillPath(Brushes.White, path); } // Write result in output stream in JPEG or PNG format if (source.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { Response.ContentType = "image/png"; } else { Response.ContentType = "image/jpeg"; } // This invariably throws an exception (A generic error occurred in GDI+) - an intermediate buffer is needed // The possible cause is that PNG format requires to read from the output stream, and Response.OutputStream does not support reading //result.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); MemoryStream tempStream = new MemoryStream(65536); // 32 KB if (source.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { result.Save(tempStream, System.Drawing.Imaging.ImageFormat.Png); } else { result.Save(tempStream, System.Drawing.Imaging.ImageFormat.Jpeg); } Response.OutputStream.Write(tempStream.ToArray(), 0, (int)tempStream.Length); tempStream.Dispose(); ms.Dispose(); source.Dispose(); g.Dispose(); result.Dispose(); }