Exemple #1
0
        protected void SaveSong(MetaInfo SongInfo)
        {
            System.Boolean DownloadCovers = false;
            System.String  AlbumPath      = "";
            System.String  NewFilePath    = "";

            lock (LastManager.StreamLocker)
            {
                Stream       RadioStream = (Stream)this.ReadHandle.AsyncState;
                System.Int32 Count       = RadioStream.EndRead(this.ReadHandle);

                if (this.SkipSave || !SongInfo.Streaming)
                {
                    //Close file
                    this.TempFile.Close();

                    //Create or overwrite tempfile
                    this.TempFile = File.Create(PathSettings.TempFilePath);

                    //Start recording agian
                    this.ReadHandle = RadioStream.BeginRead(this.Buffer, 0, this.Buffer.Length, new System.AsyncCallback(this.TempSave), RadioStream);

                    //Change SkipSave
                    this.SkipSave = false;
                }
                else
                {
                    //Write last data from stream
                    this.TempFile.Write(this.Buffer, 0, Count);

                    //Write metadata to stream as ID3v1
                    SongInfo.AppendID3(this.TempFile);

                    //Write the file, and close it
                    this.TempFile.Flush();
                    this.TempFile.Close();
                    this.TempFile.Dispose();

                    //Filesystem paths
                    AlbumPath   = this.MusicPath + System.IO.Path.DirectorySeparatorChar + LastManager.RemoveIllegalChars(SongInfo.Artist) + System.IO.Path.DirectorySeparatorChar + LastManager.RemoveIllegalChars(SongInfo.Album) + System.IO.Path.DirectorySeparatorChar;
                    NewFilePath = AlbumPath + LastManager.RemoveIllegalChars(SongInfo.Track) + ".mp3";

                    //Dont overwrite file if it already exist, new rip may be bad, and we should leave it to the user to sort them manually
                    if (File.Exists(NewFilePath))
                    {
                        File.Delete(PathSettings.TempFilePath);
                    }
                    else
                    {
                        if (!Directory.Exists(AlbumPath))
                        {
                            Directory.CreateDirectory(AlbumPath);
                        }
                        File.Move(PathSettings.TempFilePath, NewFilePath);
                    }

                    //Create or overwrite tempfile
                    this.TempFile = File.Create(PathSettings.TempFilePath);

                    //Start recording agian
                    this.ReadHandle = RadioStream.BeginRead(this.Buffer, 0, this.Buffer.Length, new System.AsyncCallback(this.TempSave), RadioStream);

                    //Set download covers, do this outside the lock.
                    DownloadCovers = true;
                }
            }
            if (DownloadCovers)
            {
                //Download covers
                WebClient Client = new WebClient();

                if ((!File.Exists(AlbumPath + "SmallCover.jpg")) && SongInfo.AlbumcoverSmall != null)
                {
                    Client.DownloadFile(SongInfo.AlbumcoverSmall, AlbumPath + "SmallCover.jpg");
                }

                if ((!File.Exists(AlbumPath + "MediumCover.jpg")) && SongInfo.AlbumcoverMedium != null)
                {
                    Client.DownloadFile(SongInfo.AlbumcoverMedium, AlbumPath + "MediumCover.jpg");
                }

                if ((!File.Exists(AlbumPath + "LargeCover.jpg")) && SongInfo.AlbumcoverLarge != null)
                {
                    Client.DownloadFile(SongInfo.AlbumcoverLarge, AlbumPath + "LargeCover.jpg");
                }
            }
        }