-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[levels] adds uploading of personal backgrounds
- Loading branch information
Showing
6 changed files
with
355 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.