📄 portraithelper.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client.Base;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Common;
using Imps.Utils;
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
public class PortraitHelper
{
private bool _isDownloading;
private Imps.Client.Core.User _owner;
private PortraitQueue _queue = new PortraitQueue();
public PortraitHelper(Imps.Client.Core.User owner)
{
this._owner = owner;
}
private void AddDownloadQueue(PersonalInfoBase personalInfo, int priority)
{
lock (this._queue.SyncObj)
{
this._queue.Enqueue(personalInfo, priority);
this.BeginDownload();
}
}
public void AsyncGetPortrait(PersonalInfoBase personalInfo, DownloadPortraitPriority priority)
{
if (personalInfo != null)
{
Image portrait = this.GetPortrait(personalInfo, priority);
string crc = personalInfo.PortraitCrc;
string localPortraitCrc = personalInfo.LocalPortraitCrc;
if ((localPortraitCrc != crc) || ((localPortraitCrc == "0") && (crc == "0")))
{
personalInfo.SetPortrait(portrait, crc);
}
}
}
public void AsyncUploadPortrait(Image newPortrait, AsyncBizOperation op)
{
UploadPortraitObject state = new UploadPortraitObject(newPortrait, op);
ThreadPool.QueueUserWorkItem(new WaitCallback(this.uploadPortrait), state);
}
public void BeginDownload()
{
if ((!string.IsNullOrEmpty(this._owner.SsiCredential) && !this._isDownloading) && (this._queue.Count != 0))
{
this._isDownloading = true;
ThreadPool.QueueUserWorkItem(new WaitCallback(this.downloadPortrait), 0);
}
}
private void downloadPortrait(object data)
{
try
{
PortraitQueueItem item;
object syncObj;
if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
{
Thread.CurrentThread.Name = "Imps.Client.Pc.DownloadPortrait";
}
Image portrait = null;
Label_0022:
Monitor.Enter(syncObj = this._queue.SyncObj);
try
{
item = this._queue.Dequeue();
if (item == null)
{
return;
}
}
finally
{
Monitor.Exit(syncObj);
}
if (string.IsNullOrEmpty(this._owner.SsiCredential))
{
this._queue.Clear();
}
else
{
HttpWebRequest request = null;
HttpWebResponse response = null;
string url = string.Empty;
try
{
url = string.Format("{0}?Uri={1}&Size={2}&c={3}", new object[] { this._owner.Configuration.SystemSetting.PortraitSetting.UriDownloadPortrait, item.Uri.Raw, 0x60, this._owner.SsicUrlEncoded });
request = ConnectionFactory.CreateHttpWebRequest(url, this._owner, false, false);
request.Accept = "image/pjpeg;image/jpeg;image/bmp;image/x-windows-bmp;image/png;image/gif";
request.ContentLength = 0;
portrait = null;
using (response = ((HttpWebResponse) request.GetResponse()))
{
if (response.StatusCode == HttpStatusCode.OK)
{
portrait = Image.FromStream(response.GetResponseStream());
}
response.Close();
}
if (portrait != null)
{
item.PersonalInfo.SetPortrait(portrait, item.PersonalInfo.PortraitCrc);
if (this._owner.Configuration.UserSetting.SaveMyInfo)
{
this._owner.PersistentManager.SavePortrait(item.Uri, portrait, item.PersonalInfo.PortraitCrc.GetHashCode());
}
}
goto Label_0022;
}
catch (WebException exception)
{
HttpWebResponse response2 = exception.Response as HttpWebResponse;
if ((response2 != null) && (response2.StatusCode == HttpStatusCode.NotFound))
{
ClientLogger.WriteBizOperation("下载头像", string.Format("不存在,url={0},crc={1}", url, item.PersonalInfo.PortraitCrc));
}
else
{
ClientLogger.WriteBizOperation("下载头像", exception.ToString(), 10);
}
if (response2 != null)
{
response2.Close();
}
goto Label_0022;
}
catch (Exception exception2)
{
ClientLogger.WriteBizOperation("下载头像", exception2.ToString(), 20);
goto Label_0022;
}
finally
{
if (response != null)
{
response.Close();
}
else if (request != null)
{
request.Abort();
}
}
}
}
finally
{
this._isDownloading = false;
}
}
private static Image GetDefaultPortrait(Gender gender)
{
switch (gender)
{
case Gender.Male:
return ImpsPortrait.GetBoyPortrait(0x60, 0x60);
case Gender.Female:
return ImpsPortrait.GetGirlPortrait(0x60, 0x60);
}
return ImpsPortrait.GetDefaultPortrait(0x60, 0x60);
}
internal Image GetPortrait(PersonalInfoBase personalInfo)
{
return this.GetPortrait(personalInfo, DownloadPortraitPriority.Normal);
}
internal Image GetPortrait(PersonalInfoBase personalInfo, DownloadPortraitPriority priority)
{
if (personalInfo == null)
{
return null;
}
Image defaultPortrait = null;
try
{
IicUri uri;
if (personalInfo is Imps.Client.Core.ContactInfo)
{
Imps.Client.Core.ContactInfo info = personalInfo as Imps.Client.Core.ContactInfo;
if (info.Owner.Type == ContactType.MobileBuddy)
{
return ImpsPortrait.GetMobilePortrait(0x60, 0x60);
}
if (info.Provision == 0)
{
return ImpsPortrait.GetMobilePortrait(0x60, 0x60);
}
if (info.Owner.Type == ContactType.Vodafone)
{
return ImpsPortrait.GetVodafonePortrait(0x60, 0x60);
}
uri = info.Owner.Uri;
}
else
{
uri = ((UserInfo) personalInfo).Owner.Uri;
}
if (!IsCrcEmpty(personalInfo.PortraitCrc))
{
int crc;
defaultPortrait = this._owner.PersistentManager.LoadPortrait(uri, out crc);
if (crc != personalInfo.PortraitCrc.GetHashCode())
{
this.AddDownloadQueue(personalInfo, (int) priority);
}
}
}
catch
{
defaultPortrait = null;
}
if (defaultPortrait == null)
{
defaultPortrait = GetDefaultPortrait(personalInfo.InnerGender);
}
return defaultPortrait;
}
internal static bool IsCrcEmpty(string crc)
{
if (!string.IsNullOrEmpty(crc))
{
return string.Equals(crc, "0");
}
return true;
}
public void StopDownload()
{
try
{
lock (this._queue.SyncObj)
{
this._queue.Clear();
}
}
catch
{
}
}
private void uploadPortrait(object obj)
{
if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
{
Thread.CurrentThread.Name = "Imps.Client.Pc.UploadPortrait";
}
UploadPortraitObject obj2 = (UploadPortraitObject) obj;
Image input = obj2.Portrait;
AsyncBizOperation op = obj2.AsyncBizOperation;
if (input != null)
{
MemoryStream ms = new MemoryStream();
ImageHelper.TryGetHighQualifiedJpegStream(ms, input, 0x60);
if (ms.Length != 0)
{
if (ms.Length > 0x32000)
{
this._owner.FireImpsErrorInUiThread(new ImpsErrorEventArgs(StringTable.Portrait.MsgTooBig, 0x6f), op);
ms.Dispose();
}
else
{
HttpWebRequest request = ConnectionFactory.CreateHttpWebRequest(this._owner.Configuration.SystemSetting.PortraitSetting.UriUploadPortrait, this._owner, true, true);
request.ContentType = "image/jpeg";
using (Stream stream2 = request.GetRequestStream())
{
byte[] buffer = ms.GetBuffer();
stream2.Write(buffer, 0, buffer.Length);
}
ms.Dispose();
try
{
using (HttpWebResponse response = ((HttpWebResponse) request.GetResponse()))
{
string text;
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
text = reader.ReadToEnd();
reader.Close();
}
this._owner.PersonalInfo.PortraitCrc = text;
response.Close();
}
this.AddDownloadQueue(this._owner.PersonalInfo, 100);
}
catch (WebException exception)
{
ClientLogger.WriteGeneral("上载头像", exception.ToString(), 20);
string summary = null;
HttpWebResponse response2 = exception.Response as HttpWebResponse;
if (response2 != null)
{
switch (((int) response2.StatusCode))
{
case 400:
case 0x19f:
summary = StringTable.Portrait.UploadIvalidPortrait;
break;
case 0x191:
summary = StringTable.User.SsiCredentialExpired;
break;
}
response2.Close();
}
if (string.IsNullOrEmpty(summary))
{
summary = StringTable.Portrait.UploadFailed;
}
this._owner.FireImpsErrorInUiThread(new ImpsErrorEventArgs(summary), op);
}
catch (Exception exception2)
{
this._owner.FireImpsErrorInUiThread(new ImpsErrorEventArgs(StringTable.Portrait.UploadFailed), op);
ClientLogger.WriteGeneral("上载头像", exception2.ToString(), 20);
}
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -