Exemplo n.º 1
0
// DelayedHandleDrop handles a dropped file
// but is called by the background dispatcher
// after some time has passed.
func DelayedHandleDrop(f File) error {
	// Get the dropped file path.
	rootPoint := f.mPoint
	if rootPoint[len(rootPoint)-1] != '/' {
		rootPoint = rootPoint + "/"
	}

	path := rootPoint + "drop/" + f.name
	err := store.HandleDrop(path, rootPoint)
	fmt.Printf("DelayedHandleDrop: %s\n", path)
	if err != nil {
		glog.Error(err)
		return err
	}
	return nil
}
Exemplo n.º 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
}