/// <summary> /// Returns true if the tildeFilePath corresponds to a file that /// exists and is a text file. /// /// In that case, the filePath and info parameters are initialized. /// </summary> /// <param name="server">The server to convert to real file paths</param> /// <param name="tildeFilePath">The tilde file path</param> /// <param name="filePath">The real file path</param> /// <param name="info">The FileInfo object</param> public static bool TildeFilePathExistsAndIsText (HttpServerUtility server, string tildeFilePath, ref string filePath, ref FileInfo info) { bool error = StringTools.IsTrivial(tildeFilePath); if (!error) { error = !tildeFilePath.StartsWith("~/"); } if (!error) { int category = FileTools.GetFileCategory(tildeFilePath); error = category != FileTools.TEXT; } if (!error) { try { filePath = server.MapPath(tildeFilePath); info = new FileInfo(filePath); long bytes = info.Length; } catch { error = true; } } return(!error); }
/// <summary> /// Returns the response category as one of three values /// FileTools.TEXT /// FileTools.IMAGE /// FileTools.OTHER /// /// First tests the response object. /// /// If response.ContentType determines that the value is /// TEXT or IMAGE then that value is returned. /// /// Otherwise, uri.FileExtension is tested using /// FileTools.GetFileCategory /// /// If that determines the answer then that is returned. /// /// Otherwise, FileTools.OTHER is returned. /// /// Assumption: The response is the result of requesting /// the uri. /// </summary> /// <param name="uri">The uri to request</param> /// <param name="response">The response to the request</param> public static int ResponseCategory (UriPlus uri, HttpWebResponse response) { int category = FileTools.OTHER; // First test response.ContentType if (response != null) { string contentType = response.ContentType; if (!StringTools.IsTrivial(contentType)) { if (contentType.StartsWith("text")) { category = FileTools.TEXT; } else if (contentType.StartsWith("image")) { category = FileTools.IMAGE; } else if (contentType.Contains("javascript")) { category = FileTools.TEXT; } else if (contentType.Contains("json")) { category = FileTools.TEXT; } else if (contentType.Contains("htm")) { category = FileTools.TEXT; } else if (contentType.StartsWith("application/xml")) { category = FileTools.TEXT; } } } // Only test uri.FileExtension if category did not change if (category == FileTools.OTHER) { if (uri != null) { string extension = uri.FileExtension; if (!StringTools.IsTrivial(extension)) { category = FileTools.GetFileCategory(extension); } } } return(category); }
private void CommonCode(HttpServerUtility server, string tildeFilePath) { _path = tildeFilePath; if (_path[0] != '~') { return; } if (!SourceTools.OKtoServe(tildeFilePath, true)) { return; } try { string filePath = server.MapPath(tildeFilePath); FileInfo info = new FileInfo(filePath); _size = info.Length; _date = info.MostRecentTime(); _valid = true; int category = FileTools.GetFileCategory(_path); bool image = category == FileTools.IMAGE; if (image) { using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { try { Bitmap bitmap = new Bitmap(stream); _width = bitmap.Width; _height = bitmap.Height; } catch { } } } } catch { } }
/// <summary> /// Returns a list of Range objects representing /// the matches of the pattern in the content of /// a text file with the given tilde file path. /// /// If successful, returns a reference to the /// text file content via an out parameter. /// /// If isRegex is true, /// uses regular expression matching, /// otherwise uses plain text matching. /// /// Ignores case if ignoreCase is true. /// /// Returns null if the file is not a text file. /// /// Precondition: The page that calls this method must: /// /// Be able to guarantee that the file is OK to serve /// in context. /// </summary> /// <param name="page"> /// The page object that uses this method</param> /// <param name="tildeFilePath"> /// The tilde file path</param> /// <param name="pattern"> /// The search pattern</param> /// <param name="isRegex"> /// If true use regex matching</param> /// <param name="ignoreCase"> /// If true ignore case</param> /// <param name="content"> /// Out param: If the file is a text file /// then returns a reference to its content; /// otherwise returns null.</param> public static List <Range> SearchFileContent (Page page, string tildeFilePath, string pattern, bool isRegex, bool ignoreCase, out string content) { content = null; int category = FileTools.GetFileCategory(tildeFilePath); if (category != FileTools.TEXT) { return(null); } string filePath = page.MapPath(tildeFilePath); content = FileTools.GetFileAsText(filePath); return(Search(content, pattern, 0, isRegex, ignoreCase)); }
// ToHtml requires a knowledge of the page on which // the html will be installed in order to properly // compute relative links. public string ToHtml(Page page) { if (!_valid) { return("<p><code>" + _path + "</code>" + errormessage + "</p>\n"); } StringBuilder builder = new StringBuilder(); // Show tilde file path // Link to the file view utility if viewable int category = FileTools.GetFileCategory(_path); bool viewable = category != FileTools.OTHER; builder.Append("<p>"); if (viewable) { string FileViewTildePath = SourceTools.FileViewTildePath; string fileViewURL = FileTools.GetRelativePath(page, FileViewTildePath); builder.Append("<a href ='"); builder.Append(fileViewURL); builder.Append("?"); builder.Append(_path); builder.Append("' target='_blank'>"); } builder.Append("<code>"); builder.Append(_path); builder.Append("</code>"); if (viewable) { builder.Append("</a>"); } builder.Append("</p>\n"); // Add launch link if servable bool servable = HttpContextTools.IsServable(_path); if (servable) { string launchURL = FileTools.GetRelativePath(page, _path); builder.Append("<p>Launch "); builder.Append("<a href ='"); builder.Append(launchURL); builder.Append("' target='_blank'>"); builder.Append("<code>"); builder.Append(_path); builder.Append("</code>"); builder.Append("</a>"); builder.Append("</p>\n"); } builder.Append("<table class='filedata'>\n"); // File size builder.Append("<tr>\n"); builder.Append("<td>"); builder.Append("Size"); builder.Append("</td>\n"); builder.Append("<td>"); builder.Append(_size.ToString()); builder.Append("</td>\n"); builder.Append("</tr>\n"); // File date builder.Append("<tr>\n"); builder.Append("<td>"); builder.Append("Date"); builder.Append("</td>\n"); builder.Append("<td>"); builder.Append(_date.ToYMD()); builder.Append("</td>\n"); builder.Append("<td>"); builder.Append(_date.ToHMS()); builder.Append("</td>\n"); builder.Append("</tr>\n"); // Add image dimensions if applicable bool image = (_width > 0) && (_height > 0); if (image) { builder.Append("<tr>\n"); builder.Append("<td>"); builder.Append("Dims"); builder.Append("</td>\n"); builder.Append("<td>"); builder.Append(_width.ToString()); builder.Append(" "); builder.Append("×"); builder.Append(" "); builder.Append(_height.ToString()); builder.Append("</td>\n"); builder.Append("</tr>\n"); } builder.Append("</table>\n"); return(builder.ToString()); }