using DAL.Redis;
using System;
using System.Configuration;
using System.IO;
using System.Web;
using WX.CRM.Common;
using WX.CRM.DAL.Redis;
using WX.CRM.Model.DTO;
namespace WxService
{
///
/// WxFileUpload 的摘要说明
///
public class WxFileUpload : IHttpHandler
{
private static string _shareName;
private static string _isShare;
private readonly PubSub _sub = new PubSub();
static WxFileUpload()
{
//用户名勿比指定共享服务器的IP或名称,否则会引起1312错误
_isShare = Utility.GetSettingByKey("IsShare");
if (!string.IsNullOrEmpty(_isShare))
{
_shareName = Utility.GetSettingByKey("NetUseShareName") ?? @"\\192.168.1.171\weixin";
string user = Utility.GetSettingByKey("NetUseUser") ?? @"192.168.1.171\admin";
string pwd = Utility.GetSettingByKey("NetUsePwd") ?? "read,./1";
NetUseHelper.Build(_shareName, user, pwd, string.Empty);//不指定盘符,避免引起盘符被占用的错误
}
}
public void ProcessRequest(HttpContext context)
{
//HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = true, message = "接收成功", url = "sys.png" }));
//return;
string msgSvrId = string.Empty;
try
{
//LogHelper.Info("===调用成功====");
context.Response.ContentType = "text/html";
string fileName = context.Request.Params["fileName"];
if (fileName.IndexOf("null.") > -1)
{
HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = true, message = "文件为空", url = fileName }));
}
if (string.IsNullOrEmpty(fileName))
throw new Exception("fileName不能为空!");
msgSvrId = context.Request.Params["msgSvrId"];
if (string.IsNullOrEmpty(msgSvrId))
throw new Exception("msgSvrId不能为空!");
//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;
if (string.IsNullOrEmpty(_isShare))
{
uploadFolder = (ConfigurationManager.AppSettings["uploadfile"] ?? "uploadfile") + "/" + yearMonth;
}
else
{
uploadFolder = _shareName + @"\WXFILE\uploadfile\" + yearMonth;
}
if (!Directory.Exists(uploadFolder))
{
Directory.CreateDirectory(uploadFolder);
}
string filePath = Path.Combine(uploadFolder, fileName);
file.SaveAs(filePath);
PushRedis(msgSvrId, fileName);
//HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = true, message = "接收成功", url = fileName }));
}
catch (Exception ex)
{
LogHelper.Error("msgSvrId:" + msgSvrId + ";错误:" + ex.ToString());
//HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(Utility.ObjectToJson(new { result = false, message = "接收失败:" + ex.Message }));
}
}
private void PushRedis(string msgSvrId, string fileName)
{
PushRedisAsync(msgSvrId, fileName);
}
private void PushRedisAsync(string msgSvrId, string fileName)
{
var redisKey = "wx:" + msgSvrId;
var fileWebUrl = "/uploadfile/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "/" + fileName;
var model = new wxfilepath
{
MsgSvrId = long.Parse(msgSvrId),
FileName = redisKey,
FileUrl = fileWebUrl,
CTime = DateTime.Now
};
RedisString rdb = new RedisString(redisKey);
TimeSpan span = new TimeSpan(30 * 6, 0, 0, 0, 0);//保留180天
rdb.Set(fileWebUrl, span);
PushRedisAndPublish(model, "filepath");
}
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;
}
}
}
}