Пример #1
0
 public static Protocol GetImage(Protocol prot, DataTcpClient client)
 {
     try
     {
         if (prot.Token == "")
         {
             return(new Protocol(MessageType.Error)
             {
                 Message = "Empty token"
             });
         }
         int              ID          = GetIdbyToken(prot.Token);
         string           query_token = "SELECT COUNT(*) FROM gamesess WHERE ID_user = @ID AND token = @token";
         MySqlParameter[] parameters  = new MySqlParameter[2];
         parameters[0] = new MySqlParameter("@ID", ID);
         parameters[1] = new MySqlParameter("@token", DBManager.Escape(prot.Token));
         if (DataServer.Database.Count(query_token, parameters) == 1)
         {
             string           query_user       = "******";
             string           query_user_count = "SELECT COUNT(*) FROM user WHERE email = @mail";
             MySqlParameter[] parameters_user  = new MySqlParameter[1];
             parameters_user[0] = new MySqlParameter("@mail", prot.Email);
             if (DataServer.Database.Count(query_user_count, parameters_user) == 1)
             {
                 Dictionary <string, string> result =
                     DataServer.Database.Select(query_user, parameters_user)[0];
                 int    idavatar = int.Parse(result["avatar_path"]);
                 byte[] img      = null;
                 try
                 {
                     string           query_img      = "SELECT * FROM image WHERE ID = @id";
                     MySqlParameter[] parameters_img = new MySqlParameter[1];
                     parameters_img[0] = new MySqlParameter("@id", idavatar);
                     Dictionary <string, string> result_img =
                         DataServer.Database.Select(query_img, parameters_img)[0];
                     string path = result_img["path"];
                     img = ConvertImage.ImageToByteArray(ConvertImage.FromFile(path));
                 }
                 catch (Exception)
                 {
                     return(new Protocol(MessageType.Error)
                     {
                         Message = "invalid request"
                     });
                 }
                 return(new Protocol(MessageType.Response)
                 {
                     Email = result["email"],
                     ImageBytes = img
                 });
             }
             return(new Protocol(MessageType.Error)
             {
                 Message = "Bad request"
             });
         }
         else
         {
             return(new Protocol(MessageType.Error)
             {
                 Message = "Bad token"
             });
         }
     }
     catch (Exception)
     {
         return(new Protocol(MessageType.Error)
         {
             Message = "Error"
         });
     }
 }