Пример #1
0
func main() {
	doHelp := false
	flag.BoolVar(&doHelp, "help", false, "Display usage info.")
	flag.Parse()

	if doHelp || flag.NArg() != 1 {
		fmt.Println("Usage: info <file>")
		return
	}

	str := flag.Arg(1)
	v, err := vlc.New()
	if err != nil {
		fmt.Println("vlc.New():", err)
		return
	}
	m, err := v.MediaNewPath(str)
	if err != nil {
		fmt.Println("vlc.MediaNewPath:", err)
		return
	}
	m.Parse()
	err = vlc.LastError()
	if err != nil {
		fmt.Println("media.Parse():", err)
		return
	}

	player, err := v.NewPlayer()
	if err != nil {
		fmt.Println("vlc.NewPlayer():", err)
		return
	}

	player.SetMedia(m)

	err = player.Play()
	if err != nil {
		fmt.Println("player.Play():", err)
		return
	}

	for k, t := range vlc.MetaTags {
		v := m.GetMeta(t)
		err = vlc.LastError()
		if err != nil {
			fmt.Println("media.GetMeta():", err)
			return
		}
		fmt.Println(k, ":", v)
	}

	time.Sleep(time.Second * 20)
	fmt.Println("end.")
}
Пример #2
0
func (d *DB) Update() {
	// keep file base up to date
	searchPath := config.Current.MediaPath
	if strings.Contains(searchPath, "~") {
		user, err := osuser.Current()
		if err != nil {
			log.Log.Println("Error getting user home directory:", err)
			return
		}
		searchPath = strings.Replace(searchPath, "~", user.HomeDir, -1)
	}
	if _, err := os.Stat(searchPath); os.IsNotExist(err) {
		log.Log.Println("Error: Music path", searchPath, "does not exist!")
		return
	}
	d.db.LogMode(false)
	tx := d.db.Begin()
	up := &updater{
		job:          d.c.RegisterJob(),
		tx:           make(chan *gorm.DB, 1),
		allFiles:     make(chan entry, bufferSize),
		newFiles:     make(chan entry, bufferSize),
		importFiles:  make(chan entry, bufferSize),
		success:      make(chan bool),
		stopStepping: make(chan bool, 1),
	}
	up.tx <- tx

	var err error
	up.vlc, err = vlc.New()
	if err != nil {
		log.Log.Println("Could not init libVLC", err)
		return
	}

	go func() {
		err := filepath.Walk(searchPath, up.step)
		if err != nil && err != ErrorTerminate {
			log.Log.Println("Updater error:", err)
		}
		close(up.allFiles)
	}()

	// filter out ignored
	go func(input, output chan entry) {
		for entry := range input {
			up.numAllFiles++
			//TODO do something with the cover jpgs
			do := false
			for _, v := range Types {
				if strings.HasSuffix(entry.Filename, v) {
					do = true
					break
				}
			}
			if do {
				output <- entry
			}
		}
		close(output)
	}(up.allFiles, up.importFiles)

	// filter out already inserted files
	go func(input, output chan entry) {
		for entry := range input {
			up.numImportFiles++
			//fmt.Println("seen filter gets:", entry)
			// check if we already did this one
			tx := <-up.tx

			var c int
			err := tx.Table(ItemTable).
				//Joins("JOIN " + FolderTable + " ON " +
				//FolderTable + ".id = " + ItemTable + ".folder_id").
				//Where(entry).
				Where("filename = ? AND folder_id = (SELECT id FROM "+FolderTable+
					" WHERE path = ?)", entry.Filename, entry.Path).
				Count(&c).Error
			if err != nil {
				if strings.Contains(err.Error(), "no rows in result set") {
					log.Log.Println("this should not happen.")
					up.tx <- tx
					continue
				}
				log.Log.Println("sql error:", err)
				up.success <- false
			} else {
				up.tx <- tx
			}
			if c != 0 {
				// this one is already in the db
				//TODO check if the tags have changed anyway
				//log.Log.Println("skipping", entry)
			} else {
				//log.Log.Println("not skipping", entry)
				output <- entry
			}
		}
		close(output)
	}(up.importFiles, up.newFiles)

	// analyze remaining files
	go func(input chan entry, success chan bool) {
		for entry := range input {
			up.numNewFiles++
			err := up.analyze(path.Join(entry.Path, entry.Filename),
				entry.Path, entry.Filename)
			if err != nil {
				up.numFailedFiles++
				fmt.Println("import error: ", err)
			}
		}
		up.success <- true
		close(up.success)
	}(up.newFiles, up.success)

	success := true
	for v := range up.success {
		if !v {
			up.stopStepping <- true
			success = false
			log.Log.Println("Rolling back...")
			if err := tx.Rollback().Error; err != nil {
				log.Log.Println("rollback error:", err)
			}
			break
		}
	}
	if success {
		tx := <-up.tx
		log.Log.Println("Committing imported files...")
		if err := tx.Commit().Error; err != nil {
			log.Log.Println("Updater error:", err)
		}
	}
	d.db.LogMode(false)
	log.Log.Println("Filebase updated:",
		"\nTotal Files:      ", up.numAllFiles,
		"\nNon-ignored Files:", up.numImportFiles,
		"\nNew Files:        ", up.numNewFiles,
		"\nImported Files:   ", up.numImportedFiles,
		"\nInvalid/Non-media:", up.numInvalidFiles,
		"\nFailed Files:     ", up.numFailedFiles)

	d.c.UnregisterJob(up.job)
}