add icon(128*128)
This commit is contained in:
@@ -2,6 +2,12 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Http;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using VNCServerList.Models;
|
||||
using VNCServerList.Services;
|
||||
|
||||
@@ -75,6 +81,20 @@ namespace VNCServerList.Web.Controllers
|
||||
return BadRequest("서버 정보가 없습니다.");
|
||||
}
|
||||
|
||||
// 아이콘이 Base64로 전송된 경우 처리
|
||||
if (!string.IsNullOrEmpty(server.IconBase64))
|
||||
{
|
||||
try
|
||||
{
|
||||
var iconData = Convert.FromBase64String(server.IconBase64);
|
||||
server.Icon = ResizeImage(iconData, 128, 128);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return BadRequest("아이콘 데이터 형식이 올바르지 않습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
bool success = _databaseService.AddServer(server);
|
||||
if (success)
|
||||
{
|
||||
@@ -102,6 +122,20 @@ namespace VNCServerList.Web.Controllers
|
||||
return BadRequest("서버 정보가 없습니다.");
|
||||
}
|
||||
|
||||
// 아이콘이 Base64로 전송된 경우 처리
|
||||
if (!string.IsNullOrEmpty(server.IconBase64))
|
||||
{
|
||||
try
|
||||
{
|
||||
var iconData = Convert.FromBase64String(server.IconBase64);
|
||||
server.Icon = ResizeImage(iconData, 128, 128);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return BadRequest("아이콘 데이터 형식이 올바르지 않습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
// 원본 사용자명과 IP로 서버를 찾아서 업데이트
|
||||
bool success = _databaseService.UpdateServerByUserAndIP(originalUser, originalIp, server);
|
||||
if (success)
|
||||
@@ -264,5 +298,154 @@ namespace VNCServerList.Web.Controllers
|
||||
return InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("icon/{user}/{ip}")]
|
||||
public HttpResponseMessage GetServerIcon(string user, string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
var server = _databaseService.GetServerByUserAndIP(user, ip);
|
||||
if (server?.Icon == null || server.Icon.Length == 0)
|
||||
{
|
||||
return Request.CreateResponse(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
var response = Request.CreateResponse(HttpStatusCode.OK);
|
||||
response.Content = new ByteArrayContent(server.Icon);
|
||||
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] ResizeImage(byte[] imageData, int maxWidth = 128, int maxHeight = 128)
|
||||
{
|
||||
using (var originalStream = new MemoryStream(imageData))
|
||||
using (var originalImage = Image.FromStream(originalStream))
|
||||
{
|
||||
// 원본 이미지 크기
|
||||
int originalWidth = originalImage.Width;
|
||||
int originalHeight = originalImage.Height;
|
||||
|
||||
// 리사이즈가 필요한지 확인
|
||||
if (originalWidth <= maxWidth && originalHeight <= maxHeight)
|
||||
{
|
||||
return imageData; // 리사이즈 불필요
|
||||
}
|
||||
|
||||
// 새로운 크기 계산 (비율 유지)
|
||||
double ratioX = (double)maxWidth / originalWidth;
|
||||
double ratioY = (double)maxHeight / originalHeight;
|
||||
double ratio = Math.Min(ratioX, ratioY);
|
||||
|
||||
int newWidth = (int)(originalWidth * ratio);
|
||||
int newHeight = (int)(originalHeight * ratio);
|
||||
|
||||
// 새 이미지 생성
|
||||
using (var resizedImage = new Bitmap(newWidth, newHeight))
|
||||
using (var graphics = Graphics.FromImage(resizedImage))
|
||||
{
|
||||
// 고품질 리사이즈 설정
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
|
||||
// 이미지 그리기
|
||||
graphics.DrawImage(originalImage, 0, 0, newWidth, newHeight);
|
||||
|
||||
// PNG로 변환하여 반환
|
||||
using (var outputStream = new MemoryStream())
|
||||
{
|
||||
resizedImage.Save(outputStream, ImageFormat.Png);
|
||||
return outputStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("icon/{user}/{ip}")]
|
||||
public IHttpActionResult UploadServerIcon(string user, string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
var httpRequest = System.Web.HttpContext.Current.Request;
|
||||
if (httpRequest.Files.Count == 0)
|
||||
{
|
||||
return BadRequest("업로드된 파일이 없습니다.");
|
||||
}
|
||||
|
||||
var file = httpRequest.Files[0];
|
||||
if (file.ContentLength > 1024 * 1024) // 1MB 제한
|
||||
{
|
||||
return BadRequest("파일 크기는 1MB를 초과할 수 없습니다.");
|
||||
}
|
||||
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
file.InputStream.CopyTo(memoryStream);
|
||||
var originalIconData = memoryStream.ToArray();
|
||||
|
||||
// 이미지 리사이즈 (128x128로 제한)
|
||||
var resizedIconData = ResizeImage(originalIconData, 128, 128);
|
||||
|
||||
var server = _databaseService.GetServerByUserAndIP(user, ip);
|
||||
if (server == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
server.Icon = resizedIconData;
|
||||
bool success = _databaseService.UpdateServer(server);
|
||||
|
||||
if (success)
|
||||
{
|
||||
return Ok(new { Message = "아이콘이 성공적으로 업로드되었습니다." });
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("아이콘 업로드에 실패했습니다.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("icon/{user}/{ip}")]
|
||||
public IHttpActionResult DeleteServerIcon(string user, string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
var server = _databaseService.GetServerByUserAndIP(user, ip);
|
||||
if (server == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
server.Icon = null;
|
||||
bool success = _databaseService.UpdateServer(server);
|
||||
|
||||
if (success)
|
||||
{
|
||||
return Ok(new { Message = "아이콘이 성공적으로 삭제되었습니다." });
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("아이콘 삭제에 실패했습니다.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user