using learun.util;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace learun.wechat
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.11.06
/// 描 述:媒体获取
///
public class MediaGet : OperationRequestBase
{
private string url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={0}";
///
///
///
///
protected override string Url()
{
return string.Format(url, media_id);
}
///
/// 媒体文件id
///
///
[IsNotNull]
public string media_id { get; set; }
///
/// 图片保存路径
///
///
[IsNotNull]
public string path { get; set; }
///
///
///
///
///
///
protected override Task HttpSend(IHttpSend httpSend, string url)
{
return httpSend.Send(url, path);
}
///
///
///
///
///
protected override MediaGetResult GetDeserializeObject(string result)
{
if (string.IsNullOrEmpty(path))
{
path = "d:\\";
}
try
{
var re = base.GetDeserializeObject(result);
if (re != null && re.errcode != 0)
{
return re;
}
}
catch (Exception)
{
}
var header = result.ToObject>();
Regex regImg = new Regex("\"(?.*)\"", RegexOptions.IgnoreCase);
MatchCollection matches = regImg.Matches(header["Content-disposition"]);
string fileName = matches[0].Groups["fileName"].Value;
string filepath = path.TrimEnd('\\') + "\\" + fileName;
return new MediaGetResult()
{
errcode = 0,
errmsg = "",
FilePath = filepath,
FileName = fileName,
FileType = header["Content-Type"]
};
}
}
}