Blog.Admin.Web.Controllers.ProfileImageController.SaveUploadedFile C# (CSharp) Method

SaveUploadedFile() private method

private SaveUploadedFile ( System.Web.HttpPostedFileBase httpPostedFileBase, int userId, bool isBackground ) : void
httpPostedFileBase System.Web.HttpPostedFileBase
userId int
isBackground bool
return void
        private void SaveUploadedFile(HttpPostedFileBase httpPostedFileBase, int userId, bool isBackground)
        {
            if (httpPostedFileBase == null) throw new Exception("Image uploaded is missing!");

            var filename = Path.GetFileName(httpPostedFileBase.FileName);
            if (filename == null) throw new Exception("Image uploaded is missing!");

            var guid = Guid.NewGuid().ToString();
            filename = string.Format("{0}{1}", guid, Path.GetExtension(filename));

            var user = _usersResource.Get(userId);
            if (user == null) throw new Exception("User not found");
            if (user.Error != null) throw new Exception(user.Error.Message);

            var basepath = _configurationHelper.GetAppSettings("MediaLocation");
            var fullpath = Path.Combine(basepath, filename);
            httpPostedFileBase.SaveAs(fullpath);

            var album = isBackground ? "Background" : "Profile";

            var resultMedia = _mediaResource.AddAsContent(user, album, filename, fullpath,
                httpPostedFileBase.ContentType);

            if (!isBackground)
            {
                user.Picture = resultMedia;
            }
            else
            {
                user.Background = resultMedia;
            }

            var userResult = _usersResource.Update(user);
            if (userResult.Error != null)
            {
                throw new Exception(userResult.Error.Message);
            }
        }