This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.zed/
|
||||||
|
|
||||||
*.exe
|
*.exe
|
||||||
*.db
|
*.db
|
||||||
400
db.go
400
db.go
@@ -1,13 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"butterfliu/internal/repository"
|
|
||||||
"butterfliu/migrations"
|
"butterfliu/migrations"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DB *sql.DB
|
var DB *sql.DB
|
||||||
@@ -25,10 +26,14 @@ func InitDB(dbPath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
DB, err = sql.Open("sqlite3", dbPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
DB, _ = sql.Open("sqlite3", dbPath)
|
if err = DB.Ping(); err != nil {
|
||||||
|
return fmt.Errorf("failed to ping database: %v", err)
|
||||||
DB.Ping()
|
}
|
||||||
|
|
||||||
if err = migrations.InitMigrationTable(DB); err != nil {
|
if err = migrations.InitMigrationTable(DB); err != nil {
|
||||||
return fmt.Errorf("failed to initialize migration table: %v", err)
|
return fmt.Errorf("failed to initialize migration table: %v", err)
|
||||||
@@ -55,390 +60,3 @@ func CloseDB() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddLibrary(name, path string) (repository.Library, error) {
|
|
||||||
absPath, err := filepath.Abs(path)
|
|
||||||
if err != nil {
|
|
||||||
return repository.Library{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := DB.Exec(
|
|
||||||
"INSERT INTO libraries (name, path) VALUES (?, ?)",
|
|
||||||
name, absPath,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return repository.Library{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return repository.Library{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return repository.Library{
|
|
||||||
ID: int(id),
|
|
||||||
Name: name,
|
|
||||||
Path: absPath,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetLibraries() ([]repository.Library, error) {
|
|
||||||
rows, err := DB.Query("SELECT id, name, path FROM libraries")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
libraries := []repository.Library{}
|
|
||||||
for rows.Next() {
|
|
||||||
var lib repository.Library
|
|
||||||
if err := rows.Scan(&lib.ID, &lib.Name, &lib.Path); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
libraries = append(libraries, lib)
|
|
||||||
}
|
|
||||||
|
|
||||||
return libraries, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetLibraryByID(id int) (repository.Library, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, name, path FROM libraries WHERE id = ?", id)
|
|
||||||
var lib repository.Library
|
|
||||||
if err := row.Scan(&lib.ID, &lib.Name, &lib.Path); err != nil {
|
|
||||||
return repository.Library{}, err
|
|
||||||
}
|
|
||||||
return lib, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateLibraryName(id int, name string) error {
|
|
||||||
_, err := DB.Exec(
|
|
||||||
"UPDATE libraries SET name = ? WHERE id = ?",
|
|
||||||
name, id,
|
|
||||||
)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func UpdateLibraryPath(id int, path string) error {
|
|
||||||
_, err := DB.Exec(
|
|
||||||
"UPDATE libraries SET path = ? WHERE id = ?",
|
|
||||||
path, id,
|
|
||||||
)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func DeleteLibrary(id int) error {
|
|
||||||
_, err := DB.Exec("DELETE FROM libraries WHERE id = ?", id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddMediaFile(path string, libraryID int) (repository.MediaFile, error) {
|
|
||||||
result, err := DB.Exec("INSERT INTO media_files (path, library_id) VALUES (?, ?)", path, libraryID)
|
|
||||||
if err != nil {
|
|
||||||
return repository.MediaFile{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return repository.MediaFile{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return repository.MediaFile{
|
|
||||||
ID: int(id),
|
|
||||||
Path: path,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMediaFileByPath(path string) (repository.MediaFile, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, path FROM media_files WHERE path = ?", path)
|
|
||||||
var mediaFile repository.MediaFile
|
|
||||||
if err := row.Scan(&mediaFile.ID, &mediaFile.Path); err != nil {
|
|
||||||
return repository.MediaFile{}, err
|
|
||||||
}
|
|
||||||
return mediaFile, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddArtist(name string) (repository.Artist, error) {
|
|
||||||
result, err := DB.Exec("INSERT INTO artists (name) VALUES (?)", name)
|
|
||||||
if err != nil {
|
|
||||||
return repository.Artist{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return repository.Artist{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return repository.Artist{
|
|
||||||
ID: int(id),
|
|
||||||
Name: name,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetArtists() ([]repository.Artist, error) {
|
|
||||||
rows, err := DB.Query("SELECT id, name FROM artists")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
artists := []repository.Artist{}
|
|
||||||
for rows.Next() {
|
|
||||||
var artist repository.Artist
|
|
||||||
if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
artists = append(artists, artist)
|
|
||||||
}
|
|
||||||
return artists, nil
|
|
||||||
}
|
|
||||||
func GetArtistByID(id int) (repository.Artist, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, name FROM artists WHERE id = ?", id)
|
|
||||||
var artist repository.Artist
|
|
||||||
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
|
|
||||||
return repository.Artist{}, err
|
|
||||||
}
|
|
||||||
return artist, nil
|
|
||||||
}
|
|
||||||
func GetArtistByName(name string) (repository.Artist, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, name FROM artists WHERE name = ?", name)
|
|
||||||
var artist repository.Artist
|
|
||||||
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
|
|
||||||
return repository.Artist{}, err
|
|
||||||
}
|
|
||||||
return artist, nil
|
|
||||||
}
|
|
||||||
func UpdateArtistName(id int, name string) error {
|
|
||||||
_, err := DB.Exec("UPDATE artists SET name = ? WHERE id = ?", name, id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteArtist(id int) error {
|
|
||||||
_, err := DB.Exec("DELETE FROM artists WHERE id = ?", id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddAlbum(title string, artistID int) (repository.Album, error) {
|
|
||||||
result, err := DB.Exec("INSERT INTO albums (title, artist_id) VALUES (?, ?)", title, artistID)
|
|
||||||
if err != nil {
|
|
||||||
return repository.Album{}, err
|
|
||||||
}
|
|
||||||
id, err := result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return repository.Album{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return repository.Album{
|
|
||||||
ID: int(id),
|
|
||||||
Title: title,
|
|
||||||
ArtistID: artistID,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAlbums() ([]repository.Album, error) {
|
|
||||||
rows, err := DB.Query("SELECT id, title, artist_id FROM albums")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
albums := []repository.Album{}
|
|
||||||
for rows.Next() {
|
|
||||||
var album repository.Album
|
|
||||||
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
albums = append(albums, album)
|
|
||||||
}
|
|
||||||
return albums, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAlbumByID(id int) (repository.Album, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, title, artist_id FROM albums WHERE id = ?", id)
|
|
||||||
var album repository.Album
|
|
||||||
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
|
||||||
return repository.Album{}, err
|
|
||||||
}
|
|
||||||
return album, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAlbumByTitle(title string) (repository.Album, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, title, artist_id FROM albums WHERE title = ?", title)
|
|
||||||
var album repository.Album
|
|
||||||
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
|
||||||
return repository.Album{}, err
|
|
||||||
}
|
|
||||||
return album, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAlbumByTitleAndArtist(title string, artistID int) (repository.Album, error) {
|
|
||||||
row := DB.QueryRow("SELECT id, title, artist_id FROM albums WHERE title = ? AND artist_id = ?", title, artistID)
|
|
||||||
var album repository.Album
|
|
||||||
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
|
||||||
return repository.Album{}, err
|
|
||||||
}
|
|
||||||
return album, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAlbumsByArtist(artistID int) ([]repository.Album, error) {
|
|
||||||
rows, err := DB.Query("SELECT id, title, artist_id FROM albums WHERE artist_id = ?", artistID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
albums := []repository.Album{}
|
|
||||||
for rows.Next() {
|
|
||||||
var album repository.Album
|
|
||||||
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
albums = append(albums, album)
|
|
||||||
}
|
|
||||||
return albums, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateAlbumTitle(id int, title string) error {
|
|
||||||
_, err := DB.Exec("UPDATE albums SET title = ? WHERE id = ?", title, id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func UpdateAlbumArtistID(id, artistID int) error {
|
|
||||||
_, err := DB.Exec("UPDATE albums SET artist_id = ?", artistID)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func DeleteAlbum(id int) error {
|
|
||||||
_, err := DB.Exec("DELETE FROM albums WHERE id = ?", id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddSong(title string, artistID, albumID, duration, mediaFileID int) (repository.Song, error) {
|
|
||||||
result, err := DB.Exec("INSERT INTO songs (title, artist_id, album_id, duration, media_file_id) VALUES (?, ?, ?, ?, ?)", title, artistID, albumID, duration, mediaFileID)
|
|
||||||
if err != nil {
|
|
||||||
return repository.Song{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return repository.Song{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return repository.Song{
|
|
||||||
ID: int(id),
|
|
||||||
Title: title,
|
|
||||||
ArtistID: artistID,
|
|
||||||
AlbumID: albumID,
|
|
||||||
Duration: duration,
|
|
||||||
MediaFileID: mediaFileID,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
func GetSongs() ([]repository.Song, error) {
|
|
||||||
rows, err := DB.Query("SELECT id, artist_id, album_id, duration, media_file_id FROM songs")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
songs := []repository.Song{}
|
|
||||||
for rows.Next() {
|
|
||||||
var song repository.Song
|
|
||||||
if err := rows.Scan(&song.ID, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
songs = append(songs, song)
|
|
||||||
}
|
|
||||||
return songs, nil
|
|
||||||
}
|
|
||||||
func UpdateSongTitle(id int, title string) error {
|
|
||||||
_, err := DB.Exec("UPDATE songs SET title = ? WHERE id = ?", title, id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func UpdateSongArtistID(id int, artistID int) error {
|
|
||||||
_, err := DB.Exec("UPDATE songs SET artist_id = ?", artistID)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func UpdateSongAlbumID(id, albumID int) error {
|
|
||||||
_, err := DB.Exec("UPDATE songs SET album_id = ?", albumID)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func UpdateSongDuration(id, duration int) error {
|
|
||||||
_, err := DB.Exec("UPDATE songs SET duration = ?", duration)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
func DeleteSong(id int) error {
|
|
||||||
_, err := DB.Exec("DELETE FROM songs WHERE id = ?", id)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetSongsByLibrary(libraryID int) ([]repository.Song, error) {
|
|
||||||
rows, err := DB.Query(`
|
|
||||||
SELECT s.id, s.title, s.artist_id, s.album_id, s.duration, s.media_file_id
|
|
||||||
FROM songs s
|
|
||||||
INNER JOIN media_files mf ON s.media_file_id = mf.id
|
|
||||||
WHERE mf.library_id = ?
|
|
||||||
ORDER BY s.title
|
|
||||||
`, libraryID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
songs := []repository.Song{}
|
|
||||||
for rows.Next() {
|
|
||||||
var song repository.Song
|
|
||||||
if err := rows.Scan(&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
songs = append(songs, song)
|
|
||||||
}
|
|
||||||
return songs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type SongDetail struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Artist string `json:"artist"`
|
|
||||||
Album string `json:"album"`
|
|
||||||
Duration int `json:"duration"`
|
|
||||||
Path string `json:"path"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetSongsByLibraryWithDetails(libraryID int) ([]SongDetail, error) {
|
|
||||||
rows, err := DB.Query(`
|
|
||||||
SELECT s.id, s.title, a.name as artist_name, al.title as album_title, s.duration, mf.path
|
|
||||||
FROM songs s
|
|
||||||
INNER JOIN media_files mf ON s.media_file_id = mf.id
|
|
||||||
INNER JOIN artists a ON s.artist_id = a.id
|
|
||||||
INNER JOIN albums al ON s.album_id = al.id
|
|
||||||
WHERE mf.library_id = ?
|
|
||||||
ORDER BY s.title
|
|
||||||
`, libraryID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
songs := []SongDetail{}
|
|
||||||
for rows.Next() {
|
|
||||||
var song SongDetail
|
|
||||||
if err := rows.Scan(&song.ID, &song.Title, &song.Artist, &song.Album, &song.Duration, &song.Path); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
songs = append(songs, song)
|
|
||||||
}
|
|
||||||
return songs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAllSongsWithDetails() ([]SongDetail, error) {
|
|
||||||
rows, err := DB.Query(`
|
|
||||||
SELECT s.id, s.title, a.name as artist_name, al.title as album_title, s.duration, mf.path
|
|
||||||
FROM songs s
|
|
||||||
INNER JOIN media_files mf ON s.media_file_id = mf.id
|
|
||||||
INNER JOIN artists a ON s.artist_id = a.id
|
|
||||||
INNER JOIN albums al ON s.album_id = al.id
|
|
||||||
ORDER BY s.title
|
|
||||||
`)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
songs := []SongDetail{}
|
|
||||||
for rows.Next() {
|
|
||||||
var song SongDetail
|
|
||||||
if err := rows.Scan(&song.ID, &song.Title, &song.Artist, &song.Album, &song.Duration, &song.Path); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
songs = append(songs, song)
|
|
||||||
}
|
|
||||||
return songs, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"butterfliu/internal/service"
|
"butterfliu/internal/service"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -23,69 +22,84 @@ func NewLibraryController(service *service.LibraryService) *LibraryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetAll(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetAll(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
libraries, err := c.service.GetAll()
|
libraries, err := c.service.GetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(libraries)
|
jsonResponse(w, libraries, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) Create(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) Create(w http.ResponseWriter, r *http.Request) {
|
||||||
var library map[string]interface{}
|
var req struct {
|
||||||
err := json.NewDecoder(r.Body).Decode(&library)
|
Name string `json:"name"`
|
||||||
if err != nil {
|
Path string `json:"path"`
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = c.service.Create(library["name"].(string), library["path"].(string))
|
if req.Name == "" || req.Path == "" {
|
||||||
if err != nil {
|
jsonError(w, "name and path are required", http.StatusBadRequest)
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Add library successfully."})
|
_, err := c.service.Create(req.Name, req.Path)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonMsg(w, "Add library successfully.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) UpdateName(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) UpdateName(w http.ResponseWriter, r *http.Request) {
|
||||||
idParam := chi.URLParam(r, "id")
|
idParam := chi.URLParam(r, "id")
|
||||||
name, _ := io.ReadAll(r.Body)
|
|
||||||
defer r.Body.Close()
|
|
||||||
id, err := strconv.Atoi(idParam)
|
id, err := strconv.Atoi(idParam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = c.service.UpdateName(id, string(name))
|
|
||||||
|
var name string
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&name); err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.service.UpdateName(id, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Updated library name."})
|
jsonMsg(w, "Updated library name.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) UpdatePath(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) UpdatePath(w http.ResponseWriter, r *http.Request) {
|
||||||
idParam := chi.URLParam(r, "id")
|
idParam := chi.URLParam(r, "id")
|
||||||
path, _ := io.ReadAll(r.Body)
|
|
||||||
defer r.Body.Close()
|
|
||||||
id, err := strconv.Atoi(idParam)
|
id, err := strconv.Atoi(idParam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = c.service.UpdatePath(id, string(path))
|
|
||||||
|
var path string
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&path); err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.service.UpdatePath(id, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Updated library path."})
|
jsonMsg(w, "Updated library path.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
||||||
c.scanMutex.Lock()
|
c.scanMutex.Lock()
|
||||||
if c.isScanning {
|
if c.isScanning {
|
||||||
c.scanMutex.Unlock()
|
c.scanMutex.Unlock()
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "Another scan is already in progress"})
|
jsonError(w, "Another scan is already in progress", http.StatusConflict)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.isScanning = true
|
c.isScanning = true
|
||||||
@@ -97,7 +111,7 @@ func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
|||||||
c.scanMutex.Lock()
|
c.scanMutex.Lock()
|
||||||
c.isScanning = false
|
c.isScanning = false
|
||||||
c.scanMutex.Unlock()
|
c.scanMutex.Unlock()
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,56 +131,60 @@ func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Printf("Scan completed successfully for library %d", id)
|
log.Printf("Scan completed successfully for library %d", id)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Library scan started."})
|
jsonMsg(w, "Library scan started.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetScanStatus(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetScanStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
c.scanMutex.Lock()
|
c.scanMutex.Lock()
|
||||||
scanning := c.isScanning
|
scanning := c.isScanning
|
||||||
c.scanMutex.Unlock()
|
c.scanMutex.Unlock()
|
||||||
json.NewEncoder(w).Encode(map[string]bool{"scanning": scanning})
|
jsonResponse(w, map[string]bool{"scanning": scanning}, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) Delete(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) Delete(w http.ResponseWriter, r *http.Request) {
|
||||||
idParam := chi.URLParam(r, "id")
|
idParam := chi.URLParam(r, "id")
|
||||||
id, _ := strconv.Atoi(idParam)
|
id, err := strconv.Atoi(idParam)
|
||||||
err := c.service.Delete(id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Deleted library."})
|
err = c.service.Delete(id)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonMsg(w, "Deleted library.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetSongs(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetSongs(w http.ResponseWriter, r *http.Request) {
|
||||||
idParam := chi.URLParam(r, "id")
|
idParam := chi.URLParam(r, "id")
|
||||||
id, err := strconv.Atoi(idParam)
|
id, err := strconv.Atoi(idParam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
songs, err := c.service.GetSongs(id)
|
songs, err := c.service.GetSongs(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(songs)
|
jsonResponse(w, songs, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetArtists(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetArtists(w http.ResponseWriter, r *http.Request) {
|
||||||
artists, err := c.service.GetArtists()
|
artists, err := c.service.GetArtists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(artists)
|
jsonResponse(w, artists, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
||||||
albums, err := c.service.GetAlbums()
|
albums, err := c.service.GetAlbums()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(albums)
|
jsonResponse(w, albums, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|||||||
20
internal/controller/response.go
Normal file
20
internal/controller/response.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func jsonResponse(w http.ResponseWriter, data interface{}, status int) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
json.NewEncoder(w).Encode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonError(w http.ResponseWriter, message string, status int) {
|
||||||
|
jsonResponse(w, map[string]string{"error": message}, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonMsg(w http.ResponseWriter, message string) {
|
||||||
|
jsonResponse(w, map[string]string{"msg": message}, http.StatusOK)
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"butterfliu/internal/service"
|
"butterfliu/internal/service"
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -20,15 +19,15 @@ func NewSongController(service *service.SongService) *SongController {
|
|||||||
func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) {
|
func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
songs, err := c.service.GetAllWithDetails()
|
songs, err := c.service.GetAllWithDetails()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(songs)
|
jsonResponse(w, songs, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SongController) Stream(w http.ResponseWriter, r *http.Request) {
|
func (c *SongController) Stream(w http.ResponseWriter, r *http.Request) {
|
||||||
parmaID := chi.URLParam(r, "id")
|
paramID := chi.URLParam(r, "id")
|
||||||
id, err := strconv.Atoi(parmaID)
|
id, err := strconv.Atoi(paramID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func NewMediaRepository(db *sql.DB) *MediaRepository {
|
|||||||
|
|
||||||
func (r *MediaRepository) Get(id int) (MediaFile, error) {
|
func (r *MediaRepository) Get(id int) (MediaFile, error) {
|
||||||
var m MediaFile
|
var m MediaFile
|
||||||
rows, err := r.db.Query("SELECT id, path, library_id FROM media_files WHERE id = $1", id)
|
rows, err := r.db.Query("SELECT id, path, library_id FROM media_files WHERE id = ?", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MediaFile{}, err
|
return MediaFile{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
2
queue.go
2
queue.go
@@ -1,3 +1,5 @@
|
|||||||
|
// Package main - Task queue for background processing.
|
||||||
|
// Currently unused but kept for future async task support (e.g., background scanning).
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
95
scanner.go
95
scanner.go
@@ -1,95 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/dhowden/tag"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ScannedSong struct {
|
|
||||||
Title string
|
|
||||||
Artist string
|
|
||||||
Album string
|
|
||||||
Duration int
|
|
||||||
Path string
|
|
||||||
}
|
|
||||||
|
|
||||||
func scanDirectory(dirPath string) ([]ScannedSong, error) {
|
|
||||||
songs := []ScannedSong{}
|
|
||||||
|
|
||||||
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
|
||||||
return songs, err
|
|
||||||
}
|
|
||||||
filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
|
||||||
|
|
||||||
if info.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isAudioFile(path) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if song, err := processAudioFile(path); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
songs = append(songs, song)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return songs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func processAudioFile(filePath string) (ScannedSong, error) {
|
|
||||||
file, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return ScannedSong{}, fmt.Errorf("failed to open file: %v", err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// Read metadata
|
|
||||||
metadata, err := tag.ReadFrom(file)
|
|
||||||
if err != nil {
|
|
||||||
return ScannedSong{}, fmt.Errorf("failed to read metadata: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract metadata
|
|
||||||
title := metadata.Title()
|
|
||||||
if title == "" {
|
|
||||||
title = filepath.Base(filePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
artist := metadata.Artist()
|
|
||||||
if artist == "" {
|
|
||||||
artist = "Unknown Artist"
|
|
||||||
}
|
|
||||||
|
|
||||||
album := metadata.Album()
|
|
||||||
if album == "" {
|
|
||||||
album = "Unknown Album"
|
|
||||||
}
|
|
||||||
|
|
||||||
return ScannedSong{
|
|
||||||
Title: title,
|
|
||||||
Artist: artist,
|
|
||||||
Album: album,
|
|
||||||
Duration: 100,
|
|
||||||
Path: filePath,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAudioFile(path string) bool {
|
|
||||||
ext := strings.ToLower(filepath.Ext(path))
|
|
||||||
audioExtensions := []string{".mp3", ".flac", ".m4a", ".wav", ".ogg"}
|
|
||||||
|
|
||||||
for _, audioExt := range audioExtensions {
|
|
||||||
if ext == audioExt {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIsAudioFile(t *testing.T) {
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
path string
|
|
||||||
expected bool
|
|
||||||
}{
|
|
||||||
{"mp3 file", "song.mp3", true},
|
|
||||||
{"flac file", "song.flac", true},
|
|
||||||
{"m4a file", "song.m4a", true},
|
|
||||||
{"wav file", "song.wav", true},
|
|
||||||
{"ogg file", "song.ogg", true},
|
|
||||||
{"aac file", "song.aac", false},
|
|
||||||
{"wma file", "song.wma", false},
|
|
||||||
{"uppercase extension", "song.MP3", true},
|
|
||||||
{"mixed case", "song.Mp3", true},
|
|
||||||
{"txt file", "song.txt", false},
|
|
||||||
{"jpg file", "song.jpg", false},
|
|
||||||
{"no extension", "song", false},
|
|
||||||
{"dot in name", "my.song.mp3", true},
|
|
||||||
{"multiple dots", "my.song.final.mp3", true},
|
|
||||||
{"path with directory", "/path/to/song.mp3", true},
|
|
||||||
{"Windows path", "C:\\Music\\song.mp3", true},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
result := isAudioFile(tt.path)
|
|
||||||
if result != tt.expected {
|
|
||||||
t.Errorf("isAudioFile(%q) = %v, want %v", tt.path, result, tt.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user