public ResponseStruct GetImageByID(string SeasonID, int ID) { ResponseStruct Result = null; LauncherClientInformations aClient; if (!OverallInformations.ManagementClients.TryGetValue(SeasonID, out aClient)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Management Client không tồn tại." }, Broadcast = new List<string>() { "Có thể kết nối mạng của bạn có độ trễ lớn nên bản ghi của bạn trên Terraria Launcher Server đã bị xóa, vui lòng khởi động lại Management Client." } }; } else if (String.IsNullOrWhiteSpace(aClient.UserName)) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Chưa đăng nhập." }, Broadcast = new List<string>() { "Tiến hành đăng nhập để tiếp tục." } }; } else { MySqlConnection DBConnection = new MySqlConnection(OverallInformations.LauncherDBConnectionString); DBConnection.Open(); string SQLQuery = "SELECT FileName FROM images_from_players WHERE ID=@ID"; MySqlCommand DBCommand = new MySqlCommand(SQLQuery, DBConnection); DBCommand.Prepare(); DBCommand.Parameters.Add("@ID", MySqlDbType.Int32).Value = ID; MySqlDataReader DBReader = DBCommand.ExecuteReader(); while (DBReader.Read()) // Chắc chắn chỉ lặp nhiều nhất 1 lần. { if (!DBReader.IsDBNull(DBReader.GetOrdinal("FileName"))) { string FilePath = Path.Combine(OverallInformations.ImagePath, DBReader.GetString(DBReader.GetOrdinal("FileName"))); if (File.Exists(FilePath)) { Base64Utils base64 = new Base64Utils(); FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.None); string Content = String.Empty; Content = base64.ImageToBase64(Image.FromStream(fs), System.Drawing.Imaging.ImageFormat.Png); fs.Close(); fs.Dispose(); Result = new ResponseStruct() { Status = ResponseStatusType.Done, Content = Content }; } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Không tìm thấy tập tin hình theo yêu cầu." } }; } } else { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Cơ sở dữ liệu bị lỗi." } }; } break; } DBReader.Dispose(); DBCommand.Dispose(); DBConnection.Dispose(); } if (Result == null) { Result = new ResponseStruct() { Status = ResponseStatusType.Fail, Error = new List<string>() { "Cơ sở dữ liệu bị lỗi." } }; } return Result; }
private static void LauncherImageWorker() { IModel channel = OverallInformations.conn.CreateModel(); channel.ExchangeDeclare(exchange: OverallInformations.LauncherServerClientExchange, type: "direct", durable: false, autoDelete: true, arguments: null); channel.QueueDeclare(queue: OverallInformations.LauncherImageQueueName, durable: false, exclusive: false, autoDelete: true, arguments: null); channel.QueueBind(queue: OverallInformations.LauncherImageQueueName, exchange: OverallInformations.LauncherServerClientExchange, routingKey: OverallInformations.LauncherImageRouterKey, arguments: null); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); EventingBasicConsumer consumer = new EventingBasicConsumer(channel); LauncherService LauncherServiceInstance = new LauncherService(); Base64Utils base64Utils = new Base64Utils(); consumer.Received += (model, ea) => { IBasicProperties deliverdProps = ea.BasicProperties; IBasicProperties replyProps = channel.CreateBasicProperties(); replyProps.CorrelationId = deliverdProps.CorrelationId; byte[] deliveredBody = ea.Body; try { string message = Encoding.UTF8.GetString(deliveredBody); Request01 IncomingContent = Newtonsoft.Json.JsonConvert.DeserializeObject<Request01>(message); if (IncomingContent.RequestType == "Launcher.Image") { if (OverallInformations.IsAllowedReceiveRequestFromClient && OverallInformations.IsAllowedReceiveImageFromClient) { LauncherServiceInstance.SaveImage(IncomingContent.Parameters[0], base64Utils.Base64ToImage(IncomingContent.Parameters[1])); } } } catch (Exception e) { OutputMessage(String.Format("[Server]: LauncherImageWorker() | Error: {0}", e.Message), MessageType.Error); } finally { channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); } }; channel.BasicConsume(queue: OverallInformations.LauncherImageQueueName, noAck: false, consumer: consumer); }