using DAL.Redis;
using Ninject;
using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using WX.CRM.Common;
using WX.CRM.DAL.Redis;
using WX.CRM.IBLL.WeWork;
using WX.CRM.Model.DTO.wework;
using WX.CRM.WebHelper.Infrastructure;
namespace WxService
{
///
/// WeWorkFileUpload 的摘要说明
///
public class WeWorkFileUpload : IHttpHandler
{
private readonly PubSub _sub = new PubSub();
private IWeWork_MsgKey msgkey = NinjectControllerFactory.ninjectKernel.Get();
public void ProcessRequest(HttpContext context)
{
string keymd5 = string.Empty;
try
{
//LogHelper.Info("===调用成功====");
context.Response.ContentType = "text/html";
keymd5 = context.Request.Params["keymd5"];
if (string.IsNullOrEmpty(keymd5))
{
HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = true, message = "找不到keyMD5", url = keymd5 }));
}
string filename = context.Request.Params["filename"];
if (string.IsNullOrEmpty(filename))
throw new Exception("filename不能为空!");
string type = HttpUtility.UrlDecode(context.Request.Params["type"], Encoding.UTF8); ;
//LogHelper.Info("fileName:" + fileName + ";msgSvrId:" + msgSvrId + ";context.Request.Files:" + context.Request.Files.Count);
HttpPostedFile file = context.Request.Files[0];
var yearMonth = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
//string uploadFolder = (ConfigurationManager.AppSettings["uploadfile"] ?? "uploadfile") + "/" + yearMonth;
var uploadFolder = string.Empty;
uploadFolder = (ConfigurationManager.AppSettings["weworkfile"] ?? "weworkfile") + "/" + yearMonth;
if (!Directory.Exists(uploadFolder))
{
Directory.CreateDirectory(uploadFolder);
}
string filePath = Path.Combine(uploadFolder, filename);
file.SaveAs(filePath);
PushRedisAsync(keymd5, filename, type);
//PushRedisAsync.Current.Response.Clear();
HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = true, message = "接收成功", url = filename }));
}
catch (Exception ex)
{
LogHelper.Error("keymd5:" + keymd5 + ";错误:" + ex.ToString());
//HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = false, message = "接收失败:" + ex.Message }));
}
}
private void PushRedisAsync(string _keymd5, string _path, string _type)
{
var redisKey = "weworkfile:" + _keymd5;
var fileWebUrl = "/weworkfile/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "/" + _path;
var model = new weworkfilepath
{
keymd5 = _keymd5,
path = fileWebUrl,
type = _type
};
RedisString rdb = new RedisString(redisKey);
TimeSpan span = new TimeSpan(30 * 6, 0, 0, 0, 0);//保留180天
rdb.Set(fileWebUrl, span);
PushRedisAndPublish(model, "weworkfile");
}
private void PushRedisAndPublish(T model, string key)
{
//写入队列
var result = new RedisList().LeftPush(key, model);
if (result > 0)
{
//发送订阅通知
_sub.Publish("sub:" + key, "");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}