2024-09-07 06:46:08 +00:00
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Common;
|
2024-06-24 09:27:12 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using RemoteServer.Models;
|
2024-09-07 06:46:08 +00:00
|
|
|
|
|
2024-06-24 09:27:12 +00:00
|
|
|
|
namespace RemoteServer.Controllers;
|
|
|
|
|
|
2024-09-07 06:46:08 +00:00
|
|
|
|
public class SyncFilesController(RemoteSyncServerFactory factory, SqliteDbContext db)
|
|
|
|
|
: ControllerBase
|
2024-06-24 09:27:12 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly SqliteDbContext _db = db;
|
2024-09-05 09:48:08 +00:00
|
|
|
|
private readonly RemoteSyncServerFactory Factory = factory;
|
|
|
|
|
|
2024-09-07 06:46:08 +00:00
|
|
|
|
[Route("/websoc")]
|
|
|
|
|
public async Task WebsocketConnection(string Name)
|
|
|
|
|
{
|
|
|
|
|
if (HttpContext.WebSockets.IsWebSocketRequest)
|
2024-09-05 09:48:08 +00:00
|
|
|
|
{
|
2024-09-07 06:46:08 +00:00
|
|
|
|
try
|
2024-09-05 09:48:08 +00:00
|
|
|
|
{
|
2024-09-07 06:46:08 +00:00
|
|
|
|
if (Factory.GetServerByName(Name) == null)
|
2024-09-05 09:48:08 +00:00
|
|
|
|
{
|
|
|
|
|
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
2024-09-22 05:27:52 +00:00
|
|
|
|
var pipeLine = new WebSocPipeLine<WebSocket>(webSocket, true);
|
2024-10-10 05:15:14 +00:00
|
|
|
|
await Factory.CreateRemoteSyncServer(pipeLine, Name);
|
2024-09-05 09:48:08 +00:00
|
|
|
|
}
|
2024-09-07 06:46:08 +00:00
|
|
|
|
else
|
2024-09-05 09:48:08 +00:00
|
|
|
|
{
|
2024-09-07 06:46:08 +00:00
|
|
|
|
throw new Exception("RemoteServer: 存在相同名称的发布正在进行!");
|
2024-09-05 09:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-07 06:46:08 +00:00
|
|
|
|
catch (Exception e)
|
2024-09-05 09:48:08 +00:00
|
|
|
|
{
|
2024-09-07 06:46:08 +00:00
|
|
|
|
HttpContext.Response.Body = new MemoryStream(Encoding.UTF8.GetBytes(e.Message));
|
|
|
|
|
HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
|
2024-09-05 09:48:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-07 06:46:08 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-12 06:30:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 上传文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-09-22 05:27:52 +00:00
|
|
|
|
[HttpPost("/UploadFile")]
|
2024-10-11 08:32:36 +00:00
|
|
|
|
public async Task<IActionResult> UploadFile(IFormFile file)
|
2024-09-22 05:27:52 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (file == null || file.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("文件不存在!");
|
|
|
|
|
}
|
2024-10-11 08:32:36 +00:00
|
|
|
|
if (!Directory.Exists(RemoteSyncServer.TempRootFile))
|
|
|
|
|
Directory.CreateDirectory(RemoteSyncServer.TempRootFile);
|
|
|
|
|
var filePath = Path.Combine(RemoteSyncServer.TempRootFile, file.FileName);
|
2024-09-22 05:27:52 +00:00
|
|
|
|
using (var stream = new FileStream(filePath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
await file.CopyToAsync(stream);
|
|
|
|
|
}
|
2024-10-11 08:32:36 +00:00
|
|
|
|
var server = Factory.GetServerById(file.FileName.Split('.').First());
|
2024-09-22 05:27:52 +00:00
|
|
|
|
if (server == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("不存在的Id!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var h = new UnPackAndReleaseHelper(server);
|
2024-09-27 07:02:55 +00:00
|
|
|
|
server.SetStateHelpBase(h);
|
2024-09-22 05:27:52 +00:00
|
|
|
|
h.UnPack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(new { IsSuccess = true, Message = "File uploaded successfully." });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-10-11 01:04:58 +00:00
|
|
|
|
return StatusCode(500, new { IsSuccess = false, Message = $"上传文件失败: {ex.Message}" });
|
2024-09-22 05:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-12 06:30:56 +00:00
|
|
|
|
|
|
|
|
|
// [HttpGet("/GetSyncFilesLogs")]
|
|
|
|
|
// public IActionResult GetSyncFilesLogs(
|
|
|
|
|
// string? ClientName,
|
|
|
|
|
// int? Status,
|
|
|
|
|
// DateTime? SyncTimeStart,
|
|
|
|
|
// DateTime? SyncTimeEnd,
|
|
|
|
|
// int page,
|
|
|
|
|
// int rows
|
|
|
|
|
// )
|
|
|
|
|
// {
|
|
|
|
|
// var item =
|
|
|
|
|
// from i in _db.SyncLogHeads
|
|
|
|
|
// where
|
|
|
|
|
// (
|
|
|
|
|
// string.IsNullOrEmpty(ClientName)
|
|
|
|
|
// || (i.ClientName != null && i.ClientName.Contains(ClientName))
|
|
|
|
|
// )
|
|
|
|
|
// && (Status == null || i.Status == Status)
|
|
|
|
|
// && (SyncTimeStart == null || i.SyncTime >= SyncTimeStart)
|
|
|
|
|
// && (SyncTimeEnd == null || i.SyncTime <= SyncTimeEnd)
|
|
|
|
|
// orderby i.Id descending
|
|
|
|
|
// select new
|
|
|
|
|
// {
|
|
|
|
|
// Head = i,
|
|
|
|
|
// Files = (from j in _db.SyncLogFiles where j.HeadId == i.Id select j).ToList()
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// return Ok(item.Skip((page - 1) * rows).Take(rows).ToList());
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public class InputFileInfo
|
|
|
|
|
// {
|
|
|
|
|
// public required string RelativePath { get; set; }
|
|
|
|
|
// public DateTime MTime { get; set; }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public class OutputFileInfo : InputFileInfo
|
|
|
|
|
// {
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// 0 新增 1 修改 2 删除
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// public int ServerOpType { get; set; }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public class ServerOpFileInfo : OutputFileInfo
|
|
|
|
|
// {
|
|
|
|
|
// public required string ServerRootDirPath { get; set; }
|
|
|
|
|
// public required string ClientRootDirPath { get; set; }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public class InputFiles
|
|
|
|
|
// {
|
|
|
|
|
// public required string ServerRootDirPath { get; set; }
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// 0 special 1 exclude
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// public int Type { get; set; }
|
|
|
|
|
// public List<FileInfo>? Files { get; set; }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public class ServerOpFiles
|
|
|
|
|
// {
|
|
|
|
|
// public required string ServerRootDirPath { get; set; }
|
|
|
|
|
// public string? ClientRootDirPath { get; set; }
|
|
|
|
|
// public List<OutputFileInfo>? Files { get; set; }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// [HttpPost("/GetFilesInfoByDir")]
|
|
|
|
|
// public IActionResult GetFilesInfoByDir([FromBody] InputFiles inputFiles)
|
|
|
|
|
// {
|
|
|
|
|
// return Ok(new { IsSuccess = true });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// [HttpPost("/InitASync")]
|
|
|
|
|
// public IActionResult InitASync([FromBody] SyncLogHead head)
|
|
|
|
|
// {
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// var CurrentSyncTaskCount = (
|
|
|
|
|
// from i in _db.SyncLogHeads
|
|
|
|
|
// where i.Status == 0
|
|
|
|
|
// select i
|
|
|
|
|
// ).Count();
|
|
|
|
|
// if (CurrentSyncTaskCount > 0)
|
|
|
|
|
// {
|
|
|
|
|
// throw new Exception("存在未完成的任务,请等待完成!");
|
|
|
|
|
// }
|
|
|
|
|
// head.Id = Guid.NewGuid();
|
|
|
|
|
// head.SyncTime = DateTime.Now;
|
|
|
|
|
// head.Status = 0;
|
|
|
|
|
// _db.SyncLogHeads.Add(head);
|
|
|
|
|
// _db.SaveChanges();
|
|
|
|
|
// return Ok(new { IsSuccess = true, head.Id });
|
|
|
|
|
// }
|
|
|
|
|
// catch (Exception e)
|
|
|
|
|
// {
|
|
|
|
|
// return Ok(new { IsSuccess = false, e.Message });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// [HttpGet("/CloseASync")]
|
|
|
|
|
// public IActionResult CloseASync(Guid Id, string Message, int Status)
|
|
|
|
|
// {
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// var current =
|
|
|
|
|
// (from i in _db.SyncLogHeads where i.Id == Id select i).FirstOrDefault()
|
|
|
|
|
// ?? throw new Exception("任务不存在!");
|
|
|
|
|
// current.Status = Status;
|
|
|
|
|
// current.Message = Message;
|
|
|
|
|
// _db.SaveChanges();
|
|
|
|
|
// return Ok(new { IsSuccess = true });
|
|
|
|
|
// }
|
|
|
|
|
// catch (Exception e)
|
|
|
|
|
// {
|
|
|
|
|
// return Ok(new { IsSuccess = false, e.Message });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-06-24 09:27:12 +00:00
|
|
|
|
}
|