Skip to content

Commit

Permalink
[levels] adds uploading of personal backgrounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Mar 2, 2018
1 parent 42916e2 commit abb2f6f
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 38 deletions.
32 changes: 26 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion _assets/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,13 @@
"roles-grant-create-success": "I granted the user the user `%s` (`#%s`) the role `%s` (`#%s`).",
"roles-deny-error-denying": "You are already granting this user this role.",
"roles-deny-remove-success": "I removed the role deny for the user `%s` (`#%s`) for the role `%s` (`#%s`).",
"roles-deny-create-success": "I denied the user the user `%s` (`#%s`) the role `%s` (`#%s`)."
"roles-deny-create-success": "I denied the user the user `%s` (`#%s`) the role `%s` (`#%s`).",
"user-background-wrong-dimensions": "The picture does not fit the requirements. <a:ablobweary:394026914479865856>\nPlease upload a 400x300px smaller than 2MB picture.",
"user-background-not-safe": "Looks like your picture might contain explicit content. <a:ablobshocked:394026914076950539>\nIf this is a false alarm please contact a Staff member: <https://discord.is/Robyul>.",
"user-background-success": "I successfully set your new background! <a:ablobsunglasses:393869335657054210> \nCheck it out: `%sprofile`!",
"background-setlog-success": "I set the background log channel!",
"user-background-upload-failed": "Something went wrong trying to process your image. <a:ablobcry:393869333740126219>\nPlease try it again in a few minutes.",
"user-reset-success": "The background for %s has been reset."
},
"gallery": {
"add-success": "Gallery successfully added. <:blobokhand:317032017164238848>",
Expand Down
61 changes: 61 additions & 0 deletions helpers/cloudvision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package helpers

import (
"context"
"os"

"io"

vision "cloud.google.com/go/vision/apiv1"
vision2 "google.golang.org/genproto/googleapis/cloud/vision/v1"
)

var visionClient *vision.ImageAnnotatorClient

func PictureIsSafe(reader io.Reader) (safe bool) {
var err error
ctx := context.Background()

if visionClient == nil {
os.Setenv(
"GOOGLE_APPLICATION_CREDENTIALS",
GetConfig().Path("google.client_credentials_json_location").Data().(string),
)

visionClient, err = vision.NewImageAnnotatorClient(ctx)
if err != nil {
RelaxLog(err)
return false
}
}

image, err := vision.NewImageFromReader(reader)
if err != nil {
RelaxLog(err)
return false
}

safeData, err := visionClient.DetectSafeSearch(ctx, image, nil)
if err != nil {
RelaxLog(err)
return false
}

if safeData.GetAdult() == vision2.Likelihood_LIKELY || safeData.GetAdult() == vision2.Likelihood_VERY_LIKELY {
return false
}

if safeData.GetMedical() == vision2.Likelihood_LIKELY || safeData.GetMedical() == vision2.Likelihood_VERY_LIKELY {
return false
}

if safeData.GetViolence() == vision2.Likelihood_LIKELY || safeData.GetViolence() == vision2.Likelihood_VERY_LIKELY {
return false
}

if safeData.GetRacy() == vision2.Likelihood_LIKELY || safeData.GetRacy() == vision2.Likelihood_VERY_LIKELY {
return false
}

return true
}
53 changes: 53 additions & 0 deletions helpers/hosting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package helpers

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"

"time"

"net/url"

"github.com/Seklfreak/Robyul2/cache"
"github.com/pkg/errors"
)

func UploadImage(imageData []byte) (hostedUrl string, err error) {
client := &http.Client{
Timeout: 60 * time.Second,
}

parameters := url.Values{"image": {base64.StdEncoding.EncodeToString(imageData)}}

req, err := http.NewRequest("POST", "https://api.imgur.com/3/image", strings.NewReader(parameters.Encode()))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "Client-ID "+GetConfig().Path("imgur.client_id").Data().(string))
res, err := client.Do(req)
if err != nil {
return "", err
}

var imgurResponse struct {
Data struct {
Link string `json:"link"`
Error string `json:"error"`
} `json:"data"`
Status int `json:"status"`
Success bool `json:"success"`
}

json.NewDecoder(res.Body).Decode(&imgurResponse)

if imgurResponse.Success == false {
return "", errors.New(fmt.Sprintf("Imgur API Error: %d (%s)", imgurResponse.Status, fmt.Sprintf("%#v", imgurResponse.Data.Error)))
}

cache.GetLogger().WithField("module", "levels").Info("uploaded a picture to imgur: " + imgurResponse.Data.Link)
return imgurResponse.Data.Link, nil
}
2 changes: 2 additions & 0 deletions models/levels_serverusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

const (
LevelsServerusersTable MongoDbCollection = "levels_serverusers"

UserProfileBackgroundLogChannelKey = "profile-background:log:channel-id"
)

type LevelsServerusersEntry struct {
Expand Down
Loading

0 comments on commit abb2f6f

Please sign in to comment.