mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-07-28 21:48:46 +00:00
Refactor auth.go and media.go to remove deprecated dependencies (#1128)
* Refactor `auth.go` and `media.go` to remove deprecated dependencies * Move the `text` dependency to direct ones in the `go.mod` --------- Co-authored-by: Konstantin Koval
This commit is contained in:
@@ -26,6 +26,7 @@ require (
|
||||
github.com/xor-gate/goexif2 v1.1.0
|
||||
golang.org/x/crypto v0.29.0
|
||||
golang.org/x/image v0.22.0
|
||||
golang.org/x/text v0.20.0
|
||||
gopkg.in/vansante/go-ffprobe.v2 v2.2.0
|
||||
gorm.io/driver/mysql v1.5.7
|
||||
gorm.io/driver/postgres v1.5.9
|
||||
@@ -59,7 +60,6 @@ require (
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.9.0 // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
golang.org/x/text v0.20.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -2,18 +2,18 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/99designs/gqlgen/handler"
|
||||
"github.com/99designs/gqlgen/graphql/handler/transport"
|
||||
"github.com/photoview/photoview/api/dataloader"
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var ErrUnauthorized = errors.New("unauthorized")
|
||||
var ErrUnauthorized = fmt.Errorf("unauthorized")
|
||||
|
||||
// A private key for context that only this package can access. This is important
|
||||
// to prevent collisions between different context uses
|
||||
@@ -59,7 +59,7 @@ func TokenFromBearer(bearer *string) (*string, error) {
|
||||
regex, _ := regexp.Compile("^(?i)Bearer ([a-zA-Z0-9]{24})$")
|
||||
matches := regex.FindStringSubmatch(*bearer)
|
||||
if len(matches) != 2 {
|
||||
return nil, errors.New("invalid bearer format")
|
||||
return nil, fmt.Errorf("invalid bearer format")
|
||||
}
|
||||
|
||||
token := matches[1]
|
||||
@@ -72,8 +72,8 @@ func UserFromContext(ctx context.Context) *models.User {
|
||||
return raw
|
||||
}
|
||||
|
||||
func AuthWebsocketInit(db *gorm.DB) func(context.Context, handler.InitPayload) (context.Context, error) {
|
||||
return func(ctx context.Context, initPayload handler.InitPayload) (context.Context, error) {
|
||||
func AuthWebsocketInit(db *gorm.DB) func(context.Context, transport.InitPayload) (context.Context, error) {
|
||||
return func(ctx context.Context, initPayload transport.InitPayload) (context.Context, error) {
|
||||
|
||||
bearer, exists := initPayload["Authorization"].(string)
|
||||
if !exists {
|
||||
@@ -90,7 +90,7 @@ func AuthWebsocketInit(db *gorm.DB) func(context.Context, handler.InitPayload) (
|
||||
// user, err := models.VerifyTokenAndGetUser(db, *token)
|
||||
if err != nil {
|
||||
log.Printf("Invalid token in websocket: %s\n", err)
|
||||
return nil, errors.New("invalid authorization token")
|
||||
return nil, fmt.Errorf("invalid authorization token")
|
||||
}
|
||||
|
||||
// put it in context
|
||||
|
||||
@@ -6,9 +6,7 @@ package resolvers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/photoview/photoview/api/dataloader"
|
||||
api "github.com/photoview/photoview/api/graphql"
|
||||
@@ -16,6 +14,8 @@ import (
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/photoview/photoview/api/graphql/models/actions"
|
||||
"github.com/photoview/photoview/api/scanner/face_detection"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
// Thumbnail is the resolver for the thumbnail field.
|
||||
@@ -80,7 +80,7 @@ func (r *mediaResolver) Favorite(ctx context.Context, obj *models.Media) (bool,
|
||||
|
||||
// Type is the resolver for the type field.
|
||||
func (r *mediaResolver) Type(ctx context.Context, obj *models.Media) (models.MediaType, error) {
|
||||
formattedType := models.MediaType(strings.Title(string(obj.Type)))
|
||||
formattedType := models.MediaType(cases.Title(language.Und).String(string(obj.Type)))
|
||||
return formattedType, nil
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ func (r *mutationResolver) FavoriteMedia(ctx context.Context, mediaID int, favor
|
||||
func (r *queryResolver) MyMedia(ctx context.Context, order *models.Ordering, paginate *models.Pagination) ([]*models.Media, error) {
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, errors.New("unauthorized")
|
||||
return nil, fmt.Errorf("unauthorized")
|
||||
}
|
||||
|
||||
return actions.MyMedia(r.DB(ctx), user, order, paginate)
|
||||
@@ -212,7 +212,7 @@ func (r *queryResolver) MediaList(ctx context.Context, ids []int) ([]*models.Med
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return nil, errors.New("no ids provided")
|
||||
return nil, fmt.Errorf("no ids provided")
|
||||
}
|
||||
|
||||
var media []*models.Media
|
||||
|
||||
Reference in New Issue
Block a user