Example #1
0
        private string processIO(string id, string type, string path, string folder, string folder_new, string file_name, string data)
        {
            string result = "{}", path_new, path_file = string.Empty;
            bool   isroot = false;

            if (string.IsNullOrEmpty(path))
            {
                path = PATH_ROOT;
            }
            path     = path.Replace('/', '\\');
            path_new = path;
            if (string.IsNullOrEmpty(folder))
            {
                isroot = true;
            }
            else
            {
                if (!type.Contains("_create"))
                {
                    path = Path.Combine(path, folder);
                }
            }

            if (!Directory.Exists(path))
            {
                return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "Cannot find path: " + path }));
            }

            switch (type)
            {
            case "dir_get":
                #region

                var dirs = Directory.GetDirectories(path).Select(x => new
                {
                    dir      = Path.GetFileName(x),
                    sum_file =
                        Directory.GetDirectories(x).Length +
                        "*.txt|*.html".Split('|').SelectMany(filter => System.IO.Directory.GetFiles(x, filter)).Count()
                        //Directory.GetFiles(x, "*.txt,*.html").Length + Directory.GetDirectories(x).Length
                }).ToArray();
                if (isroot)
                {
                    result = JsonConvert.SerializeObject(new
                    {
                        id    = id,
                        ok    = true,
                        path  = path.Replace('\\', '/'),
                        dirs  = dirs,
                        files = new string[] { }
                    });
                }
                else
                {
                    var files =
                        //Directory.GetFiles(path, "*.txt, *.html")
                        "*.txt|*.html".Split('|').SelectMany(filter => System.IO.Directory.GetFiles(path, filter))
                        .Select(x => new
                    {
                        file = Path.GetFileName(x),
                        //title = Regex.Replace(Regex.Replace(File.ReadAllLines(x)[0], "<.*?>", " "), "[ ]{2,}", " ").Trim()
                        title = File.ReadAllText(x).Split(new char[] { '\r', '\n' })[0]
                    }).ToArray();

                    result = JsonConvert.SerializeObject(new
                    {
                        id    = id,
                        ok    = true,
                        path  = path.Replace('\\', '/'),
                        dirs  = dirs,
                        files = files
                    });
                }
                #endregion
                break;

            case "dir_create":
                #region
                if (string.IsNullOrEmpty(folder_new))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "the field [folder_new] must be not empty" }));
                }
                try
                {
                    path = Path.Combine(path, folder_new);
                    Directory.CreateDirectory(path);
                    result = JsonConvert.SerializeObject(new { id = id, ok = true, path = path /*.Replace("\\", "/")*/, msg = "Create folder [" + folder_new + "] successfully." });
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, msg = "Create folder [" + folder_new + "] fail: " + ex.Message }));
                }
                #endregion
                break;

            case "dir_edit":
                #region
                if (string.IsNullOrEmpty(folder_new))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "the field [folder_new] must be not empty" }));
                }
                try
                {
                    path_new = Path.Combine(path_new, folder_new);
                    Directory.Move(path, path_new);
                    result = JsonConvert.SerializeObject(new { id = id, ok = true, path = path /*.Replace("\\", "/")*/, msg = "Rename folder [" + folder_new + "] successfully." });
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, msg = "Rename folder [" + folder_new + "] fail: " + ex.Message }));
                }
                #endregion
                break;

            case "dir_remove":
                #region
                if (string.IsNullOrEmpty(folder))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "The field [folder] must be not empty" }));
                }
                try
                {
                    Directory.Delete(path);
                    result = JsonConvert.SerializeObject(new { id = id, ok = true, path = path /*.Replace("\\", "/")*/, msg = "Remove folder [" + folder + "] successfully." });
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, msg = "Remove folder [" + folder + "] fail: " + ex.Message }));
                }
                #endregion
                break;

            case "file_load":
                #region

                if (string.IsNullOrEmpty(file_name))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "the field [file_name] must be not empty" }));
                }

                path_file = Path.Combine(path, file_name);

                if (!File.Exists(path_file))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "Cannot find file: " + path_file }));
                }
                string s = File.ReadAllText(path_file), ext = Path.GetExtension(path_file),
                       htm = string.Empty, text = string.Empty, word = string.Empty;
                string[] line  = new string[] { };
                word[]   aword = new word[] { };
                switch (ext)
                {
                case ".html":
                    text  = Regex.Replace(s, @"<[^>]*>", " ");
                    text  = Regex.Replace(s, "[^0-9a-zA-Z]+", " ").ToLower();
                    text  = Regex.Replace(text, "[ ]{2,}", " ").ToLower();
                    aword = text.Split(' ').Where(x => x.Length > 3)
                            .GroupBy(x => x)
                            .OrderByDescending(x => x.Count())
                            .Select(x => new word()
                    {
                        w = x.Key, k = x.Count()
                    })
                            .ToArray();
                    htm    = string.Format("<{0} class=ext_html>{1}</{0}>", EL.TAG_ARTICLE, s);
                    result = JsonConvert.SerializeObject(new { id = id, ok = true, extension = ext, text = text, html = htm, word = aword });
                    break;

                case ".txt":
                    line  = s.Split(new char[] { '\n', '\r' }).Where(x => x != string.Empty).ToArray();
                    text  = Regex.Replace(s, "[^0-9a-zA-Z]+", " ").ToLower();
                    text  = Regex.Replace(text, "[ ]{2,}", " ").ToLower();
                    aword = text.Split(' ').Where(x => x.Length > 3)
                            .GroupBy(x => x)
                            .OrderByDescending(x => x.Count())
                            .Select(x => new word()
                    {
                        w = x.Key, k = x.Count()
                    })
                            .ToArray();
                    htm = HtmlBuilder.renderFile(line);

                    result = JsonConvert.SerializeObject(new { id = id, ok = true, extension = ext, text = s, html = htm, word = aword });
                    break;
                }

                #endregion
                break;

            case "file_create":
                #region
                if (string.IsNullOrEmpty(file_name))
                {
                    file_name = DateTime.Now.ToString("yyMMddHHmmssfff") + ".txt";
                }
                try
                {
                    path_file = Path.Combine(path, file_name);
                    if (File.Exists(path_file))
                    {
                        string r = "_" + DateTime.Now.ToString("yyMMddHHmmssfff") + ".";
                        file_name = string.Join(r, file_name.Split('.'));
                        path_file = Path.Combine(path, file_name);
                    }

                    if (data == null)
                    {
                        data = string.Empty;
                    }
                    File.WriteAllText(path_file, data);

                    result = JsonConvert.SerializeObject(new { id = id, ok = true, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Create file [" + path_file + "] successfully." });
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Create file [" + path_file + "] fail: " + ex.Message }));
                }
                #endregion
                break;

            case "file_edit":
                #region
                if (string.IsNullOrEmpty(file_name))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "the field [file_name] must be not empty" }));
                }
                try
                {
                    path_file = Path.Combine(path, file_name);
                    if (!File.Exists(path_file))
                    {
                        return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Cannot find file [" + path_file + "]" }));
                    }

                    if (data == null)
                    {
                        data = string.Empty;
                    }
                    File.WriteAllText(path_file, data);

                    result = JsonConvert.SerializeObject(new { id = id, ok = true, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Update file [" + path_file + "] successfully." });
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Update file [" + path_file + "] fail: " + ex.Message }));
                }
                #endregion
                break;

            case "file_remove":
                #region
                if (string.IsNullOrEmpty(file_name))
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, msg = "the field [file_name] must be not empty" }));
                }
                try
                {
                    path_file = Path.Combine(path, file_name);
                    if (!File.Exists(path_file))
                    {
                        return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Cannot find file [" + path_file + "]" }));
                    }

                    File.Delete(path_file);

                    result = JsonConvert.SerializeObject(new { id = id, ok = true, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Remove file [" + path_file + "] successfully." });
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { id = id, ok = false, path = path /*.Replace("\\", "/")*/, file_name = file_name, msg = "Remove file [" + path_file + "] fail: " + ex.Message }));
                }
                #endregion
                break;

            default:
                result = JsonConvert.SerializeObject(new { id = id, ok = false, msg = "Cannot find action type is " + type });
                break;
            }
            return(result);
        }
Example #2
0
        protected override void ProcessRequest(System.Net.HttpListenerContext Context)
        {
            HttpListenerRequest  Request  = Context.Request;
            HttpListenerResponse Response = Context.Response;

            Response.AppendHeader("Access-Control-Allow-Origin", "*");
            Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");


            // Do any of these result in META tags e.g. <META HTTP-EQUIV="Expire" CONTENT="-1">
            // HTTP Headers or both?
            // Is this required for FireFox? Would be good to do this without magic strings.
            // Won't it overwrite the previous setting
            //Response.Headers.Add("Cache-Control", "no-cache, no-store");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Pragma", "no-store");
            Response.AddHeader("cache-control", "no-cache");

            string result = string.Empty, data = string.Empty,
                   content_type = "text/html; charset=utf-8",
                   uri          = HttpUtility.UrlDecode(Request.RawUrl);
            Stream OutputStream = Response.OutputStream;

            byte[] bOutput;

            switch (Request.Url.LocalPath)
            {
            case "/favicon.ico":
                break;

            case "/SERVER-SENT-EVENTS":
                #region
                content_type         = "text /event-stream; charset=utf-8";
                Response.ContentType = "text/event-stream";
                while (true)
                {
                    try
                    {
                        OutputStream.Write(sseBuffer, 0, 8);
                        OutputStream.Flush();
                    }
                    catch { }
                    System.Threading.Thread.Sleep(300);
                }
                OutputStream.Close();
                #endregion
                break;

            case "/msg.html":
                #region
                result = File.ReadAllText("msg.html");
                #endregion
                break;

            case "/fetch":
                #region
                string url_fetch = Request.QueryString["url"];
                try
                {
                    url_fetch = Encoding.UTF8.GetString(Convert.FromBase64String(url_fetch));

                    HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(url_fetch);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream       receiveStream = response.GetResponseStream();
                        StreamReader readStream    = null;

                        if (response.CharacterSet == null)
                        {
                            readStream = new StreamReader(receiveStream);
                        }
                        else
                        {
                            readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                        }

                        result = readStream.ReadToEnd();

                        response.Close();
                        readStream.Close();
                    }
                }
                catch (Exception exception)
                {
                    //throw new Exception("Error in base64Encode" + exception.Message);
                }

                #endregion
                break;

            case "/crawler":
                #region

                content_type = "application/json; charset=utf-8";
                string url_crawler = Request.QueryString["url"];
                try
                {
                    string s = "";
                    if (dicCacheCrawler.ContainsKey(url_crawler))
                    {
                        s = dicCacheCrawler[url_crawler];
                    }
                    else
                    {
                        url_crawler = Encoding.UTF8.GetString(Convert.FromBase64String(url_crawler));
                        s           = Chrome.getDataTabOpening(url_crawler);
                        dicCacheCrawler[url_crawler] = s;
                    }

                    string[] line = s.Split(new char[] { '\n', '\r' }).Where(x => x != string.Empty).ToArray();
                    string   text = Regex.Replace(s, "[^0-9a-zA-Z]+", " ").ToLower();
                    text = Regex.Replace(text, "[ ]{2,}", " ").ToLower();
                    var aword = text.Split(' ').Where(x => x.Length > 3)
                                .GroupBy(x => x)
                                .OrderByDescending(x => x.Count())
                                .Select(x => new word()
                    {
                        w = x.Key, k = x.Count()
                    })
                                .ToArray();
                    string htm = HtmlBuilder.renderFile(line);

                    result = JsonConvert.SerializeObject(new { id = 0, ok = true, extension = "", text = s, html = htm, word = aword });
                }
                catch (Exception exception)
                {
                    //throw new Exception("Error in base64Encode" + exception.Message);
                }

                #endregion
                break;

            case "/cambridge":
                #region

                content_type = "application/json; charset=utf-8";
                string w_cambridge = Request.QueryString["w"];
                try
                {
                    string s = "";
                    if (dicCacheWord.ContainsKey(uri))
                    {
                        s = dicCacheWord[uri];
                    }
                    else
                    {
                    }

                    result = JsonConvert.SerializeObject(new { id = 0, ok = true, extension = "", html = s });
                }
                catch (Exception exception)
                {
                    //throw new Exception("Error in base64Encode" + exception.Message);
                }

                #endregion
                break;

            case "/DIV_CLASS_END":
                #region
                content_type = "text/plain; charset=utf-8";
                if (File.Exists("DIV_CLASS_END.txt"))
                {
                    DIV_CLASS_END = File.ReadAllLines("DIV_CLASS_END.txt");
                    result        = string.Join(Environment.NewLine, DIV_CLASS_END);
                }
                else
                {
                    result = "Cannot find file DIV_CLASS_END.txt";
                }
                #endregion
                break;

            case "/TEXT_END":
                #region
                content_type = "text/plain; charset=utf-8";
                if (File.Exists("TEXT_END.txt"))
                {
                    TEXT_END = File.ReadAllLines("TEXT_END.txt");
                    result   = string.Join(Environment.NewLine, TEXT_END);
                }
                else
                {
                    result = "Cannot find file TEXT_END.txt";
                }
                #endregion
                break;

            default:
                #region
                HtmlDocument doc        = null;
                string       type       = Request.QueryString["type"],
                             id         = Request.QueryString["id"],
                             url        = getURL(uri),
                             path       = string.Empty,
                             folder     = string.Empty,
                             folder_new = string.Empty,
                             file_name  = string.Empty;
                if (id == null)
                {
                    id = string.Empty;
                }
                if (!string.IsNullOrEmpty(type))
                {
                    /* dir, file */
                    if (string.IsNullOrEmpty(url))
                    {
                        content_type = "application/json; charset=utf-8";
                        //path = Request.QueryString["path"];
                        //folder = Request.QueryString["folder"];
                        //folder_new = Request.QueryString["folder_new"];
                        //file_name = Request.QueryString["file_name"];

                        var dic = new Dictionary <string, string>()
                        {
                        };
                        if (uri.IndexOf('?') != -1)
                        {
                            dic = uri.Split('?')[1].Split('&').Select(x => x.Split('=')).ToDictionary(x => x[0].Trim().ToLower(), x => x[1].Trim());
                        }

                        if (dic.ContainsKey("path"))
                        {
                            path = dic["path"];
                        }
                        if (dic.ContainsKey("folder"))
                        {
                            folder = dic["folder"];
                        }
                        if (dic.ContainsKey("folder_new"))
                        {
                            folder_new = dic["folder_new"];
                        }
                        if (dic.ContainsKey("file_name"))
                        {
                            file_name = dic["file_name"];
                        }

                        using (StreamReader stream = new StreamReader(Request.InputStream))
                        {
                            data = stream.ReadToEnd();
                            if (!string.IsNullOrEmpty(data))
                            {
                                data = HttpUtility.UrlDecode(data);
                            }
                        }

                        result = processIO(id, type, path, folder, folder_new, file_name, data);
                    }
                    else
                    {
                        /* crawler */
                        result = getHtml(url);
                        if (!string.IsNullOrEmpty(result))
                        {
                            content_type = "text/plain; charset=utf-8";
                            switch (type)
                            {
                            case "html":
                                content_type = "text/html; charset=utf-8";
                                break;

                            case "text":
                                #region
                                doc = new HtmlDocument();
                                doc.LoadHtml(result);

                                StringWriter sw = new StringWriter();
                                hasH1         = false;
                                hasContentEnd = false;
                                ConvertToText(doc.DocumentNode, sw);
                                sw.Flush();
                                result = sw.ToString();
                                int p = result.IndexOf("{H1}");
                                if (p > 0)
                                {
                                    p += 4; result = result.Substring(p, result.Length - p).Trim();
                                }
                                if (!hasContentEnd)
                                {
                                    int pos_end = -1;
                                    for (int k = 0; k < TEXT_END.Length; k++)
                                    {
                                        pos_end = result.IndexOf(TEXT_END[k]);
                                        if (pos_end != -1)
                                        {
                                            result        = result.Substring(0, pos_end);
                                            hasContentEnd = true;
                                            break;
                                        }
                                    }
                                }
                                result = result.Replace(@"""", "”");

                                result = string.Join(Environment.NewLine, result.Split(new char[] { '\r', '\n' })
                                                     //.Select(x => x.Trim())
                                                     .Where(x => x != string.Empty && !x.Contains("©"))
                                                     .ToArray());

                                #endregion
                                break;

                            case "image":
                                break;

                            case "link":
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
                else
                {
                    result = "Cannot find [type] in QueryString.";
                }

                #endregion
                break;
            }

            bOutput = Encoding.UTF8.GetBytes(result);
            Response.ContentType     = content_type;
            Response.ContentLength64 = bOutput.Length;
            OutputStream.Write(bOutput, 0, bOutput.Length);
            OutputStream.Close();
        }