// OauthGithub link connection and user. func OauthGithub(c *gin.Context) (int, error) { var authResponse oauth2.AuthResponse var oauthUser OauthUser c.BindWith(&authResponse, binding.Form) log.Debugf("oauthRedirect form: %v", authResponse) response, token, err := oauth2.OauthRequest(github.RequestURL, github.Config, authResponse) if err != nil { log.Error("get response error") return http.StatusInternalServerError, err } githubUser, err := SetGithubUser(response) if err != nil { log.Error("SetGithubUser error") return http.StatusInternalServerError, err } modelHelper.AssignValue(&oauthUser, githubUser) oauthUser.Id = strconv.Itoa(githubUser.UserId) log.Debugf("\noauthUser item : %v", oauthUser) status, err := LoginOrCreateOauthUser(c, &oauthUser, github.ProviderId, token) if err != nil { log.Errorf("LoginOrCreateOauthUser error: %d", status) return status, err } return http.StatusSeeOther, nil }
// CreateOauthUser creates oauth user. func CreateOauthUser(c *gin.Context, oauthUser *OauthUser, connection *model.Connection) (model.User, int, error) { var registrationForm userService.RegistrationForm var user model.User modelHelper.AssignValue(®istrationForm, oauthUser) registrationForm.Password = random.GenerateRandomString(12) if len(registrationForm.Username) == 0 { if len(registrationForm.Email) > 0 { registrationForm.Username = strings.Split(registrationForm.Email, "@")[0] } else { registrationForm.Username = "******" } } registrationForm.Username = userService.SuggestUsername(registrationForm.Username) generatedPassword, err := bcrypt.GenerateFromPassword([]byte(registrationForm.Password), 10) if err != nil { return user, http.StatusInternalServerError, errors.New("Password not generated.") } registrationForm.Password = string(generatedPassword) currentUser, err := userService.CurrentUser(c) if err != nil { log.Errorf("currentUser : %v", currentUser) user, err = userService.CreateUserFromForm(registrationForm) if err != nil { return user, http.StatusInternalServerError, errors.New("User is not created.") } } else { if db.ORM.Where("id = ?", currentUser.Id).First(&user).RecordNotFound() { return user, http.StatusInternalServerError, errors.New("User is not found.") } } db.ORM.Model(&user).Association("Connections").Append(connection) if db.ORM.Save(&user).Error != nil { return user, http.StatusInternalServerError, errors.New("Connection not appended to user.") } return user, http.StatusOK, nil }
// UploadImageFile uploads an image file to a storage. func UploadImageFile(s3UploadPath string, part *multipart.Part) error { mediatype, _, _ := mime.ParseMediaType(part.Header.Get("Content-Type")) log.Debugf("params %s", mediatype) log.Debug("fileName : " + part.FileName()) inbuf, err := ioutil.ReadAll(part) if err != nil { // log.CheckErrorWithMessage(err, "Image read failed.") return err } // Image resize is a bottleneck. How can we improve this? // https://github.com/fawick/speedtest-resize said vipsthumbnail is fastest one. // Currenctly goyangi uses vips(https://github.com/DAddYE/vips). // dst, _ := image.ResizeMedium(mediatype, bytes.NewReader(inBuf)) var dst, dstLarge, dstMedium, dstThumbnail *bytes.Buffer dst = bytes.NewBuffer(inbuf) buf, err := image.ResizeLargeVips(inbuf) if err != nil { // log.CheckErrorWithMessage(err, "Image resizing failed.") log.Errorf("Image large resizing failed. %s", err.Error()) dstLarge = nil } else { dstLarge = bytes.NewBuffer(buf) mbuf, err := image.ResizeMediumVips(buf) if err != nil { dstMedium = nil log.Errorf("Image medium resizing failed. %s", err.Error()) } else { dstMedium = bytes.NewBuffer(mbuf) tbuf, err := image.ResizeThumbnailVips(mbuf) if err != nil { dstThumbnail = nil log.Errorf("Image small resizing failed. %s", err.Error()) } else { dstThumbnail = bytes.NewBuffer(tbuf) } } } // var thumbDst *bytes.Buffer // thumbBuf, err := image.ResizeThumbnailVips(buf) // if err != nil { // log.CheckErrorWithMessage(err, "Image thumbnailing failed.") // thumbDst = bytes.NewBuffer(buf) // } else { // thumbDst = bytes.NewBuffer(thumbBuf) // } basename := part.FileName() ext := filepath.Ext(basename) name := strings.TrimSuffix(basename, ext) originName := basename largeName := name + "_large" + ext mediumName := name + "_medium" + ext thumbnailName := name + "_thumbnail" + ext switch config.UploadTarget { case "LOCAL": err = file.SaveLocal(originName, dst) if dstLarge != nil { err = file.SaveLocal(largeName, dstLarge) } if dstMedium != nil { err = file.SaveLocal(mediumName, dstMedium) } if dstThumbnail != nil { err = file.SaveLocal(thumbnailName, dstThumbnail) } case "S3": switch config.Environment { case "DEVELOPMENT": fallthrough case "TEST": err = aws.PutToMyPublicTestBucket(s3UploadPath, originName, dst, mediatype) if dstLarge != nil { err = aws.PutToMyPublicTestBucket(s3UploadPath, largeName, dstLarge, mediatype) } if dstMedium != nil { err = aws.PutToMyPublicTestBucket(s3UploadPath, mediumName, dstMedium, mediatype) } if dstThumbnail != nil { err = aws.PutToMyPublicTestBucket(s3UploadPath, thumbnailName, dstThumbnail, mediatype) } case "PRODUCTION": err = aws.PutToMyPublicBucket(s3UploadPath, originName, dst, mediatype) if dstLarge != nil { err = aws.PutToMyPublicBucket(s3UploadPath, largeName, dstLarge, mediatype) } if dstMedium != nil { err = aws.PutToMyPublicBucket(s3UploadPath, mediumName, dstMedium, mediatype) } if dstThumbnail != nil { err = aws.PutToMyPublicBucket(s3UploadPath, thumbnailName, dstThumbnail, mediatype) } } // err = aws.PutToMyPublicBucket("images/", part.FileName()+"Thumbnail", thumbDst, mediatype) } if err != nil { // log.CheckErrorWithMessage(err, "Uploading failed.") return err } return nil }