Ejemplo n.º 1
0
 public void Run()
 {
     ThreadPool.QueueUserWorkItem((o) =>
     {
         Console.WriteLine("Webserver running...");
         try
         {
             while (_listener.IsListening)
             {
                 ThreadPool.QueueUserWorkItem((c) =>
                 {
                     var ctx = c as HttpListenerContext;
                     try
                     {
                         wsResponse wsRes             = _responderMethod(ctx.Request);
                         ctx.Response.ContentLength64 = wsRes.ba.Length;
                         ctx.Response.ContentType     = wsRes.szMinetype;
                         ctx.Response.OutputStream.Write(wsRes.ba, 0, wsRes.ba.Length);
                     }
                     catch { } // suppress any exceptions
                     finally
                     {
                         // always close the stream
                         ctx.Response.OutputStream.Close();
                     }
                 }, _listener.GetContext());
             }
         }
         catch { } // suppress any exceptions
     });
 }
Ejemplo n.º 2
0
        public wsResponse SendResponse(HttpListenerRequest request)
        {
            Dictionary <string, string> postParams = new Dictionary <string, string>();
            wsResponse swRes = new wsResponse();

            string szProjectname = "";
            string szFilename    = "";
            string szHasJson     = "";

            if (request.QueryString["project"] != null)
            {
                szProjectname = request.QueryString["project"].ToString();
            }
            if (request.QueryString["file"] != null)
            {
                szFilename = request.QueryString["file"].ToString();
            }
            if (request.QueryString["json"] != null)
            {
                szHasJson = request.QueryString["json"].ToString();
            }

            swRes.ba = Encoding.UTF8.GetBytes("{}");
            if (request.HttpMethod.Equals("POST", StringComparison.InvariantCultureIgnoreCase))
            {
                if (string.IsNullOrEmpty(szProjectname))
                {
                    JsonError je = new JsonError {
                        szMessage = "POST requires a project "
                    };
                    ShowError(je.szMessage);

                    string szJson = Newtonsoft.Json.JsonConvert.SerializeObject(je);
                    swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                    swRes.szMinetype = "text/json";
                }
                else if (string.IsNullOrEmpty(szHasJson) && string.IsNullOrEmpty(szFilename))
                {
                    JsonError je = new JsonError {
                        szMessage = "POST requires both project and file "
                    };
                    ShowError(je.szMessage);

                    string szJson = Newtonsoft.Json.JsonConvert.SerializeObject(je);
                    swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                    swRes.szMinetype = "text/json";
                }
                else if (!string.IsNullOrEmpty(szHasJson))
                {
                    string szJson = "";
                    System.Text.Encoding encoding = request.ContentEncoding;
                    using (StreamReader sr = new StreamReader(request.InputStream, encoding))
                    {
                        szJson = sr.ReadToEnd();
                    }
                    request.InputStream.Close();
                    try
                    {
                        BildInfo bi = Newtonsoft.Json.JsonConvert.DeserializeObject <BildInfo>(szJson);
                        if (bi.CaptureDate == null)
                        {
                            bi.CaptureDate = DateTime.Now;
                        }
                        ProjectSql tmpProject = new ProjectSql(szProjectname, Config.current.szDbSuffix);
                        var        biResult   = tmpProject.GetBildId(bi.BildName, bi.CaptureDate);
                        tmpProject.SetCaptureDate(biResult.BildId, bi.CaptureDate);

                        Gebaeude gebauede = tmpProject.sqlGebaeude.Ensure(bi.GebaeudeBezeichnung) as Gebaeude;
                        Etage    etage    = tmpProject.sqlEtage.Ensure(bi.EtageBezeichnung) as Etage;
                        Wohnung  wohnung  = tmpProject.sqlWohnung.Ensure(bi.WohnungBezeichnung) as Wohnung;
                        Zimmer   zimmer   = tmpProject.sqlZimmer.Ensure(bi.ZimmerBezeichnung) as Zimmer;
                        if (gebauede != null)
                        {
                            tmpProject.sqlGebaeude.Set(biResult.BildId, gebauede.ID);
                        }
                        if (etage != null)
                        {
                            tmpProject.sqlEtage.Set(biResult.BildId, etage.ID);
                        }
                        if (wohnung != null)
                        {
                            tmpProject.sqlWohnung.Set(biResult.BildId, wohnung.ID);
                        }
                        if (zimmer != null)
                        {
                            tmpProject.sqlZimmer.Set(biResult.BildId, zimmer.ID);
                        }
                        tmpProject.SetComment(biResult.BildId, bi.KommentarBezeichnung);
                    }
                    catch (Exception ex)
                    {
                        JsonError je = new JsonError {
                            szMessage = "POST " + ex.ToString()
                        };
                        ShowError(je.szMessage);

                        string szErrJson = Newtonsoft.Json.JsonConvert.SerializeObject(je);
                        swRes.ba         = Encoding.UTF8.GetBytes(szErrJson);
                        swRes.szMinetype = "text/json";
                    }
                }
                else
                {
                    try
                    {
                        string szFullPath = System.IO.Path.Combine(XCamera.Util.Config.current.szBasedir, szProjectname, Config.current.szPicSuffix);
                        if (!Directory.Exists(szFullPath))
                        {
                            Directory.CreateDirectory(szFullPath);
                        }
                        string szFullFilename = System.IO.Path.Combine(szFullPath, szFilename);
                        ShowInfo("recieving " + szFullFilename);

                        using (FileStream fs = new FileStream(szFullFilename, FileMode.Create, FileAccess.Write))
                        {
                            request.InputStream.CopyTo(fs);
                        }
                        request.InputStream.Close();
                        ProjectSql tmpProject = new ProjectSql(szProjectname, Config.current.szDbSuffix);
                        tmpProject.AddBild(szFilename);
                    }
                    catch (Exception ex)
                    {
                        JsonError je = new JsonError {
                            szMessage = "POST " + ex.ToString()
                        };
                        ShowError(je.szMessage);

                        string szJson = Newtonsoft.Json.JsonConvert.SerializeObject(je);
                        swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                        swRes.szMinetype = "text/json";
                    }
                }
            }
            else
            {
                if (string.IsNullOrEmpty(szProjectname))
                {
                    ShowInfo("sending project list");
                    string[]           szProjects = Directory.GetDirectories(XCamera.Util.Config.current.szBasedir, "*.", SearchOption.TopDirectoryOnly);
                    List <JsonProject> jProjects  = new List <JsonProject>();
                    foreach (var szProject in szProjects)
                    {
                        jProjects.Add(new JsonProject {
                            szProjectName = System.IO.Path.GetFileNameWithoutExtension(szProject),
                            lSize         = GetDirectorySize(0, System.IO.Path.Combine(XCamera.Util.Config.current.szBasedir, szProject))
                        });
                    }

                    string szJson = Newtonsoft.Json.JsonConvert.SerializeObject(jProjects);
                    swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                    swRes.szMinetype = "text/json";
                }
                else if (!string.IsNullOrWhiteSpace(szFilename))
                {
                    string szFullFilename = System.IO.Path.Combine(XCamera.Util.Config.current.szBasedir, szProjectname, szFilename);
                    ShowInfo("sending  " + szFullFilename);
                    if (File.Exists(szFullFilename))
                    {
                        if (System.IO.Path.GetExtension(szFullFilename).Equals(".jpg", StringComparison.InvariantCultureIgnoreCase))
                        {
                            swRes.ba         = System.IO.File.ReadAllBytes(szFullFilename);
                            swRes.szMinetype = "image/jpeg";
                        }
                        else
                        {
                            // swRes.szMinetype = "Application/x-sqlite3";
                            ProjectSql tmpProject = new ProjectSql(szProjectname);
                            var        metaData   = tmpProject.GetMetaData();
                            string     szJson     = Newtonsoft.Json.JsonConvert.SerializeObject(metaData);
                            swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                            swRes.szMinetype = "text/json";
                        }
                    }
                    else
                    {
                        JsonError je = new JsonError {
                            szMessage = "File not found " + szFullFilename
                        };
                        ShowError(je.szMessage);
                        string szJson = Newtonsoft.Json.JsonConvert.SerializeObject(je);
                        swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                        swRes.szMinetype = "text/json";
                    }
                }
                else if (!string.IsNullOrEmpty(szProjectname))
                {
                    ShowInfo("sending project info " + szProjectname);
                    List <JsonProject> jProjects = new List <JsonProject>();

                    jProjects.Add(new JsonProject
                    {
                        szProjectName = System.IO.Path.GetFileNameWithoutExtension(szProjectname),
                        lSize         = GetDirectorySize(0, System.IO.Path.Combine(XCamera.Util.Config.current.szBasedir, szProjectname))
                    });


                    string szJson = Newtonsoft.Json.JsonConvert.SerializeObject(jProjects);
                    swRes.ba         = Encoding.UTF8.GetBytes(szJson);
                    swRes.szMinetype = "text/json";
                }
            }

            return(swRes);
        }