You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.0 KiB
C#
84 lines
3.0 KiB
C#
using System.Globalization;
|
|
using WebSocketSharp;
|
|
|
|
namespace OMS.NET.Instructs
|
|
{
|
|
public class GetServerImgInstruct : Instruct
|
|
{
|
|
public GetServerImgInstruct()
|
|
{
|
|
this.Type = "get_serverImg";
|
|
}
|
|
|
|
public override Task Handler(string wsid)
|
|
{
|
|
return Task.Run(() =>
|
|
{
|
|
if (this.Time != null)
|
|
{
|
|
//时间解析
|
|
string format = "yyyy-MM-dd HH:mm:ss";
|
|
DateTime clientDateTime = DateTime.ParseExact(this.Time, format, CultureInfo.InvariantCulture);
|
|
//Console.WriteLine("Converted DateTime: " + clientDateTime);
|
|
string serverImgPath = GlobalArea.ServerConfig.Img;
|
|
if (File.Exists(serverImgPath))
|
|
{
|
|
DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath);
|
|
Instruct res = new SendServerConfigInstruct
|
|
{
|
|
IsResponse = true
|
|
};
|
|
if (serverImgLastModified > clientDateTime)
|
|
{
|
|
byte[] imageBytes = File.ReadAllBytes(serverImgPath);
|
|
string imageFileType = GetImageFileType(imageBytes);
|
|
string base64String = $"data:image/{imageFileType};base64," + Convert.ToBase64String(imageBytes);
|
|
//Console.WriteLine("Base64 Encoded Image: " + base64String);
|
|
res.Data = new
|
|
{
|
|
@string = base64String,
|
|
time = serverImgLastModified.ToString(format),
|
|
};
|
|
}
|
|
else
|
|
{
|
|
res.Data = new
|
|
{
|
|
@string = ""
|
|
};
|
|
}
|
|
this.ResponseOrBroadcastInstructs.Add(res);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
static string GetImageFileType(byte[] imageBytes)
|
|
{
|
|
byte[] fileHeader = imageBytes.SubArray(0, 8);
|
|
|
|
// PNG 文件的文件头字节为 89 50 4E 47 0D 0A 1A 0A
|
|
if (fileHeader[0] == 0x89 && fileHeader[1] == 0x50 && fileHeader[2] == 0x4E && fileHeader[3] == 0x47 &&
|
|
fileHeader[4] == 0x0D && fileHeader[5] == 0x0A && fileHeader[6] == 0x1A && fileHeader[7] == 0x0A)
|
|
{
|
|
return "png";
|
|
}
|
|
|
|
// JPEG 文件的文件头字节为 FF D8 FF
|
|
if (fileHeader[0] == 0xFF && fileHeader[1] == 0xD8 && fileHeader[2] == 0xFF)
|
|
{
|
|
return "jpeg";
|
|
}
|
|
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
public class SendServerImgInstruct : Instruct
|
|
{
|
|
public SendServerImgInstruct()
|
|
{
|
|
this.Type = "send_serverImg";
|
|
}
|
|
}
|
|
} |