2018-01-23 10:48:44 +00:00
|
|
|
package common
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
import (
|
|
|
|
"image"
|
2018-07-28 14:51:24 +00:00
|
|
|
"image/gif"
|
2018-07-28 12:52:23 +00:00
|
|
|
"image/jpeg"
|
2019-10-28 07:46:14 +00:00
|
|
|
"image/png"
|
2018-07-28 12:52:23 +00:00
|
|
|
"os"
|
2018-07-29 04:34:28 +00:00
|
|
|
"strconv"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
2018-07-29 04:34:28 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-07-28 12:52:23 +00:00
|
|
|
)
|
|
|
|
|
2018-07-29 04:34:28 +00:00
|
|
|
func ThumbTask(thumbChan chan bool) {
|
|
|
|
for {
|
|
|
|
// Put this goroutine to sleep until we have work to do
|
|
|
|
<-thumbChan
|
|
|
|
|
|
|
|
// TODO: Use a real queue
|
2019-06-09 03:21:48 +00:00
|
|
|
// TODO: Transactions? Self-repairing?
|
2018-08-04 11:46:36 +00:00
|
|
|
acc := qgen.NewAcc()
|
2018-07-29 04:34:28 +00:00
|
|
|
err := acc.Select("users_avatar_queue").Columns("uid").Limit("0,5").EachInt(func(uid int) error {
|
|
|
|
// TODO: Do a bulk user fetch instead?
|
|
|
|
user, err := Users.Get(uid)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has the avatar been removed or already been processed by the thumbnailer?
|
|
|
|
if len(user.RawAvatar) < 2 || user.RawAvatar[1] == '.' {
|
|
|
|
_, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid)
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-05 04:46:43 +00:00
|
|
|
_, err = os.Stat("./uploads/avatar_" + strconv.Itoa(user.ID) + user.RawAvatar)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
_, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid)
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2018-07-29 04:34:28 +00:00
|
|
|
// This means it's an external image, they aren't currently implemented, but this is here for when they are
|
|
|
|
if user.RawAvatar[0] != '.' {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
/*if user.RawAvatar == ".gif" {
|
|
|
|
return nil
|
|
|
|
}*/
|
2019-10-30 02:51:23 +00:00
|
|
|
if user.RawAvatar != ".png" && user.RawAvatar != ".jpg" && user.RawAvatar != ".jpeg" && user.RawAvatar != ".jfif" && user.RawAvatar != ".gif" {
|
2018-07-29 04:34:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Thumbnailer.Resize(user.RawAvatar[1:], "./uploads/avatar_"+strconv.Itoa(user.ID)+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_tmp"+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_w48"+user.RawAvatar, 48)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = user.ChangeAvatar("." + user.RawAvatar)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
_, err = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid)
|
|
|
|
return errors.WithStack(err)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2019-10-28 07:46:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
err := acc.Select("attach_image_queue").Columns("attachID").Limit("0,5").EachInt(func(attachID int) error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
_, err = acc.Delete("attach_image_queue").Where("attachID = ?").Run(uid)
|
|
|
|
}
|
|
|
|
*/
|
2018-08-04 11:46:36 +00:00
|
|
|
if err = acc.FirstError(); err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2018-07-29 04:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 10:48:44 +00:00
|
|
|
var Thumbnailer ThumbnailerInt
|
|
|
|
|
|
|
|
type ThumbnailerInt interface {
|
2018-07-28 14:51:24 +00:00
|
|
|
Resize(format string, inPath string, tmpPath string, outPath string, width int) error
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RezThumbnailer struct {
|
|
|
|
}
|
|
|
|
|
2018-07-28 14:51:24 +00:00
|
|
|
func (thumb *RezThumbnailer) Resize(format string, inPath string, tmpPath string, outPath string, width int) error {
|
2018-01-23 10:48:44 +00:00
|
|
|
// TODO: Sniff the aspect ratio of the image and calculate the dest height accordingly, bug make sure it isn't excessively high
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 14:51:24 +00:00
|
|
|
func (thumb *RezThumbnailer) resize(format string, inPath string, outPath string, width int, height int) error {
|
2018-01-23 10:48:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
// ! Note: CaireThumbnailer can't handle gifs, so we'll have to either cap their sizes or have another resizer deal with them
|
|
|
|
type CaireThumbnailer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCaireThumbnailer() *CaireThumbnailer {
|
|
|
|
return &CaireThumbnailer{}
|
|
|
|
}
|
|
|
|
|
2018-07-28 14:51:24 +00:00
|
|
|
func precodeImage(format string, inPath string, tmpPath string) error {
|
2018-07-28 12:52:23 +00:00
|
|
|
imageFile, err := os.Open(inPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer imageFile.Close()
|
|
|
|
|
|
|
|
img, _, err := image.Decode(imageFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile, err := os.Create(tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
2018-01-23 10:48:44 +00:00
|
|
|
|
2018-07-28 14:51:24 +00:00
|
|
|
// TODO: Make sure animated gifs work after being encoded
|
|
|
|
if format == "gif" {
|
|
|
|
return gif.Encode(outFile, img, nil)
|
2019-10-28 07:46:14 +00:00
|
|
|
} else if format == "png" {
|
|
|
|
return png.Encode(outFile, img)
|
2018-07-28 14:51:24 +00:00
|
|
|
}
|
2018-07-28 12:52:23 +00:00
|
|
|
return jpeg.Encode(outFile, img, nil)
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 14:51:24 +00:00
|
|
|
func (thumb *CaireThumbnailer) Resize(format string, inPath string, tmpPath string, outPath string, width int) error {
|
|
|
|
err := precodeImage(format, inPath, tmpPath)
|
2018-07-28 12:52:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// TODO: Caire doesn't work. Try something else. Or get them to fix the index out of range. We get enough wins from re-encoding as jpeg anyway
|
|
|
|
/*imageFile, err := os.Open(tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer imageFile.Close()
|
|
|
|
|
|
|
|
outFile, err := os.Create(outPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
p := &caire.Processor{NewWidth: width, Scale: true}
|
|
|
|
return p.Process(imageFile, outFile)*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
type LilliputThumbnailer struct {
|
2018-01-23 10:48:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
*/
|