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 +}