Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement imgur.io resolving #365

Merged
merged 11 commits into from
Oct 22, 2022
Merged
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch API",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/api"
}
]
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Dev: Improve FrankerFaceZ tests. (#295)
- Dev: Improve Livestreamfails tests. (#297, #301)
- Dev: Improve default resolver tests. (#300)
- Dev: Resolve imgur.io links (#365)
pajlada marked this conversation as resolved.
Show resolved Hide resolved

## 1.2.3

Expand Down
6 changes: 4 additions & 2 deletions internal/logger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ var (
)

func FromContext(ctx context.Context) Logger {
if v := ctx.Value(contextKey); v != nil {
return v.(Logger)
if ctx != nil {
if v := ctx.Value(contextKey); v != nil {
return v.(Logger)
}
}

log.Fatal("No logger found in context")
Expand Down
3 changes: 2 additions & 1 deletion internal/resolvers/default/link_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ func (r *LinkResolver) HandleRequest(w http.ResponseWriter, req *http.Request) {

resolverHits.WithLabelValues(m.Name()).Inc()

if err != nil {
if err != nil || data == nil {
log.Errorw("Error in custom resolver, falling back to default",
"name", m.Name(),
"url", requestUrl,
"error", err,
"data", data,
)
break
}
Expand Down
16 changes: 15 additions & 1 deletion internal/resolvers/imgur/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/koffeinsource/go-imgur"
)

var VALID_IMGUR_DOMAINS = []string{"imgur.com", "imgur.io"}

type ImgurClient interface {
GetInfoFromURL(urlString string) (*imgur.GenericInfo, int, error)
}
Expand All @@ -27,7 +29,19 @@ type Resolver struct {
}

func (r *Resolver) Check(ctx context.Context, url *url.URL) (context.Context, bool) {
return ctx, utils.IsSubdomainOf(url, "imgur.com")
for _, domain := range VALID_IMGUR_DOMAINS {
result := utils.IsSubdomainOf(url, domain)
if result {
return nil, result
}

result = utils.IsDomain(url, domain)
if result {
return nil, result
}
}

return ctx, false
gempir marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *Resolver) Run(ctx context.Context, url *url.URL, req *http.Request) (*cache.Response, error) {
Expand Down