|
1 | 1 | package http |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
5 | 6 | "musicboxapi/configuration" |
6 | 7 | "musicboxapi/database" |
7 | 8 | "musicboxapi/models" |
8 | 9 | "net/http" |
| 10 | + "os" |
9 | 11 | "strconv" |
| 12 | + "strings" |
10 | 13 |
|
11 | 14 | "github.com/gin-gonic/gin" |
12 | 15 | ) |
@@ -124,6 +127,50 @@ func (handler *PlaylistSongHandler) DeletePlaylistSong(ctx *gin.Context) { |
124 | 127 |
|
125 | 128 | // Delete from database |
126 | 129 | songTable.DeleteSongById(song.Id) |
| 130 | + |
| 131 | + // Delete from video_archive |
| 132 | + strSplit := strings.Split(song.Path, ".") |
| 133 | + |
| 134 | + filenameWithOutExtension := strSplit[0] |
| 135 | + strSplit = strings.Split(filenameWithOutExtension, "/") |
| 136 | + |
| 137 | + filenameWithOutExtension = strSplit[1] |
| 138 | + |
| 139 | + filePath := fmt.Sprintf("%s/%s", configuration.Config.SourceFolder, "video_archive") |
| 140 | + |
| 141 | + // Read the file |
| 142 | + file, err := os.Open(filePath) |
| 143 | + if err != nil { |
| 144 | + fmt.Println("Error opening file:", err) |
| 145 | + ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err)) |
| 146 | + return |
| 147 | + } |
| 148 | + defer file.Close() |
| 149 | + |
| 150 | + var lines []string |
| 151 | + scanner := bufio.NewScanner(file) |
| 152 | + for scanner.Scan() { |
| 153 | + line := scanner.Text() |
| 154 | + // Keep the line only if it’s not the target |
| 155 | + if strings.TrimSpace(line) != fmt.Sprintf("youtube %s", filenameWithOutExtension) { |
| 156 | + lines = append(lines, line) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if err := scanner.Err(); err != nil { |
| 161 | + fmt.Println("Error reading file:", err) |
| 162 | + ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err)) |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + // Write the updated content back to the file |
| 167 | + output := strings.Join(lines, "\n") |
| 168 | + err = os.WriteFile(filePath, []byte(output+"\n"), 0644) |
| 169 | + if err != nil { |
| 170 | + fmt.Println("Error writing file:", err) |
| 171 | + ctx.JSON(http.StatusInternalServerError, models.ErrorResponse(err)) |
| 172 | + return |
| 173 | + } |
127 | 174 | } |
128 | 175 |
|
129 | 176 | ctx.Status(http.StatusOK) |
|
0 commit comments