package main import ( "fmt" "io/ioutil" "log" "net/http" "os" "sort" "strconv" "strings" ) func syncHandler(w http.ResponseWriter, r *http.Request) { (w).Header().Set("Access-Control-Allow-Origin", "*") if r.URL.Path == "/sync" { w.Header().Add("Content-Type", "application/json") syncGames() _, err := fmt.Fprint(w, "Games are synced") if err != nil { w.WriteHeader(http.StatusInternalServerError) } } else if r.URL.Path == "/sync/reset" { w.Header().Add("Content-Type", "application/json") resetDB() _, err := fmt.Fprint(w, "Games and songs are deleted from the database") if err != nil { w.WriteHeader(http.StatusInternalServerError) } } } func syncGames() { host := os.Getenv("DB_HOST") var dir string if host != "" { dir = "/sorted/" } else { dir = "/Users/sebastian/Resilio Sync/Sorterat_test/" } fmt.Printf("dir: %s\n", dir) foldersToSkip := []string{".sync"} fmt.Println(foldersToSkip) setGameDeletionDate() files, err := ioutil.ReadDir(dir) if err != nil { log.Fatal(err) } for _, file := range files { if file.IsDir() && !contains(foldersToSkip, file.Name()) { fmt.Println(file.Name()) path := dir + file.Name() + "/" fmt.Println(path) innerFiles, err := ioutil.ReadDir(path) if err != nil { log.Fatal(err) } id := -1 for _, song := range innerFiles { id = getIdFromFile(song) if id != -1 { break } } if id == -1 { addNewGame(file.Name(), path) } else { checkIfChanged(id, file.Name(), path) checkSongs(path, id) } } } } func getIdFromFile(file os.FileInfo) int { name := file.Name() if !file.IsDir() && strings.HasSuffix(name, ".id") { name = strings.Replace(name, ".id", "", 1) name = strings.Replace(name, ".", "", 1) i, _ := strconv.Atoi(name) return i } return -1 } func checkIfChanged(id int, name string, path string) { fmt.Printf("Id from file: %v\n", id) nameFromDb := getGameName(id) fmt.Printf("Name from file: %v\n", name) fmt.Printf("Name from DB: %v\n", nameFromDb) if nameFromDb == "" { fmt.Println("Not in db") insertGameWithExistingId(id, name, path) fmt.Println("Added to db") } else if name != nameFromDb { fmt.Println("Diff name") updateGameName(id, name, path) } removeDeletionDate(id) } func addNewGame(name string, path string) { newId := getIdByGameName(name) if newId == -1 { newId = insertGame(name, path) } fmt.Printf("newId = %v", newId) fileName := path + "/." + strconv.Itoa(newId) + ".id" fmt.Printf("fileName = %v", fileName) err := ioutil.WriteFile(fileName, nil, 0644) if err != nil { panic(err) } checkSongs(path, newId) } func checkSongs(gameDir string, gameId int) { songs := make([]SongData, 0) findSongsFromGame := findSongsFromGame(gameId) files, err := ioutil.ReadDir(gameDir) if err != nil { log.Fatal(err) } for _, entry := range files { path := gameDir + entry.Name() songName := entry.Name() if !entry.IsDir() && !strings.HasSuffix(songName, ".id") { songs = append(songs, SongData{gameId: gameId, songName: songName, path: path}) } } if len(songs) != len(findSongsFromGame) { clearSongs(gameId) for _, song := range songs { addSong(song) } } } func resetDB() { clearSongs(-1) clearGames() } func contains(s []string, searchTerm string) bool { i := sort.SearchStrings(s, searchTerm) return i < len(s) && s[i] == searchTerm }