示例#1
0
        /// <summary>抓取远程头像</summary>
        /// <param name="user"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public virtual Boolean FetchAvatar(IManageUser user, String url = null)
        {
            if (url.IsNullOrEmpty())
            {
                url = user.GetValue("Avatar") as String;
            }
            //if (av.IsNullOrEmpty()) throw new Exception("用户头像不存在 " + user);

            // 尝试从用户链接获取头像地址
            if (url.IsNullOrEmpty() || !url.StartsWithIgnoreCase("http"))
            {
                var list = UserConnect.FindAllByUserID(user.ID);
                url = list.OrderByDescending(e => e.UpdateTime)
                      .Where(e => !e.Avatar.IsNullOrEmpty() && e.Avatar.StartsWithIgnoreCase("http"))
                      .FirstOrDefault()?.Avatar;
            }

            if (url.IsNullOrEmpty())
            {
                return(false);
            }
            if (!url.StartsWithIgnoreCase("http"))
            {
                return(false);
            }

            // 不要扩展名
            var set  = Setting.Current;
            var dest = set.AvatarPath.CombinePath(user.ID + ".png").GetBasePath();

            //// 头像是否已存在
            //if (File.Exists(dest)) return false;

            LogProvider.Provider?.WriteLog(user.GetType(), "抓取头像", $"{url} => {dest}", user.ID, user + "");

            dest.EnsureDirectory(true);

            try
            {
                //var wc = new WebClientX();
                //Task.Factory.StartNew(() => wc.DownloadFileAsync(url, av)).Wait(5000);

                var client = new HttpClient();
                var rs     = client.GetAsync(url).Result;
                var buf    = rs.Content.ReadAsByteArrayAsync().Result;
                File.WriteAllBytes(dest, buf);

                // 更新头像
                user.SetValue("Avatar", "/Sso/Avatar/" + user.ID);
                (user as IEntity)?.Update();

                return(true);
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
            }

            return(false);
        }
示例#2
0
        /// <summary>抓取远程头像</summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public virtual Boolean FetchAvatar(IManageUser user)
        {
            var av = user.GetValue("Avatar") as String;

            if (av.IsNullOrEmpty())
            {
                throw new Exception("用户头像不存在 " + user);
            }

            var url = av;

            if (!url.StartsWithIgnoreCase("http"))
            {
                return(false);
            }

            // 不要扩展名
            var set = Setting.Current;

            av = set.AvatarPath.CombinePath(user.ID + ".png").GetFullPath();

            // 头像是否已存在
            if (File.Exists(av))
            {
                return(false);
            }

            av.EnsureDirectory(true);

            try
            {
                //var wc = new WebClientX();
                //Task.Factory.StartNew(() => wc.DownloadFileAsync(url, av)).Wait(5000);

                var client = new HttpClient();
                var rs     = client.GetAsync(url).Result;
                var buf    = rs.Content.ReadAsByteArrayAsync().Result;
                File.WriteAllBytes(av, buf);

                // 更新头像
                user.SetValue("Avatar", "/Sso/Avatar/" + user.ID);

                return(true);
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
            }

            return(false);
        }