Commit b581c96066
Unsigned
Layout: unified · split
internal/localTracking.go +22 −2
| @@ -4,6 +4,7 @@ import ( | ||
| 4 | 4 | "encoding/csv" |
| 5 | 5 | "fmt" |
| 6 | 6 | "os" |
| 7 | "path/filepath" | |
| 7 | 8 | "strconv" |
| 8 | 9 | ) |
| 9 | 10 | |
| @@ -78,13 +79,32 @@ func LocalDeleteAnime(databaseFile string, anilistID int, allanimeID string) { | ||
| 78 | 79 | // Function to get all anime entries from the database |
| 79 | 80 | func LocalGetAllAnime(databaseFile string) []Anime { |
| 80 | 81 | animeList := []Anime{} |
| 81 | file, err := os.Open(databaseFile) | |
| 82 | ||
| 83 | // Ensure the directory exists | |
| 84 | dir := filepath.Dir(databaseFile) | |
| 85 | if err := os.MkdirAll(dir, 0755); err != nil { | |
| 86 | fmt.Println("Error creating directory:", err) | |
| 87 | return animeList | |
| 88 | } | |
| 89 | ||
| 90 | // Open the file, create if it doesn't exist | |
| 91 | file, err := os.OpenFile(databaseFile, os.O_RDONLY|os.O_CREATE, 0644) | |
| 82 | 92 | if err != nil { |
| 83 | fmt.Println("Error opening file:", err) | |
| 93 | fmt.Println("Error opening or creating file:", err) | |
| 84 | 94 | return animeList |
| 85 | 95 | } |
| 86 | 96 | defer file.Close() |
| 87 | 97 | |
| 98 | // If the file was just created, it will be empty, so return an empty list | |
| 99 | fileInfo, err := file.Stat() | |
| 100 | if err != nil { | |
| 101 | fmt.Println("Error getting file info:", err) | |
| 102 | return animeList | |
| 103 | } | |
| 104 | if fileInfo.Size() == 0 { | |
| 105 | return animeList | |
| 106 | } | |
| 107 | ||
| 88 | 108 | reader := csv.NewReader(file) |
| 89 | 109 | records, err := reader.ReadAll() |
| 90 | 110 | if err != nil { |