128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Web.Http;
|
|||
|
using Common.Utils;
|
|||
|
using Server.Services.DataMigration;
|
|||
|
|
|||
|
namespace Server.Controllers.OpenManagement
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 数据迁移Api
|
|||
|
/// </summary>
|
|||
|
public class DataMigrationController : DefaultController
|
|||
|
{
|
|||
|
private const string defaultTempPath = "Temp";
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 上传数据
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult Upload()
|
|||
|
{
|
|||
|
//替换只允许修改文件
|
|||
|
var provider = new MultipartMemoryStreamProvider();
|
|||
|
var r = Request.Content.ReadAsMultipartAsync(provider).Result;
|
|||
|
System.Net.Http.StreamContent item = null;
|
|||
|
foreach (var content in r.Contents)
|
|||
|
{
|
|||
|
switch (content.Headers.ContentDisposition.Name.Replace("\"", ""))
|
|||
|
{
|
|||
|
case "File":
|
|||
|
item = content as System.Net.Http.StreamContent;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
var fileName = Util.MapFile("datamigration.zip", defaultTempPath);
|
|||
|
//删除旧数据
|
|||
|
Util.DeleteFile(fileName);
|
|||
|
using (var ms = item.ReadAsStreamAsync().Result)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using (var filems = File.OpenWrite(fileName))
|
|||
|
{
|
|||
|
ms.CopyTo(filems);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
//如果新增失败,删掉无效文件
|
|||
|
Util.DeleteFile(fileName);
|
|||
|
return PutData(ex);
|
|||
|
}
|
|||
|
}
|
|||
|
return PutSuccess;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 检查上传的数据
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult Check()
|
|||
|
{
|
|||
|
var fileName = Util.MapFile("datamigration.zip", defaultTempPath);
|
|||
|
if (!File.Exists(fileName))
|
|||
|
{
|
|||
|
return PutData("请先上传数据。");
|
|||
|
}
|
|||
|
var err = DataMigrationManageSerivce.Instance.Check(fileName);
|
|||
|
if (string.IsNullOrWhiteSpace(err))
|
|||
|
{
|
|||
|
return PutSuccess;
|
|||
|
}
|
|||
|
return PutData(err);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 开始导入
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult OpenImport()
|
|||
|
{
|
|||
|
var state = DataMigrationProgressService.Instance.GetState();
|
|||
|
if (state != null && state.State == 1)
|
|||
|
{
|
|||
|
return PutData("后台已经正在处理中,请不要重复操作。");
|
|||
|
}
|
|||
|
var fileName = Util.MapFile("datamigration.zip", defaultTempPath);
|
|||
|
if (!File.Exists(fileName))
|
|||
|
{
|
|||
|
return PutData("请先上传数据。");
|
|||
|
}
|
|||
|
var err = DataMigrationManageSerivce.Instance.Start(fileName);
|
|||
|
if (string.IsNullOrWhiteSpace(err))
|
|||
|
{
|
|||
|
return PutSuccess;
|
|||
|
}
|
|||
|
return PutData(err);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 获取进度
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult GetProgress()
|
|||
|
{
|
|||
|
var state = DataMigrationProgressService.Instance.GetState();
|
|||
|
if (state == null)
|
|||
|
{
|
|||
|
return PutData("当前还未开始");
|
|||
|
}
|
|||
|
return PutData(new
|
|||
|
{
|
|||
|
StartDateTime = state.StartDateTime,
|
|||
|
EndDateTime = state.EndDateTime,
|
|||
|
Logs = state.Logs.ToArray(),
|
|||
|
State = state.State,
|
|||
|
ElapsedTime = state.ElapsedTime.ToString("0.00")
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|