You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

155 lines
4.8 KiB

using log4net;
using System;
using OT.COM.SignalerMessage;
using CounsellorBL.Helper;
using SoldierData.EnterprizeV4;
using MonumentDefine;
using Newtonsoft.Json;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using OT.COM.LogisticsUtil;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Concurrent;
namespace EnterprizeV4.Controllers
{
public class TaskController : Controller
{
const string CACHE_KEY_tb_sys_uploadlog = "CACHE_KEY_tb_sys_uploadlog";
private ILog _inst = null;
protected ILog Logger
{
get
{
if (_inst == null)
{
_inst = LogManager.GetLogger(this.GetType());
}
return _inst;
}
}
IMemoryCache _getCache()
{
IMemoryCache ic = null;
if (HttpContext.RequestServices != null)
{
object o = HttpContext.RequestServices.GetService(typeof(IMemoryCache));
if (o != null)
{
ic = o as IMemoryCache;
}
}
return ic;
}
[HttpGet]
public ActionResult Download(string fileid)
{
ActionResult ar = null;
string sMsg = null;
try
{
do
{
string sRelPath = null;
if (fileid != null)
{
IMemoryCache imc = _getCache();
if (imc != null)
{
bool bNeedDB = true;
if (!imc.TryGetValue(CACHE_KEY_tb_sys_uploadlog, out ConcurrentDictionary<string, tb_sys_uploadlog> dicMap))
{
var entryOptions = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.NeverRemove);
dicMap = new ConcurrentDictionary<string, tb_sys_uploadlog>();
imc.Set(CACHE_KEY_tb_sys_uploadlog, dicMap, entryOptions);
}
else if(dicMap.TryGetValue(fileid, out tb_sys_uploadlog ulRes))
{
sRelPath = ulRes.path;
bNeedDB = false;
}
if(bNeedDB)
{
sMsg = DownloadHelper.GetFilePathByID(fileid, out tb_sys_uploadlog ulRes);
if (sMsg != null)
{
break;
}
dicMap.TryAdd(fileid, ulRes);
sRelPath = ulRes.path;
}
}
else
{
sMsg = DownloadHelper.GetFilePathByID(fileid, out tb_sys_uploadlog ulRes);
if (sMsg != null)
{
break;
}
sRelPath = ulRes.path;
}
}
else
{
sMsg = MessageWording.PARAM_NOT_EXPECTED;
break;
}
if (sRelPath == null)
{
sMsg = MessageWording.PARAM_NOT_EXPECTED;
break;
}
string sPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, sRelPath);
if (!System.IO.File.Exists(sPath))
{
sMsg = BLWording.FILE_NOT_EXIST;
break;
}
string[] saNam = sPath.Split("\\/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var stream = new FileStream(sPath, FileMode.Open);
ar = File(stream, System.Net.Mime.MediaTypeNames.Application.Octet, saNam[^1]);
}
while (false);
}
catch (DirectoryNotFoundException ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
}
if (sMsg != null)
{
ar = Content(JsonConvert.SerializeObject(new CErrorResponseMessage(sMsg, null)));
}
return ar;
}
}
}