Used a more efficient image resizing library
This commit is contained in:
11
lib/img.go
11
lib/img.go
@ -4,35 +4,34 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/anthonynsimon/bild/transform"
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
func ResizeImg(file multipart.File, width int, height int) ([]byte, error) {
|
||||
// Read file content
|
||||
fileContent, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, errors.New("Error reading image file")
|
||||
return nil, errors.New("error reading image file")
|
||||
}
|
||||
|
||||
// Decode image
|
||||
img, _, err := image.Decode(bytes.NewReader(fileContent))
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return nil, errors.New("Error decoding image")
|
||||
return nil, errors.New("error decoding image")
|
||||
}
|
||||
|
||||
// Resize the image
|
||||
resizedImg := transform.Resize(img, width, height, transform.Linear)
|
||||
resizedImg := imaging.Resize(img, width, height, imaging.Lanczos)
|
||||
|
||||
// Encode the resized image as PNG
|
||||
buf := new(bytes.Buffer)
|
||||
if err := png.Encode(buf, resizedImg); err != nil {
|
||||
return nil, errors.New("Error encoding image to PNG")
|
||||
return nil, errors.New("error encoding image to PNG")
|
||||
}
|
||||
|
||||
// Return the resized image as response
|
||||
|
@ -3,25 +3,37 @@ package lib
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"atri.dad/lib/pubsub"
|
||||
"github.com/zmb3/spotify"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
var spotifyOAuth2Endpoint = oauth2.Endpoint{
|
||||
TokenURL: "https://accounts.spotify.com/api/token",
|
||||
AuthURL: "https://accounts.spotify.com/authorize",
|
||||
var (
|
||||
spotifyOAuth2Endpoint = oauth2.Endpoint{
|
||||
TokenURL: "https://accounts.spotify.com/api/token",
|
||||
AuthURL: "https://accounts.spotify.com/authorize",
|
||||
}
|
||||
config *oauth2.Config
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func GetOAuth2Config(clientID string, clientSecret string) *oauth2.Config {
|
||||
once.Do(func() {
|
||||
config = &oauth2.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
Scopes: []string{spotify.ScopeUserReadCurrentlyPlaying},
|
||||
Endpoint: spotifyOAuth2Endpoint,
|
||||
}
|
||||
})
|
||||
return config
|
||||
}
|
||||
|
||||
func GetCurrentlyPlayingTrack(clientID string, clientSecret string, refreshToken string) (*spotify.CurrentlyPlaying, error) {
|
||||
// OAuth2 config
|
||||
config := &oauth2.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
Scopes: []string{spotify.ScopeUserReadCurrentlyPlaying},
|
||||
Endpoint: spotifyOAuth2Endpoint,
|
||||
}
|
||||
config := GetOAuth2Config(clientID, clientSecret)
|
||||
|
||||
// Token source
|
||||
tokenSource := config.TokenSource(context.Background(), &oauth2.Token{RefreshToken: refreshToken})
|
||||
|
Reference in New Issue
Block a user