public static Track GetTrackIfExists(this IPod dev, NewTrack newTrack) { foreach (Track existing in dev.Tracks) { if (existing.Title == newTrack.Title && existing.Artist == newTrack.Artist && existing.Album == newTrack.Album && existing.TrackNumber == newTrack.TrackNumber) { return existing; } } return null; }
public static string Get(NewTrack track, string format, bool escapeName = false) { string newName = format; newName = newName.Replace("[Title]", getSafeTag(track.Title, escapeName)); newName = newName.Replace("[Artist]", getSafeTag(track.Artist, escapeName)); newName = newName.Replace("[Album]", getSafeTag(track.Album, escapeName)); newName = newName.Replace("[TrackNumber]", track.TrackNumber.ToString("00")); newName = newName.Replace("[Genre]", getSafeTag(track.Genre, escapeName)); newName = newName.Replace("[Composer]", getSafeTag(track.Composer, escapeName)); newName = newName.Replace("[AlbumArtist]", getSafeTag(track.AlbumArtist, escapeName)); newName = newName.Replace("[DiscNumber]", track.DiscNumber.ToString()); newName = newName.Replace("[Year]", track.Year.ToString()); while (newName.Contains(" \\")) { newName = newName.Replace(" \\", "\\"); } return newName; }
protected override bool workOnDevice() { bool isMetaPlayListName = IPodTrackFormatter.HasMetaInfo(_targetPlayListName); if (!isMetaPlayListName) createPlayList(_targetPlayListName); _trackFactory = new IPodTrackFactory(); notProcessedItems.Clear(); // add notProcessedItems logic var fse = new FileSystemEnumerator(_source, _files); var copyList = new List<string>(); ProgressFormHelper.Invoke(f => { copyList = fse.FetchFiles().ToList(); }, MsgStr.MsgGettingFiles); // critical !!! if (ProgressFormHelper.LastError != null) throw ProgressFormHelper.LastError; int progress = 0; int totalCount = copyList.Count; totalByteToCopy = fse.TotalFileSize; foreach (string fileName in copyList) { if (!canExecute()) return false; setProgress(progress++, totalCount); if (!tryDo(() => { _newTrack = _trackFactory.Get(fileName); }, canSkip: true, obj: fileName)) continue; if (_newTrack == null) continue; if (isMetaPlayListName) createPlayList(IPodTrackFormatter.Get(_newTrack, _targetPlayListName)); if (ifTargetExist()) continue; // copy track local before adding string newFileName = Path.Combine( device.FileSystem.IPodControlPath, Path.GetRandomFileName() + Path.GetExtension(fileName)); if (!copyFile(fileName, newFileName)) continue; if (!canExecute()) return false; // why ??? //_newTrack.FilePath = newFileName; Track addedTrack = null; // try add track to device if (tryDo(() => { addedTrack = device.Tracks.Add(_newTrack); }, canSkip: true, obj: _newTrack)) { // if added if (!_targetPlaylist.IsMaster) { tryDo(() => _targetPlaylist.AddTrack(addedTrack), canSkip: true, obj: addedTrack); } if (isMoveOperation) { // remove original file on move tryDo(() => { File.SetAttributes(_newTrack.FilePath, FileAttributes.Normal); File.Delete(_newTrack.FilePath); }, canSkip: true, obj: _newTrack.FilePath); } } else { // if not remove tmp-copy file if (File.Exists(newFileName)) { tryDo(() => { File.SetAttributes(newFileName, FileAttributes.Normal); File.Delete(newFileName); }, canSkip: true, obj: newFileName); } } } if (isMoveOperation) { // remvoe dirs on move file foreach (string dir in fse.GetBypassDirectores()) { if (!canExecute()) return false; if (Directory.GetFiles(dir).Length == 0) { tryDo(() => Directory.Delete(dir), canSkip: true, obj: dir); } } } return true; }
public NewTrack Get(string filePath) { var file = new FileInfo(filePath); if (!_mmForMedia.Compare(file.Name)) { return null; } var newTrack = new NewTrack(); using (TagLib.File mediaFile = TagLib.File.Create(file.FullName)) { newTrack.FilePath = file.FullName; newTrack.Artist = mediaFile.Tag.FirstPerformer; newTrack.Year = mediaFile.Tag.Year; newTrack.Album = mediaFile.Tag.Album; newTrack.TrackNumber = mediaFile.Tag.Track; newTrack.Title = mediaFile.Tag.Title; newTrack.Genre = mediaFile.Tag.FirstGenre; newTrack.AlbumTrackCount = mediaFile.Tag.TrackCount; newTrack.DiscNumber = mediaFile.Tag.Disc; newTrack.TotalDiscCount = mediaFile.Tag.DiscCount; newTrack.AlbumArtist = mediaFile.Tag.FirstAlbumArtist; newTrack.Composer = mediaFile.Tag.FirstComposer; newTrack.Comments = mediaFile.Tag.Comment; newTrack.Length = (uint)mediaFile.Properties.Duration.TotalMilliseconds; newTrack.Bitrate = (uint)mediaFile.Properties.AudioBitrate; newTrack.IsVideo = mediaFile.Properties.MediaTypes.HasFlag(TagLib.MediaTypes.Video); if (string.IsNullOrEmpty(newTrack.Title)) newTrack.Title = Path.GetFileNameWithoutExtension(file.Name); bool artworkFileInTags = false; string artworkFile; if (mediaFile.Tag.Pictures.Length > 0 && mediaFile.Tag.Pictures[0].Data.Count > 100) { artworkFile = Path.GetTempFileName(); File.WriteAllBytes(artworkFile, mediaFile.Tag.Pictures[0].Data.Data); artworkFileInTags = true; newTrack.ArtworkFile = artworkFile; _forDeleteList.Add(artworkFile); } if (!artworkFileInTags) { string artFile = Directory .GetFiles(file.Directory.FullName) .Where(f => _mmForArt.Compare(f)) .FirstOrDefault(); if (!string.IsNullOrEmpty(artFile)) { artworkFile = Path.Combine(file.Directory.FullName, artFile); if (File.Exists(artworkFile)) newTrack.ArtworkFile = artworkFile; } } } return newTrack; }