Files
2026-05-21 12:51:16 +08:00

67 lines
1.2 KiB
Go

package upscayl
import (
"context"
"fmt"
"image"
"os"
"os/exec"
"time"
_ "image/jpeg"
_ "image/png"
_ "golang.org/x/image/webp"
"trle5.xyz/upscayl-server/configs"
)
type Image struct {
Format string
Size int64
Width int
Height int
Second int
}
func Run(ctx context.Context, p Params) (*Image, error) {
start := time.Now()
err := p.Validate()
if err != nil {
return nil, fmt.Errorf("failed to validation params: %w", err)
}
err = exec.CommandContext(ctx, configs.UpscaylPath, p.ToArgs()...).Run()
if err != nil {
return nil, fmt.Errorf("failed to run upscayl: %w", err)
}
f, err := os.Open(p.Output)
if err != nil {
return nil, fmt.Errorf("failed to open output image: %w", err)
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("failed to get output image info: %w", err)
}
if stat.Size() == 0 {
return nil, fmt.Errorf("file is empty")
}
cfg, format, err := image.DecodeConfig(f)
if err != nil {
return nil, fmt.Errorf("failed to decode output image config: %w", err)
}
return &Image{
Size: stat.Size(),
Format: format,
Width: cfg.Width,
Height: cfg.Height,
Second: int(time.Since(start).Seconds()),
}, nil
}