Skip to content

Commit 66f70bd

Browse files
committed
Merge branch 'feature/delete-songs' of https://github.com/DutchJavaDev/MyMusicBox into feature/delete-songs
2 parents 29b246c + f247e84 commit 66f70bd

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

MyMusicBoxApi/http/playlistsong.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package http
22

33
import (
4+
"bufio"
45
"fmt"
56
"musicboxapi/configuration"
67
"musicboxapi/database"
78
"musicboxapi/models"
89
"net/http"
10+
"os"
911
"strconv"
12+
"strings"
1013

1114
"github.com/gin-gonic/gin"
1215
)
@@ -124,6 +127,50 @@ func (handler *PlaylistSongHandler) DeletePlaylistSong(ctx *gin.Context) {
124127

125128
// Delete from database
126129
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+
}
127174
}
128175

129176
ctx.Status(http.StatusOK)

MyMusicBoxApi/service/ytdlp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func StartDownloadTask(downloadRequest models.DownloadRequestModel) {
2626

2727
config := configuration.Config
2828
storageFolderName := config.SourceFolder
29-
archiveFileName := fmt.Sprintf("%s/video_archive", storageFolderName)
29+
archiveFileName := fmt.Sprintf("%s/video_archive", storageFolderName) // move to env / congif
3030
idsFileName := fmt.Sprintf("%s/ids.%d", storageFolderName, parentTask.Id)
3131
namesFileName := fmt.Sprintf("%s/names.%d", storageFolderName, parentTask.Id)
3232
durationFileName := fmt.Sprintf("%s/durations.%d", storageFolderName, parentTask.Id)

0 commit comments

Comments
 (0)