Beispiel #1
0
        public async Task <System.Web.Http.IHttpActionResult> UpdateImage()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var user = User.Identity.Name;

            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            var fileManager = new AzureFileManager();

            foreach (var file in provider.Contents)
            {
                var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                var buffer   = await file.ReadAsByteArrayAsync();

                //Do whatever you want with filename and its binary data.

                var result = fileManager.UploadFileAsync(buffer, $"{user}.png");//pass file stream

                if (!string.IsNullOrEmpty(result.Result))
                {
                    return(BadRequest(result.Result));
                }
            }

            return(Ok());
        }
Beispiel #2
0
        public async Task <IHttpActionResult> Upload(long taskId)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var task = TaskManager.GetTask(taskId);

            if (task == null)
            {
                return(NotFound());
            }

            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            //var fileManager = new AwsFileManager(); //amazon
            var fileManager = new AzureFileManager();

            foreach (var file in provider.Contents)
            {
                var filename = DateTime.Now.ToString("HHmmSS") + file.Headers.ContentDisposition.FileName.Trim('\"');
                //var filename = "asdsad";
                var buffer = await file.ReadAsByteArrayAsync();

                //var filename = model.file.FileName/*file.Headers.ContentDisposition.FileName*/.Trim('\"') + DateTime.Now.ToString("HH:mm:SS");
                //var buffer = ReadFully(model.file.InputStream);//await file.ReadAsByteArrayAsync();
                //Do whatever you want with filename and its binary data.

                var result = fileManager.UploadFileAsync(buffer, filename);//pass file stream

                if (string.IsNullOrEmpty(result.Result))
                {
                    return(BadRequest(result.Result));
                }

                //save attachment
                var attachment = new Attachment()
                {
                    FileName   = filename,
                    FileUrl    = result.Result,
                    FileType   = 0,
                    TaskId     = taskId,
                    LastUpdate = DateTime.Now
                };
                FileManager.SaveAttachment(attachment);
            }

            return(Ok());
        }
Beispiel #3
0
        public async Task <object> Register()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            byte[] buffer = null;
            string filename;
            string email           = string.Empty;
            string username        = string.Empty;
            string password        = string.Empty;
            string confirmpassword = string.Empty;

            var fileManager = new AzureFileManager();

            foreach (var file in provider.Contents)
            {
                if (file.Headers.ContentDisposition.Name.Contains("email"))
                {
                    email = await file.ReadAsStringAsync();
                }
                else if (file.Headers.ContentDisposition.Name.Contains("username"))
                {
                    username = await file.ReadAsStringAsync();
                }
                else if (file.Headers.ContentDisposition.Name.Contains("password"))
                {
                    password = await file.ReadAsStringAsync();
                }
                else if (file.Headers.ContentDisposition.Name.Contains("confirmpassword"))
                {
                    confirmpassword = await file.ReadAsStringAsync();
                }
                else
                {
                    filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                    buffer   = await file.ReadAsByteArrayAsync();
                }
            }

            var model = new RegisterBindingModel()
            {
                email           = email,
                username        = username,
                password        = password,
                confirmpassword = confirmpassword,
            };

            logger.Log(LogLevel.Info, $"Register({model.email})");
            if (!ModelState.IsValid)
            {
                logger.Log(LogLevel.Error, $"Register({model.email}). Error: model state is not invalid");
                return(BadRequest(ModelState));
            }

            //var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
            var userInfo = UserManager2.CreateUserInfo("", username, "", "", "");

            var user = new User()
            {
                UserName      = model.email,
                PasswordHash  = model.password,
                IsActive      = true,
                UserInfoId    = userInfo.Id,
                UserType      = (int)UserTypeEnum.Client,
                AccountPlanId = (int)AccountPlanEnum.Start,
                Registration  = DateTime.Now,
                LastUpdate    = DateTime.Now
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            user = UserManager2.Create(user);
            //SignInManager.SignIn(user, false, false);
            ClaimsIdentity oAuthIdentity = await UserManager.CreateIdentityAsync(user, OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookiesIdentity = await UserManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);

            Authentication.SignIn(properties, oAuthIdentity, cookiesIdentity);

            var token = GetToken(model.email, model.password);

            //avatar
            var uploadResult = fileManager.UploadFileAsync(buffer, $"{user.Id}.png");//pass file stream

            if (string.IsNullOrEmpty(uploadResult.Result))
            {
                return(BadRequest(uploadResult.Result));
            }


            var info = UserManager2.FindUserInfo(user.UserInfoId);

            info.PhotoUrl = uploadResult.Result;
            UserManager2.UpdateInfo(info);
            try
            {
                GroupManager.CreateFavorites(user.Id);
            }
            catch (Exception ex)
            { }

            var usermodel = UserManager2.GetUserModel(user.UserName);

            return(new
            {
                token = token,
                user = usermodel
            });
        }