Example #1
0
// visitPlaylist checks that the specified file is
// a music file and is on the correct path.
// If it is ok, it stores it on the database.
func visitPlaylist(name, path, mPoint string) error {
	if path[len(path)-1] != '/' {
		path = path + "/"
	}

	fullPath := path + name
	if strings.HasSuffix(fullPath, ".m3u") {
		glog.Infof("Reading %s\n", fullPath)
		err := playlistmgr.CheckPlaylistFile(fullPath)
		if err != nil {
			glog.Infof("Error in %s playlist\n", name)
			return err
		}

		files, err := playlistmgr.ProcessPlaylist(fullPath)
		if err != nil {
			glog.Infof("Problem reading playlist %s: %s\n", name, err)
			return err
		}

		playlistName := name[:len(name)-len(".m3u")]
		playlistName, _ = store.CreatePlaylist(playlistName, mPoint)

		for _, f := range files {
			store.AddFileToPlaylist(f, playlistName)
		}

		os.Remove(fullPath)
		store.RegeneratePlaylistFile(playlistName, mPoint)
	}
	return nil
}
Example #2
0
// DelayedHandlePlaylistSong handles a dropped file
// inside a playlist and is called by the background
// dispatcher after some time has passed.
func DelayedHandlePlaylistSong(f File) error {
	rootPoint := f.mPoint
	if rootPoint[len(rootPoint)-1] != '/' {
		rootPoint = rootPoint + "/"
	}

	path := rootPoint + "playlists/" + f.album + "/" + f.name

	extension := filepath.Ext(f.name)
	if extension != ".mp3" {
		os.Remove(path)
		return errors.New("File is not an mp3.")
	}

	src, err := os.Stat(path)
	if err != nil || src.IsDir() {
		return errors.New("File not found.")
	}

	err, tags := musicmgr.GetMp3Tags(path)
	if err != nil {
		os.Remove(path)
		return err
	}

	artist := store.GetCompatibleString(tags.Artist)
	album := store.GetCompatibleString(tags.Album)
	title := tags.Title
	if strings.HasSuffix(title, ".mp3") {
		title = title[:len(title)-len(".mp3")]
	}
	title = store.GetCompatibleString(title) + ".mp3"

	newPath, err := store.GetFilePath(artist, album, title)
	if err == nil {
		var playlistFile playlistmgr.PlaylistFile
		playlistFile.Title = title
		playlistFile.Artist = artist
		playlistFile.Album = album
		playlistFile.Path = newPath
		err = store.AddFileToPlaylist(playlistFile, f.album)
	} else {
		err = store.HandleDrop(path, rootPoint)
		if err == nil {
			newPath, err = store.GetFilePath(artist, album, title)
			if err == nil {
				var playlistFile playlistmgr.PlaylistFile
				playlistFile.Title = title
				playlistFile.Artist = artist
				playlistFile.Album = album
				playlistFile.Path = newPath
				err = store.AddFileToPlaylist(playlistFile, f.album)
			}
		}
	}

	os.Remove(path)
	if err == nil {
		err = store.RegeneratePlaylistFile(f.album, rootPoint)
	}
	return err
}