Andreas Mieke
3e9ddbf757
Adds functionality to export images on program close, it also deletes the originals from the database and the content folder. It also deletes the content folder itself. But before it does anything it asks the user for confirmation and does nothing if anything else than a clear yes gets reported (includes variations of 'No', empty responses and unknown responses).
119 lines
3 KiB
Go
119 lines
3 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/robfig/cron"
|
|
|
|
"git.1750studios.com/AniNite/SocialDragon/config"
|
|
"git.1750studios.com/AniNite/SocialDragon/database"
|
|
"git.1750studios.com/AniNite/SocialDragon/folder"
|
|
"git.1750studios.com/AniNite/SocialDragon/instagram"
|
|
"git.1750studios.com/AniNite/SocialDragon/snapchat"
|
|
"git.1750studios.com/AniNite/SocialDragon/twitter"
|
|
)
|
|
|
|
func main() {
|
|
config.LoadConfig(os.Getenv("HOME") + "/.socialdragon.toml")
|
|
database.InitDb(SendUpdate)
|
|
|
|
c := cron.New()
|
|
if config.C.Snapchat.UserName != "" {
|
|
c.AddFunc("@every 30s", snapchat.LoadNewSnaps)
|
|
}
|
|
if config.C.Instagram.Tag != "" {
|
|
c.AddFunc("@every 30s", instagram.LoadNewInstas)
|
|
}
|
|
if config.C.Folder.Folder != "" {
|
|
c.AddFunc("@every 30s", folder.LoadNewFolders)
|
|
}
|
|
c.Start()
|
|
|
|
if len(config.C.Twitter.Filter) != 0 {
|
|
go twitter.LoadNewTweets()
|
|
}
|
|
go setupGin()
|
|
|
|
ch := make(chan os.Signal)
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
|
|
|
twitter.Stop()
|
|
|
|
exportAndDeleteImages()
|
|
}
|
|
|
|
func setupGin() {
|
|
router := gin.Default()
|
|
router.GET("/", renderHomepage)
|
|
|
|
router.GET("/admin", func(c *gin.Context) { c.Redirect(301, "/admin/inbox") })
|
|
router.GET("/admin/inbox", renderAdminInbox)
|
|
router.GET("/admin/approved", renderAdminApproved)
|
|
router.GET("/admin/rejected", renderAdminRejected)
|
|
|
|
router.GET("/admin/stats", stats)
|
|
router.POST("/admin/approve/:id", approveSnap)
|
|
router.POST("/admin/reject/:id", rejectSnap)
|
|
|
|
router.GET("/ws", func(c *gin.Context) { wsHandler(c.Writer, c.Request) })
|
|
router.GET("/listSnaps", listSnaps)
|
|
|
|
router.LoadHTMLGlob(config.C.TemplatesDirectory + "/*.html")
|
|
router.Static("/static", config.C.AssetsDirectory)
|
|
router.Static(config.C.ContentWebDirectory, config.C.ContentDirectory)
|
|
router.Run(config.C.BindAddress)
|
|
}
|
|
|
|
func exportAndDeleteImages() {
|
|
conf := askForConfirmationNo("Do you want to export and delete the images from the database?")
|
|
if conf {
|
|
var ITs []database.Item
|
|
database.Db.Find(&ITs)
|
|
for _, IT := range ITs {
|
|
old := path.Join(path.Join(config.C.ContentDirectory, ".."), IT.Path)
|
|
new := path.Join(path.Join(config.C.ContentDirectory, ".."), "export")
|
|
state := "unknown"
|
|
if IT.State == database.Approved {
|
|
state = "approved"
|
|
} else if IT.State == database.Rejected {
|
|
state = "rejected"
|
|
} else if IT.State == database.Inbox {
|
|
state = "inbox"
|
|
}
|
|
new = path.Join(new, state)
|
|
os.MkdirAll(new, 0755)
|
|
paths := strings.Split(IT.Path, ".")
|
|
str := strconv.FormatUint(uint64(IT.UserID), 10) + "-" + string(IT.OriginalID) + "." + paths[len(paths)-1]
|
|
new = path.Join(new, str)
|
|
os.Rename(old, new)
|
|
database.Db.Delete(&IT)
|
|
}
|
|
os.RemoveAll(config.C.ContentDirectory)
|
|
}
|
|
}
|
|
|
|
func askForConfirmationNo(s string) bool {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Printf("%s [y/N]: ", s)
|
|
response, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
response = strings.ToLower(strings.TrimSpace(response))
|
|
if response == "y" || response == "yes" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|