TG.WXCRM.V4/WxService/WeWorkFileUpload.ashx.cs

102 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// WeWorkFileUpload 的摘要说明
/// </summary>
public class WeWorkFileUpload : IHttpHandler
{
private readonly PubSub _sub = new PubSub();
private IWeWork_MsgKey msgkey = NinjectControllerFactory.ninjectKernel.Get<IWeWork_MsgKey>();
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<string> rdb = new RedisString<string>(redisKey);
TimeSpan span = new TimeSpan(30 * 6, 0, 0, 0, 0);//保留180天
rdb.Set(fileWebUrl, span);
PushRedisAndPublish(model, "weworkfile");
}
private void PushRedisAndPublish<T>(T model, string key)
{
//写入队列
var result = new RedisList<T>().LeftPush(key, model);
if (result > 0)
{
//发送订阅通知
_sub.Publish("sub:" + key, "");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}