示例#1
0
// RenamePlaylist moves the entire Playlist and changes all
// the links to the songs in every MuLi song.
func RenamePlaylist(oldName, newName, mPoint string) (string, error) {
	glog.Infof("Renaming %s playlist to %s.\n", oldName, newName)
	newName = GetCompatibleString(newName)
	db, err := bolt.Open(config.DbPath, 0600, nil)
	if err != nil {
		return "", err
	}
	defer db.Close()

	err = db.Update(func(tx *bolt.Tx) error {
		root := tx.Bucket([]byte("Playlists"))
		if root == nil {
			glog.Errorf("Error opening Playlists bucket.\n")
			return errors.New("Error opening Playlists bucket.")
		}

		playlistBucket := root.Bucket([]byte(oldName))
		if playlistBucket == nil {
			return nil
		}

		newPlaylistBucket, err := root.CreateBucketIfNotExists([]byte(newName))
		if err != nil {
			glog.Infof("Cannot create new playlist bucket: %s\n", err)
			return err
		}

		c := playlistBucket.Cursor()
		for k, songJson := c.First(); k != nil; k, songJson = c.Next() {
			if songJson == nil {
				glog.Infof("Error opening song Json: %s in playlist: %s\n", k, oldName)
				continue
			}

			// Get the PlaylistFile
			var file playlistmgr.PlaylistFile
			err = json.Unmarshal(songJson, &file)
			if err != nil {
				continue
			}

			// Open the song in the MuLi database
			// to remove the playlists connection.
			artistsBucket := tx.Bucket([]byte("Artists"))
			if artistsBucket == nil {
				glog.Error("Cannot open Artists bucket.")
				return errors.New("Cannot open Artists bucket.")
			}

			artistBucket := artistsBucket.Bucket([]byte(file.Artist))
			if artistBucket == nil {
				glog.Infof("Cannot open Artist bucket: %s.\n", file.Artist)
				continue
			}

			albumBucket := artistBucket.Bucket([]byte(file.Album))
			if albumBucket == nil {
				glog.Infof("Cannot open Album bucket: %s in Artist: %s.\n", file.Album, file.Artist)
				continue
			}

			jsonFile := albumBucket.Get([]byte(file.Title))
			if jsonFile == nil {
				glog.Infof("Cannot open song: %s Album bucket: %s in Artist: %s.\n", file.Title, file.Album, file.Artist)
				continue
			}

			var song SongStore
			err = json.Unmarshal(jsonFile, &song)
			if err != nil {
				return err
			}

			// Remove the playlist from the song's playlists list
			if song.Playlists != nil {
				for i, list := range song.Playlists {
					if list == oldName {
						song.Playlists[i] = newName
						break
					}
				}
			}

			encoded, err := json.Marshal(song)
			if err != nil {
				return err
			}

			// Store the modified song version
			err = albumBucket.Put([]byte(k), encoded)
			if err != nil {
				continue
			}

			err = newPlaylistBucket.Put(k, songJson)
			if err != nil {
				continue
			}
		}

		playlistmgr.DeletePlaylist(oldName, mPoint)
		return root.DeleteBucket([]byte(oldName))
	})

	if err != nil {
		return "", err
	}

	return newName, nil
}
示例#2
0
// DeletePlaylist function deletes a playlist from the database
// and also deletes all the entries in the specific files and
// deletes it from the filesystem.
func DeletePlaylist(name, mPoint string) error {
	db, err := bolt.Open(config.DbPath, 0600, nil)
	if err != nil {
		return err
	}
	defer db.Close()

	err = db.Update(func(tx *bolt.Tx) error {
		root := tx.Bucket([]byte("Playlists"))
		if root == nil {
			glog.Errorf("Error opening Playlists bucket.\n")
			return errors.New("Error opening Playlists bucket.")
		}

		playlistBucket := root.Bucket([]byte(name))
		if playlistBucket == nil {
			return nil
		}

		c := playlistBucket.Cursor()
		for k, songJson := c.First(); k != nil; k, songJson = c.Next() {
			if songJson == nil {
				glog.Infof("Error opening song Json: %s in playlist: %s\n", k, name)
				continue
			}

			// Get the PlaylistFile
			var file playlistmgr.PlaylistFile
			err = json.Unmarshal(songJson, &file)
			if err != nil {
				continue
			}

			// Open the song in the MuLi database
			// to remove the playlists connection.
			artistsBucket := tx.Bucket([]byte("Artists"))
			if artistsBucket == nil {
				glog.Error("Cannot open Artists bucket.")
				return errors.New("Cannot open Artists bucket.")
			}

			artistBucket := artistsBucket.Bucket([]byte(file.Artist))
			if artistBucket == nil {
				glog.Infof("Cannot open Artist bucket: %s.\n", file.Artist)
				continue
			}

			albumBucket := artistBucket.Bucket([]byte(file.Album))
			if albumBucket == nil {
				glog.Infof("Cannot open Album bucket: %s in Artist: %s.\n", file.Album, file.Artist)
				continue
			}

			jsonFile := albumBucket.Get([]byte(file.Title))
			if jsonFile == nil {
				glog.Infof("Cannot open song: %s Album bucket: %s in Artist: %s.\n", file.Title, file.Album, file.Artist)
				continue
			}

			var song SongStore
			err = json.Unmarshal(jsonFile, &song)
			if err != nil {
				return err
			}

			// Remove the playlist from the song's playlists list
			if song.Playlists != nil {
				for i, list := range song.Playlists {
					if list == name {
						song.Playlists = append(song.Playlists[:i], song.Playlists[i+1:]...)
						break
					}
				}
			}

			encoded, err := json.Marshal(song)
			if err != nil {
				return err
			}

			// Store the modified song version
			return albumBucket.Put([]byte(k), encoded)

		}

		return root.DeleteBucket([]byte(name))
	})

	return playlistmgr.DeletePlaylist(name, mPoint)
}