Example #1
0
// visit 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 visit(path string, f os.FileInfo, err error) error {
	if strings.HasSuffix(path, ".mp3") {
		glog.Infof("Reading %s\n", path)
		err, f := musicmgr.GetMp3Tags(path)
		if err != nil {
			glog.Errorf("Error in %s\n", path)
		}
		if f.Artist == "drop" {
			glog.Errorf("Error in %s\n", path)
		}
		if f.Artist == "playlists" {
			glog.Errorf("Error in %s\n", path)
		}
		store.StoreNewSong(&f, path)
	}
	return nil
}
Example #2
0
/** This function manages the Drop directory.
 *  The user can copy/create files into this directory and
 *  the files will be organized to the correct directory
 *  based on the file tags.
 */
func HandleDrop(path, rootPoint string) error {
	glog.Infof("Handle drop with path: %s\n", path)
	err, fileTags := musicmgr.GetMp3Tags(path)
	if err != nil {
		deleteDrop(path)
		return fuse.EIO
	}

	extension := filepath.Ext(path)

	artist, err := CreateArtist(fileTags.Artist)
	if err != nil && err != fuse.EEXIST {
		glog.Infof("Error creating Artist: %s\n", err)
		return err
	}

	album, err := CreateAlbum(artist, fileTags.Album)
	if err != nil && err != fuse.EEXIST {
		glog.Infof("Error creating Album: %s\n", err)
		return err
	}

	//_, file := filepath.Split(path)
	newPath := rootPoint + artist + "/" + album + "/"
	os.MkdirAll(newPath, 0777)

	file := GetCompatibleString(fileTags.Title) + extension
	err = os.Rename(path, newPath+file)
	if err != nil {
		glog.Infof("Error renaming song: %s\n", err)
		return fuse.EIO
	}

	_, err = CreateSong(artist, album, fileTags.Title+extension, newPath)
	deleteDrop(path)
	if err != nil {
		glog.Infof("Error creating song in the DB: %s\n", err)
	}
	return err
}
Example #3
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
}