From 3e9ddbf7571c46ee18c4e26f4c37f41a824afca8 Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Sun, 21 May 2017 22:54:42 +0200 Subject: [PATCH] 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). --- socialdragon/main.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/socialdragon/main.go b/socialdragon/main.go index 1e345ba..ece282c 100644 --- a/socialdragon/main.go +++ b/socialdragon/main.go @@ -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 +}