112 lines
4.3 KiB
C#
112 lines
4.3 KiB
C#
using Competition.Common.Util;
|
|
using CompetitionAPI.api.unity;
|
|
using CompetitionAPI.Util;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using System.Xml.Linq;
|
|
|
|
namespace CompetitionAPI.Controllers.unity
|
|
{
|
|
[Route("unity/[controller]")]
|
|
[ApiController]
|
|
public class UploadFileController : Controller
|
|
{
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
|
|
Competition.Mysql.BLL.pow_achievement achievement_bll = new Competition.Mysql.BLL.pow_achievement();
|
|
|
|
Competition.Mysql.BLL.pow_video video_bll = new Competition.Mysql.BLL.pow_video();
|
|
|
|
public UploadFileController(IWebHostEnvironment webHostEnvironment)
|
|
{
|
|
_webHostEnvironment = webHostEnvironment;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上传文件接口
|
|
/// </summary>
|
|
/// <param name="req"></param>
|
|
/// <returns></returns>
|
|
[Authorize]
|
|
[HttpPost]
|
|
[APIFilter]
|
|
public async Task<JsonResult> Index([FromForm] IFormFile Files, [FromForm] UploadFileRequest req)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(req.ExamId))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试id不能为空"));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(req.UserId))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "用户id不能为空"));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(req.FileDesc))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "文件描述不能为空"));
|
|
}
|
|
|
|
var model = achievement_bll.GetModelList(" ExamId='" + req.ExamId + "' and UserId='" + req.UserId + "'").FirstOrDefault();
|
|
if (model == null)
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "找不到考生考试记录"));
|
|
}
|
|
|
|
try
|
|
{
|
|
if (Files.Length <= 0)
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "文件不能为空"));
|
|
}
|
|
// Handle file upload
|
|
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(Files.FileName);
|
|
var folderPath = "/Upload/PlayBack/";
|
|
if (!Directory.Exists(_webHostEnvironment.WebRootPath + folderPath))
|
|
{
|
|
Directory.CreateDirectory(_webHostEnvironment.WebRootPath + folderPath);
|
|
}
|
|
var filePath = folderPath + fileName;
|
|
var serverPath = _webHostEnvironment.WebRootPath + filePath;
|
|
using (var stream = new FileStream(serverPath, FileMode.Create))
|
|
{
|
|
await Files.CopyToAsync(stream);
|
|
}
|
|
|
|
var video_model = new Competition.Mysql.Model.pow_video();
|
|
video_model.VideoId = Guid.NewGuid().ToString("N");
|
|
video_model.AchievementId = model.AchievementId;
|
|
video_model.VideoPath = filePath;
|
|
video_model.VideoDesc = req.FileDesc;
|
|
video_model.CreateTime = DateTime.Now;
|
|
|
|
if (video_bll.Add(video_model))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Success, "文件上传成功"));
|
|
}
|
|
else
|
|
{
|
|
if (System.IO.File.Exists(serverPath))
|
|
{
|
|
System.IO.File.Delete(serverPath);
|
|
}
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "文件上传失败"));
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, e.Message));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|