func (this *musicManager) SaveMusicList(musicList []*element.MusicInfo) error { for _, info := range musicList { ret, musicId, err := model.MusicModelInstance().CheckMusicIsExistByNameAndAuthor(info.MusicName, info.MusicAuthor) // 已经存在本地数据库了 if ret == true { info.MusicId = musicId } else if err != nil { return err } else { musicId, err = model.MusicModelInstance().SaveMusic(info) info.MusicId = musicId } } return nil }
func (this *MusicController) fetchListenedList(userId int, w http.ResponseWriter, r *http.Request) { musicList, err := model.MusicModelInstance().FetchLoveList(userId) if err != nil { fmt.Println("fetchLoveList error: ", err) NormalResponse(w, DatabaseError) } else { var packet *LoveMusicInfoPacket = &LoveMusicInfoPacket{} for _, music := range musicList { var elementPacket LoveMusicElementPacket elementPacket.MusicID = music.MusicId elementPacket.MusicName = music.MusicName elementPacket.ArtistName = music.MusicAuthor elementPacket.AlbumName = music.AlbumName elementPacket.MusicTime = music.MusicTime elementPacket.LoveDegree = music.LoveDegree packet.Param.LoveMusicList = append(packet.Param.LoveMusicList, elementPacket) fmt.Println(music.MusicName) } body, err := json.Marshal(packet) if err != nil { fmt.Println("fetch Random List Marshal Error: ", err) } fmt.Fprintf(w, string(body)) } }
func (this *musicManager) DownloadSmallCoverImage(musicId int, callback DownloadProgressCallback) { musicInfo, err := model.MusicModelInstance().QueryMusicById(musicId) if musicInfo == nil { var stop bool callback(nil, errors.New("对应的musicID无音乐"), &stop) return } else if err != nil { var stop bool callback(nil, err, &stop) return } fmt.Println("Download small cover image: " + musicInfo.MusicName) switch musicInfo.SourceType { case element.BaiduMusicSourceType: downloadInfo := &DownloadElement{} downloadInfo.DownloadType = DownloadTypeSingle downloadInfo.DownloadContent = musicInfo.SmallCoverImagePath downloadInfo.DownloadPath = cache.CacheManagerInstance().GenerateCacheFile() downloadInfo.Progress = func(content []byte, err error, stop *bool) { callback(content, err, stop) } downloadInfo.CompleteSignal = make(chan bool) DownloadManagerInstance().AddDownloadInfoIntoQueue(downloadInfo) <-downloadInfo.CompleteSignal cache.CacheManagerInstance().DeleteCacheFile(downloadInfo.DownloadPath) case element.LocalMusicSourceType: content, err := this.readLocalFile(musicInfo.SmallCoverImagePath) var stop bool callback(content, err, &stop) } }
func (this *MusicController) searchMusic(w http.ResponseWriter, r *http.Request) { key := r.Form.Get("key") searchList, err := model.MusicModelInstance().SearchMusic(key) if err != nil { fmt.Println(err) NormalResponse(w, DatabaseError) } else { var packet *MusicInfoPacket = &MusicInfoPacket{} for _, music := range searchList { var elementPacket MusicElementPacket elementPacket.MusicID = music.MusicId elementPacket.MusicName = music.MusicName elementPacket.ArtistName = music.MusicAuthor elementPacket.AlbumName = music.AlbumName elementPacket.MusicTime = music.MusicTime packet.Param.MusicList = append(packet.Param.MusicList, elementPacket) fmt.Println(music.MusicName) } body, err := json.Marshal(packet) if err != nil { fmt.Println("fetch Random List Marshal Error: ", err) } fmt.Fprintf(w, string(body)) } }
func (this *myMusicPlayer) SearchMusic(keyword string) []*element.MusicInfo { musicList, err := model.MusicModelInstance().SearchMusic(keyword) if err == nil { return musicList } return nil }
func (this *myMusicPlayer) FetchMusicById(musicId int) *element.MusicInfo { musicInfo, err := model.MusicModelInstance().QueryMusicById(musicId) if err == nil { return musicInfo } return nil }
func (this *myMusicPlayer) FetchMusicList(musicType int) []*element.MusicInfo { musicList, err := model.MusicModelInstance().FetchRandomList(fetchCount) if err == nil { return musicList } return nil }
func (this *MusicController) listenMusic(userId int, w http.ResponseWriter, r *http.Request) { musicId, err := strconv.Atoi(r.Form.Get("musicId")) if err != nil { NormalResponse(w, InvalidParam) } else { if err = model.MusicModelInstance().ListenMusic(userId, musicId); err != nil { fmt.Println(err) NormalResponse(w, DatabaseError) } else { NormalResponse(w, OK) } } }
func (this *MusicController) loveMusic(userId int, w http.ResponseWriter, r *http.Request) { musicId, err1 := strconv.Atoi(r.Form.Get("musicId")) loveDegree, err2 := strconv.Atoi(r.Form.Get("degree")) if err1 != nil || err2 != nil { NormalResponse(w, InvalidParam) } else { if model.MusicModelInstance().LoveMusic(userId, musicId, loveDegree) != nil { NormalResponse(w, DatabaseError) } else { NormalResponse(w, OK) } } }
func (this *musicManager) DownloadMusic(musicId int, callback DownloadProgressCallback) { musicInfo, err := model.MusicModelInstance().QueryMusicById(musicId) if musicInfo == nil { var stop bool callback(nil, errors.New("对应的musicID无音乐"), &stop) return } else if err != nil { var stop bool callback(nil, err, &stop) return } fmt.Println("Download Music: " + musicInfo.MusicName) switch musicInfo.SourceType { case element.BaiduMusicSourceType: downloadInfo := &DownloadElement{} downloadInfo.Complete = func(musicInfo *element.MusicInfo) { fmt.Println("Download Success!") // 下载完成,将文件拷贝到本地目录,然后删除缓存目录 // musicInfo.MusicPath = musicInfo.DownloadPath localPath := fmt.Sprintf("./resource/%s/", musicInfo.MusicUUID) cache.CacheManagerInstance().MoveCacheFile(downloadInfo.DownloadPath, localPath) this.WriteMusicInfoIntoDB(musicInfo) } downloadInfo.Failed = func(musicInfo *element.MusicInfo, err error) { cache.CacheManagerInstance().DeleteCacheFile(downloadInfo.DownloadPath) } musicInfo.MusicUUID = this.createUUID() downloadInfo.DownloadPath = fmt.Sprintf("/tmp/%s", musicInfo.MusicUUID) downloadInfo.DownloadType = DownloadTypeMusic downloadInfo.DownloadContent = musicInfo downloadInfo.Progress = func(content []byte, err error, stop *bool) { callback(content, err, stop) } downloadInfo.CompleteSignal = make(chan bool) DownloadManagerInstance().AddDownloadInfoIntoQueue(downloadInfo) <-downloadInfo.CompleteSignal case element.LocalMusicSourceType: content, err := this.readLocalFile(musicInfo.MusicPath) var stop bool callback(content, err, &stop) } }
func (this *musicManager) WriteMusicInfoIntoDB(musicInfo *element.MusicInfo) { model.MusicModelInstance().ChangeSourceType(element.BaiduMusicSourceType, element.LocalMusicSourceType, musicInfo) }