Beispiel #1
0
        /// <summary>
        /// "Decompiles" the data
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="stream">Stream</param>
        /// <param name="name">Name</param>
        /// <returns></returns>
        public static bool Decompile(IDecompileNodeContext context, Stream stream, string name)
        {
            if (stream == null || stream.Length > 500 * 1024)
            {
                return(false);
            }

            stream.Position = 0;
            FileType type = GuessFileType.DetectFileType(stream);

            if (type == FileType.Binary)
            {
                return(false);
            }

            stream.Position = 0;
            context.Output.Write(new StreamReader(stream, true).ReadToEnd(), BoxedTextColor.Text);
            string ext;

            if (type == FileType.Xml)
            {
                ext = ".xml";
            }
            else
            {
                try {
                    ext = Path.GetExtension(NameUtilities.CleanName(name));
                }
                catch (ArgumentException) {
                    ext = ".txt";
                }
            }
            context.ContentTypeString = ContentTypes.TryGetContentTypeStringByExtension(ext) ?? ContentTypes.PlainText;
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the string contents of <paramref name="stream"/> if it's text, else null is returned
        /// </summary>
        /// <param name="stream">Stream</param>
        /// <returns></returns>
        public static string TryGetString(Stream stream)
        {
            if (stream == null)
            {
                return(null);
            }

            stream.Position = 0;
            if (GuessFileType.DetectFileType(stream) == FileType.Binary)
            {
                return(null);
            }

            stream.Position = 0;
            return(new StreamReader(stream, true).ReadToEnd());
        }