Esempio n. 1
0
        public PlaylistItemFile GetTag(string filePath, PlayListItemSource source)
        {
            //No point loading the file again if we've already got the information.
            var playlistItemFile =
                _cachedPlaylistItemFiles.FirstOrDefault(
                    c => c != null && c.FileName.Equals(filePath, StringComparison.OrdinalIgnoreCase));

            if (playlistItemFile != null)
                return playlistItemFile;

            try
            {
                if (source == PlayListItemSource.Disk)
                {
                    using (var fileStream = new FileStream(filePath, FileMode.Open))
                    {
                        try
                        {
                            //TODO: Find out how to use the File.AddFileResolver as this means we can handle the custom file types we're currently catching.
                            var file = File.Create(new StreamFileAbstraction(filePath, fileStream, fileStream));
                            var isCorrupt = file.CorruptionReasons != null && file.CorruptionReasons.Any();
                            var title = isCorrupt || file.Tag.Title == null ? Path.GetFileName(filePath) : file.Tag.Title;
                            var track = isCorrupt ? 0 : file.Tag.Track;

                            //This won't be hit if the file doesn't exist.
                            playlistItemFile = new PlaylistItemFile
                            {
                                FileName = filePath,
                                Type = file is TagLib.Mpeg.AudioFile
                                     ? PlayListItemType.Audio : PlayListItemType.Video,
                                Tag = new PlayListItemFileTag
                                {
                                    Title = title,
                                    Track = track
                                }
                            };

                            var fileTypeSplit = file.GetType().FullName.Split('.');
                            if (fileTypeSplit.Length > 0)
                            {
                                switch (fileTypeSplit[1].ToLower())
                                {
                                    case "png":
                                    case "jpg":
                                    case "jpeg":
                                    case "bmp":
                                    case "image":
                                    playlistItemFile.Type = PlayListItemType.Image;
                                    break;
                                }
                            }

                            foreach (var picture in file.Tag.Pictures)
                            {
                                playlistItemFile.Tag.Pictures.Add(new PlayListItemFileTagImage
                                {
                                    MimeType = picture.MimeType,
                                    Data = new PlayListItemFileTagImageData
                                    {
                                        Data = picture.Data.Data
                                    }
                                });
                            }

                            _cachedPlaylistItemFiles.Add(playlistItemFile);
                            return playlistItemFile;
                        }
                        catch (UnsupportedFormatException)
                        {
                            //Ignore as we will create the file in TryMakeFileTag below.
                        }
                    }
                }

                playlistItemFile = TryMakeFileTag(filePath, source);
                _cachedPlaylistItemFiles.Add(playlistItemFile);
                return playlistItemFile;
            }
            catch (IOException)
            {
                //If the file is in use (already playing) then we don't want to throw an error.
                //This will however prevent the same file being added to the play list when it's currently playing
                //I can live with that ...
                return null;
            }
        }
Esempio n. 2
0
        public string GenerateForFile(string filePath, PlayListItemSource source)
        {
            var thumbnailDirectory = String.Format("{0}\\{1}", PathHelper.GetApplicationPath(), Settings.Default.ThumbnailLocation);
            if (!Directory.Exists(thumbnailDirectory))
                Directory.CreateDirectory(thumbnailDirectory);

            var tagFile = _fileTagService.GetTag(filePath, source);
            if (tagFile == null)
                return "";

            var imageFilePath = "";
            foreach (var picture in tagFile.Tag.Pictures)
            {
                imageFilePath =
                    String.Format("{0}\\{1}\\{2}.{3}",
                        PathHelper.GetApplicationPath(),
                        Settings.Default.ThumbnailLocation,
                        Path.GetFileName(filePath), picture.MimeType.Split('/').Last()
                        );

                if (File.Exists(imageFilePath))
                    return imageFilePath;

                if (string.IsNullOrEmpty(picture.FilePath))
                {
                    using (var ms = new MemoryStream(picture.Data.Data))
                    {
                        using (var returnImage = Image.FromStream(ms))
                        {
                            returnImage.Save(imageFilePath);
                        }
                    }
                }
                else
                    imageFilePath = picture.FilePath;


                return imageFilePath;
            }

            try
            {
                imageFilePath =
                    String.Format("{0}\\{1}\\{2}.{3}",
                        PathHelper.GetApplicationPath(),
                        Settings.Default.ThumbnailLocation,
                        Path.GetFileName(filePath), "jpeg"
                        );
                new Ffmpeg().GetThumbnail(filePath, imageFilePath, "120x120");

                //If Ffmpeg hasn't managed to get an image then assume this file IS an image at this point.
                if (!File.Exists(imageFilePath))
                    imageFilePath = filePath;
            }
            catch
            {
                //I don't want the lack of an image to break anything here.
            }

            return imageFilePath;
        }
Esempio n. 3
0
 private PlaylistItemFile TryMakeFileTag(string filePath, PlayListItemSource source)
 {
     switch (Path.GetExtension(filePath))
     {
         case ".pdf":
             return new PdfService().GetTag(filePath);
         default:
             if (source == PlayListItemSource.Streamed)
             {
                 return new StreamService().GetTag(filePath);
             }
             break;
     }
     return null;
 }