Exemplo n.º 1
0
        public CommonResponse GetProfile(string QueryUserID, string ClaimsUserID)
        {
            var user = _users.GetProfile(QueryUserID);

            if (user == null)
            {
                return new CommonResponse {
                           StatusCode = -1
                }
            }
            ;
            else
            {
                var applied = ApplyPrivacyToUserProfile(user, _users.GetProfilePrivacy(user.Userid), QueryUserID == ClaimsUserID);

                return(TypeMerger.MergeProperties(new GetprofileResponse {
                    StatusCode = 0
                }, applied));
            }
        }
Exemplo n.º 2
0
        public async Task <JsonResult> UpdateAvatar()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(HttpContext.Request.ContentType))
            {
                return(new JsonResult(new CommonResponse {
                    StatusCode = -1
                }));                                                           // 没有在Header处声明Multipart/form-data.
            }
            var UserID = User.Claims.ToList()[0].Value;

            try
            {
                var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), 70);
                var reader   = new MultipartReader(boundary, Request.Body);
                var section  = await reader.ReadNextSectionAsync();

                string fileExt = string.Empty;
                while (section != null)
                {
                    ContentDispositionHeaderValue contentDisposition;
                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            var fileName = contentDisposition.FileName.HasValue ? contentDisposition.FileName.Value.Trim() : string.Empty;
                            if (string.IsNullOrEmpty(fileName))
                            {
                                return(new JsonResult(new CommonResponse()
                                {
                                    StatusCode = -5
                                }));                                                             // 没有fileName
                            }

                            var fileNamez = fileName.Split(".");
                            if (fileNamez.Length < 2)
                            {
                                return(new JsonResult(new CommonResponse()
                                {
                                    StatusCode = -6
                                }));                                                                                   // fileName没有后缀
                            }
                            fileExt = fileNamez[1];

                            var path = Path.Combine(_resolver.GetAvatar(), $"{UserID}.{fileExt}");
                            using (var targetStream = System.IO.File.Create(path))
                            {
                                await section.Body.CopyToAsync(targetStream);
                            }
                        }
                    }
                    section = await reader.ReadNextSectionAsync();
                }

                var CurrentUserProfile = _profDb.GetProfile(UserID);
                CurrentUserProfile.Avatar = $"{UserID}.{fileExt}";

                return(new JsonResult(await _profile.ModifyProfile(UserID, CurrentUserProfile)));
            }
            catch (InvalidDataException exceed)
            {
                System.Console.WriteLine(exceed.Message);
                return(new JsonResult(new CommonResponse {
                    StatusCode = -2
                }));                                                           // 上传的图片大小超过了2MB
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message); // 服务器的其他未知错误。
                return(new JsonResult(new CommonResponse {
                    StatusCode = -3
                }));                                                           // 服务器发生未知错误
            }
        }