Example #1
0
 public static Protocol SendImage(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      = "******";
             MySqlParameter[] parameters_user = new MySqlParameter[1];
             parameters_user[0] = new MySqlParameter("@ID", ID);
             if (DataServer.Database.Count(query_user, parameters_user) == 1 && prot.User != null)
             {
                 if (prot.ImageBytes == null)
                 {
                     return(new Protocol(MessageType.Error)
                     {
                         Message = "Empty image"
                     });
                 }
                 Image  img     = ConvertImage.ByteArrayToImage(prot.ImageBytes);
                 string newpath = "/var/www/accer/data/image/" + (new Random()).Next().ToString() +
                                  DateTime.Now.Ticks.ToString() + ".png";
                 string hash = Hash.Sha1(newpath);
                 ConvertImage.SaveFile(img, newpath);
                 string           insert_img     = "INSERT INTO image(hash, path, extension) VALUES(@hash, @path, @ext)";
                 MySqlParameter[] parameters_img = new MySqlParameter[3];
                 parameters_img[0] = new MySqlParameter("@hash", hash);
                 parameters_img[1] = new MySqlParameter("@path", newpath);
                 parameters_img[2] = new MySqlParameter("@ext", "png");
                 if (DataServer.Database.Insert(insert_img, parameters_img))
                 {
                     string           query_imgid      = "SELECT * FROM image WHERE path = @path AND hash = @hash";
                     MySqlParameter[] parameters_imgid = new MySqlParameter[2];
                     parameters_imgid[0] = new MySqlParameter("@path", newpath);
                     parameters_imgid[1] = new MySqlParameter("@hash", hash);
                     Dictionary <string, string> result_imgid =
                         DataServer.Database.Select(query_imgid, parameters_imgid)[0];
                     int              idimg             = int.Parse(result_imgid["ID"]);
                     string           query_update      = "UPDATE user SET avatar_path = @idimg WHERE ID = @ID";
                     MySqlParameter[] parameters_update = new MySqlParameter[2];
                     parameters_update[0] = new MySqlParameter("@idimg", idimg);
                     parameters_update[1] = new MySqlParameter("@ID", ID);
                     if (DataServer.Database.Update(query_update, parameters_update))
                     {
                         return(new Protocol(MessageType.Response)
                         {
                             Message = "success"
                         });
                     }
                     return(new Protocol(MessageType.Error)
                     {
                         Message = "Server Error"
                     });
                 }
                 return(new Protocol(MessageType.Error)
                 {
                     Message = "Server Error"
                 });
             }
             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"
         });
     }
 }