2018-01-23 10:48:44 +00:00
|
|
|
package common
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
import (
|
2022-02-21 03:53:13 +00:00
|
|
|
"image"
|
|
|
|
"image/gif"
|
|
|
|
"image/jpeg"
|
|
|
|
"image/png"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2018-07-29 04:34:28 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
"golang.org/x/image/tiff"
|
2019-10-30 03:52:04 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
|
|
|
"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) {
|
2022-02-21 03:32:53 +00:00
|
|
|
defer EatPanics()
|
|
|
|
acc := qgen.NewAcc()
|
|
|
|
for {
|
|
|
|
// Put this goroutine to sleep until we have work to do
|
|
|
|
<-thumbChan
|
|
|
|
|
|
|
|
// TODO: Use a real queue
|
|
|
|
// TODO: Transactions? Self-repairing?
|
|
|
|
err := acc.Select("users_avatar_queue").Columns("uid").Limit("0,5").EachInt(func(uid int) error {
|
|
|
|
// TODO: Do a bulk user fetch instead?
|
|
|
|
u, 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(u.RawAvatar) < 2 || u.RawAvatar[1] == '.' {
|
|
|
|
_, _ = acc.Delete("users_avatar_queue").Where("uid=?").Run(uid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err = os.Stat("./uploads/avatar_" + strconv.Itoa(u.ID) + u.RawAvatar)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
_, _ = acc.Delete("users_avatar_queue").Where("uid=?").Run(uid)
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This means it's an external image, they aren't currently implemented, but this is here for when they are
|
|
|
|
if u.RawAvatar[0] != '.' {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
/*if user.RawAvatar == ".gif" {
|
|
|
|
return nil
|
|
|
|
}*/
|
|
|
|
canResize := func(ext string) bool {
|
|
|
|
// TODO: Fix tif and tiff extensions?
|
|
|
|
return ext == ".png" && ext == ".jpg" && ext == ".jpe" && ext == ".jpeg" && ext == ".jif" && ext == ".jfi" && ext == ".jfif" && ext == ".gif" && ext == ".tiff" && ext == ".tif"
|
|
|
|
}
|
|
|
|
if !canResize(u.RawAvatar) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ap := "./uploads/avatar_"
|
|
|
|
err = Thumbnailer.Resize(u.RawAvatar[1:], ap+strconv.Itoa(u.ID)+u.RawAvatar, ap+strconv.Itoa(u.ID)+"_tmp"+u.RawAvatar, ap+strconv.Itoa(u.ID)+"_w48"+u.RawAvatar, 48)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.ChangeAvatar("." + u.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
Resize(format, inPath, tmpPath, outPath string, width int) error
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RezThumbnailer struct {
|
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (thumb *RezThumbnailer) Resize(format, inPath, tmpPath, outPath string, width int) error {
|
2022-02-21 03:32:53 +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-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (thumb *RezThumbnailer) resize(format, inPath, outPath string, width, height int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
return nil
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
return &CaireThumbnailer{}
|
2018-07-28 12:52:23 +00:00
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func precodeImage(format, inPath, tmpPath string) error {
|
2022-02-21 03:32:53 +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()
|
|
|
|
|
|
|
|
// TODO: Make sure animated gifs work after being encoded
|
|
|
|
switch format {
|
|
|
|
case "gif":
|
|
|
|
return gif.Encode(outFile, img, nil)
|
|
|
|
case "png":
|
|
|
|
return png.Encode(outFile, img)
|
|
|
|
case "tiff", "tif":
|
|
|
|
return tiff.Encode(outFile, img, nil)
|
|
|
|
}
|
|
|
|
return jpeg.Encode(outFile, img, nil)
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (thumb *CaireThumbnailer) Resize(format, inPath, tmpPath, outPath string, width int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
err := precodeImage(format, inPath, tmpPath)
|
|
|
|
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)*/
|
2018-07-28 12:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
type LilliputThumbnailer struct {
|
2018-01-23 10:48:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
*/
|