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
func (d *Dir) Rename(ctx context.Context, r *fuse.RenameRequest, newDir fs.Node) error {
	var newD *Dir

	newD = newDir.(*Dir)
	glog.Infof("Renaming: OldName: %s, NewName: %s, newDir: %s/%s\n", r.OldName, r.NewName, newD.artist, newD.album)

	if d.mPoint[len(d.mPoint)-1] != '/' {
		d.mPoint = d.mPoint + "/"
	}
	path := d.mPoint + d.artist + "/" + d.album + "/" + r.OldName

	if r.OldName == ".description" || r.NewName == ".description" {
		return fuse.EPERM
	}

	if r.NewName[0] == '.' || r.OldName[0] == '.' {
		glog.Info("Names starting with dot are not allowed.")
		return fuse.EPERM
	}

	if len(d.artist) < 1 {
		glog.Info("Changing artist name.")
		if len(newD.artist) > 0 {
			return fuse.EPERM
		}

		err := store.MoveArtist(r.OldName, r.NewName, d.mPoint)
		return err
	}

	if d.artist == "drop" {
		glog.Info("Cannot rename inside drop folder.")
		return fuse.EPERM
	}

	if d.artist == "playlists" {
		glog.Info("Rename inside playlists folder.")
		var err error
		if len(d.album) < 1 {
			glog.Info("Rename playlist name.")
			var newName string
			if len(newD.album) < 1 {
				newName, err = store.RenamePlaylist(r.OldName, r.NewName, d.mPoint)
			} else {
				newName, err = store.RenamePlaylist(r.OldName, newD.album, d.mPoint)
			}
			if err != nil {
				return fuse.EIO
			}
			err = store.RegeneratePlaylistFile(newName, d.mPoint)
			if err != nil {
				return fuse.EIO
			}
			return nil
		}

		_, err = store.RenamePlaylistSong(d.album, r.OldName, r.NewName, d.mPoint)
		if err != nil {
			return fuse.EIO
		}

		err = store.RegeneratePlaylistFile(d.album, d.mPoint)
		if err != nil {
			return fuse.EIO
		}

		return nil
	}

	if len(d.album) < 1 {
		glog.Info("Moving album")
		if len(newD.album) > 0 {
			return fuse.EPERM
		}

		err := store.MoveAlbum(d.artist, r.OldName, newD.artist, r.NewName, d.mPoint)
		return err
	}

	_, err := store.MoveSongs(d.artist, d.album, r.OldName, newD.artist, newD.album, r.NewName, path, d.mPoint)
	if err != nil {
		return fuse.EIO
	}
	return nil
}
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
}
Example #4
0
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
	name := req.Name
	glog.Infof("Entering mkdir with name: %s.\n", name)
	// Do not allow creating directories starting with dot
	if name[0] == '.' {
		glog.Info("Names starting with dot are not allowed.")
		return nil, fuse.EPERM
	}

	if d.mPoint[len(d.mPoint)-1] != '/' {
		d.mPoint = d.mPoint + "/"
	}
	if len(d.artist) < 1 {
		glog.Info("Creating an Artist.")
		ret, err := store.CreateArtist(name)
		if err != nil {
			glog.Infof("Error creating artist: %s\n", err)
			return nil, err
		}

		path := d.mPoint + ret
		err = os.MkdirAll(path, 0777)
		if err != nil {
			glog.Infof("Error creating artist folder: %s\n", err)
			return nil, fuse.EIO
		}
		return &Dir{fs: d.fs, artist: ret, album: "", mPoint: d.mPoint}, nil
	}

	if d.artist == "drop" {
		return nil, fuse.EIO
	}

	if d.artist == "playlists" {
		if len(d.album) < 1 {
			ret, err := store.CreatePlaylist(name, d.mPoint)
			if err != nil {
				glog.Infof("Error creating playlist: %s\n", err)
				return nil, err
			}

			err = store.RegeneratePlaylistFile(ret, d.mPoint)
			if err != nil {
				glog.Infof("Error regenerating playlist: %s\n", err)
				return nil, err
			}
			return &Dir{fs: d.fs, artist: "playlists", album: ret, mPoint: d.mPoint}, nil
		}
		return nil, fuse.EPERM
	}

	if len(d.album) < 1 {
		glog.Infof("Creating album: %s in artist: %s.\n", d.artist, name)
		ret, err := store.CreateAlbum(d.artist, name)
		if err != nil {
			return nil, err
		}
		path := d.mPoint + d.artist + "/" + ret
		err = os.MkdirAll(path, 0777)
		if err != nil {
			glog.Infof("Error creating artist folder: %s\n", err)
			return nil, fuse.EIO
		}
		return &Dir{fs: d.fs, artist: d.artist, album: ret, mPoint: d.mPoint}, nil
	}

	return nil, fuse.EIO
}