Add export images functionality

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).
This commit is contained in:
Andreas Mieke 2017-05-21 22:54:42 +02:00
parent be67ae464c
commit 3e9ddbf757

View file

@ -1,8 +1,14 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"github.com/gin-gonic/gin"
@ -43,6 +49,8 @@ func main() {
<-ch
twitter.Stop()
exportAndDeleteImages()
}
func setupGin() {
@ -66,3 +74,45 @@ func setupGin() {
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
}