public ITextBuffer Create(FileName fileName) { try { foreach (FileName name in viewContentFileNamesCollection) { if (FileUtility.IsEqualFileName(name, fileName)) { ITextBuffer buffer = WorkbenchSingleton.SafeThreadFunction(ReadFile, fileName); if (buffer != null) { return(buffer); } } } using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { if (MimeTypeDetection.FindMimeType(stream).StartsWith("text/")) { stream.Position = 0; return(new StringTextBuffer(ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(stream, Encoding.Default))); } } return(null); } catch (IOException) { return(null); } catch (UnauthorizedAccessException) { return(null); } }
public ITextSource Create(FileName fileName) { try { foreach (FileName name in viewContentFileNamesCollection) { if (FileUtility.IsEqualFileName(name, fileName)) { ITextSource buffer = SD.MainThread.InvokeIfRequired(() => ReadFile(fileName)); if (buffer != null) { return(buffer); } } } using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { if (MimeTypeDetection.FindMimeType(stream).StartsWith("text/", StringComparison.Ordinal)) { stream.Position = 0; return(new StringTextSource(ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(stream, SD.FileService.DefaultFileEncoding))); } } return(null); } catch (IOException) { return(null); } catch (UnauthorizedAccessException) { return(null); } }
public IViewContent CreateContentForFile(OpenedFile file) { var codons = SD.DisplayBindingService.GetCodonsPerFileName(file.FileName); DisplayBindingDescriptor bestMatch = null; double max = double.NegativeInfinity; const int BUFFER_LENGTH = 4 * 1024; using (var stream = file.OpenRead()) { string mime = "text/plain"; if (stream.Length > 0) { stream.Position = 0; mime = MimeTypeDetection.FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH)); } foreach (var codon in codons) { stream.Position = 0; double value = codon.Binding.AutoDetectFileContent(file.FileName, stream, mime); if (value > max) { max = value; bestMatch = codon; } } } if (bestMatch == null) { throw new InvalidOperationException(); } return(bestMatch.Binding.CreateContentForFile(file)); }
/// <summary> /// Dumps the contents of the specified file in the HTTP response. /// Returns an error if there is a problem in opening/closing the file. /// </summary> /// <param name="path">The path.</param> /// <param name="context">Http context to respond to.</param> private static void DumpFile(string path, HttpListenerContext context) { try { string mimeType = MimeTypeDetection.GetMimeFromFile(path); context.Response.ContentType = mimeType; //// context.Response.SendChunked = true; using (FileStream handle = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { handle.CopyTo(context.Response.OutputStream); } } finally { context.Response.Close(); } }
void TestMime(byte[] bytes, string expectedMime) { Assert.AreEqual(expectedMime, MimeTypeDetection.FindMimeType(bytes)); }
/// <summary> /// Detects the type of the content in a file. /// </summary> /// <param name="path">The path to the file.</param> /// <returns>A MIME type.</returns> private static string DetectContentType(string path) { return(MimeTypeDetection.GetMimeFromFile(path)); }