public override void DoWork() { FileInfo[] fileInfos = SourceFolder.GetFiles("*.zip"); Context.DatabaseDriver.BeginTransaction(); foreach (FileInfo fileInfo in fileInfos) { WorkerForm.InvokeNextStep(Path.GetFileName(fileInfo.Name)); int productId = GetOrCreateProduct(fileInfo); Product product = Context.DatabaseDriver.GetProductById(productId); int mediaId = Context.DatabaseDriver.CreateMediaAndReturnId(productId, Path.GetFileNameWithoutExtension(fileInfo.Name)); Media media = Context.DatabaseDriver.GetMediaById(mediaId); media.MediaTypeId = 14; ZipFile zf = ZipFile.Read(fileInfo.FullName); string albumName = Path.GetFileNameWithoutExtension(fileInfo.FullName); string outputPathString = Path.Combine(TargetFolder.FullName, albumName); DirectoryInfo albumOutputPath = new DirectoryInfo(outputPathString); EnsureDirectoryExists(albumOutputPath); ZipEntry[] zipEntries = zf.Entries.ToArray(); List <BandcampAlbumEntry> albumEntries = new List <BandcampAlbumEntry>(); for (int i = 0; i < zipEntries.Length; i++) { ZipEntry zipEntry = zipEntries[i]; MemoryStream ms = new MemoryStream(); zipEntry.Extract(ms); string outputFileName = zipEntry.FileName; if (outputFileName.StartsWith(albumName)) { outputFileName = outputFileName.Substring(albumName.Length); } if (outputFileName.StartsWith(" - ")) { outputFileName = outputFileName.Substring(3); } outputFileName = Path.Combine(albumOutputPath.FullName, outputFileName); FileInfo outputFile = new FileInfo(outputFileName); System.IO.File.WriteAllBytes(outputFile.FullName, ms.ToArray()); outputFile.Refresh(); if (zipEntry.FileName.ToLowerInvariant().EndsWith(".flac")) { ArbitraryStreamFileAbstraction abstraction = new ArbitraryStreamFileAbstraction(ms, zipEntry.FileName); File file = File.Create(abstraction); media.Name = file.Tag.Album; if (!IsDiscography) { if (product.Picture == null) { if (file.Tag.Pictures.Length > 0) { IPicture tagPicture = file.Tag.Pictures[0]; product.Picture = tagPicture.Data.Data; Context.DatabaseDriver.SetCover(product); } } } BandcampAlbumEntry bandcampAlbumEntry = new BandcampAlbumEntry(); bandcampAlbumEntry.FileInfo = outputFile; bandcampAlbumEntry.TrackNo = file.Tag.Track; albumEntries.Add(bandcampAlbumEntry); file.Dispose(); } WorkerForm.InvokeSetCurrentStepProgress(i, zipEntries.Length); } if (albumEntries.Count > 1) { albumEntries.Sort((x, y) => x.TrackNo.CompareTo(y.TrackNo)); FileInfo fi = new FileInfo(Path.Combine(albumOutputPath.FullName, "disc.m3u8")); StreamWriter sw = new StreamWriter(fi.OpenWrite(), Encoding.UTF8); albumEntries.ForEach(x => sw.WriteLine(String.Format("{1}", fi.Name, x.FileInfo.Name))); sw.Flush(); sw.Close(); sw.Dispose(); fi.Refresh(); media.SetDumpFile(fi); media.MetaFileContent = System.IO.File.ReadAllText(fi.FullName); } Context.DatabaseDriver.UpdateMedia(media); } Context.DatabaseDriver.EndTransaction(true); WorkerForm.InvokeClose(); }
public override void DoWork() { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = Environment.SpecialFolder.Desktop; fbd.ShowNewFolderButton = false; fbd.Description = "Quellordner auswählen"; DialogResult dialogResult = DialogResult.Abort; WorkerForm.Invoke((MethodInvoker) delegate { dialogResult = fbd.ShowDialog(WorkerForm); }); if (dialogResult != DialogResult.OK) { WorkerForm.InvokeClose(); return; } FileExtensionDictionary fileExtensionDictionary = new FileExtensionDictionary(); DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(fbd.SelectedPath); if (!sourceDirectoryInfo.Exists) { WorkerForm.InvokeMessageBox("Quellordner existiert nicht!"); WorkerForm.InvokeClose(); return; } WorkerForm.InvokeSetCurrentStepProgress(1); foreach (FileInfo fileInfo in sourceDirectoryInfo.GetFiles()) { fileExtensionDictionary.CountFile(fileInfo); } FileInfo singleFileOutput = null; DirectoryInfo outputDir; string singleFileFilter = null; bool singleFileMode = false; string outputExtension = null; bool isAudiobook = false; if (fileExtensionDictionary.HasExtension(".dvd") && fileExtensionDictionary.HasExtension(".iso")) { singleFileMode = true; singleFileFilter = "DVDISO (*.iso)|*.iso"; outputExtension = ".iso"; } else if (fileExtensionDictionary.CountExtensions(".iso") == 1 && fileExtensionDictionary.CountExtensions(".md5") == 1 && fileExtensionDictionary.CountExtensions(".ibg") == 1) { singleFileMode = true; singleFileFilter = "DVDISO (*.iso)|*.iso"; outputExtension = ".iso"; } else if (fileExtensionDictionary.CountExtensions(".flac") > 1 && fileExtensionDictionary.HasExtension(".m3u8")) { singleFileMode = false; } else if (fileExtensionDictionary.CountExtensions(".mkv") > 1 && fileExtensionDictionary.HasExtension(".m3u8")) { singleFileMode = false; } else if (fileExtensionDictionary.HasExtension(".cue") && fileExtensionDictionary.HasExtension(".bin")) { singleFileMode = true; singleFileFilter = "CDROM RAW (*.bin)|*.bin"; outputExtension = ".bin"; } else if (fileExtensionDictionary.HasExtension(".3ds") && fileExtensionDictionary.HasExtension(".bin")) { singleFileMode = true; singleFileFilter = "3DS ROM (*.3ds)|*.3ds"; outputExtension = ".3ds"; } else if (fileExtensionDictionary.HasExtension(".xci")) { singleFileMode = true; singleFileFilter = "Nintendo Switch ROM (*.xci)|*.xci"; outputExtension = ".xci"; } else if (fileExtensionDictionary.CountExtensions(".mkv") == 1 && fileExtensionDictionary.DifferentExtension == 1) { singleFileMode = true; singleFileFilter = "MatrosKa Video (*.mkv)|*.mkv"; outputExtension = ".mkv"; } else if (fileExtensionDictionary.CountExtensions(".wav") == 1 && fileExtensionDictionary.HasExtension(".ibg")) { isAudiobook = true; singleFileMode = true; singleFileFilter = "MP3 (*.mp3)|*.mp3"; outputExtension = ".mp3"; } else { MessageBox.Show("Failed to detect Dump Type"); WorkerForm.InvokeClose(); return; } WorkerForm.InvokeSetCurrentStepProgress(2); MediaPickerForm mpf = new MediaPickerForm(); WorkerForm.Invoke((MethodInvoker) delegate { dialogResult = mpf.ShowDialog(WorkerForm); }); if (dialogResult != DialogResult.OK) { WorkerForm.InvokeClose(); return; } if (singleFileMode) { SaveFileDialog ofd = new SaveFileDialog(); ofd.Filter = singleFileFilter; ofd.FileName = GuessFilename(mpf, outputExtension); if (fileExtensionDictionary.CountExtensions(".iso") == 1) { FileInfo fileInfo = fileExtensionDictionary.GetFileFromExtension(".iso"); string ps2sku = PlaystationSkuDetector.DetectPs2Sku(fileInfo); if (ps2sku != null) { ofd.FileName = ps2sku + "." + ofd.FileName; } } if (fileExtensionDictionary.CountExtensions(".xci") == 1) { ofd.FileName = fileExtensionDictionary.GetFileFromExtension(".xci").Name; } WorkerForm.Invoke((MethodInvoker) delegate { dialogResult = ofd.ShowDialog(WorkerForm); }); if (dialogResult != DialogResult.OK) { WorkerForm.InvokeClose(); return; } singleFileOutput = new FileInfo(ofd.FileName); outputDir = singleFileOutput.Directory; } else { fbd = new FolderBrowserDialog(); fbd.RootFolder = Environment.SpecialFolder.Desktop; fbd.SelectedPath = GuessFolderName(mpf); fbd.ShowNewFolderButton = true; fbd.Description = GuessFolderName(mpf); WorkerForm.Invoke((MethodInvoker) delegate { dialogResult = fbd.ShowDialog(WorkerForm); }); if (dialogResult != DialogResult.OK) { WorkerForm.InvokeClose(); return; } outputDir = new DirectoryInfo(fbd.SelectedPath); } WorkerForm.InvokeSetCurrentStepProgress(3); AzusaContext azusaContext = AzusaContext.GetInstance(); IDatabaseDriver databaseDriver = azusaContext.DatabaseDriver; Media mediaById = databaseDriver.GetMediaById(mpf.SelectedMedia.MediaId); foreach (FileInfo fileInfo in sourceDirectoryInfo.GetFiles()) { fileInfo.Refresh(); if (!fileInfo.Exists) { continue; } WorkerForm.InvokeNextStep(fileInfo.Name); string ext = fileInfo.Extension.ToLower(); switch (ext) { case ".3ds": if (!string.IsNullOrEmpty(mediaById.DumpStorageSpacePath)) { continue; } if (singleFileMode) { CopyFile(fileInfo, singleFileOutput); mediaById.SetDumpFile(singleFileOutput); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); } else { throw new NotImplementedException("3ds in multi file mode."); } break; case ".iso": if (!string.IsNullOrEmpty(mediaById.DumpStorageSpacePath)) { continue; } if (singleFileMode) { CopyFile(fileInfo, singleFileOutput); mediaById.SetDumpFile(singleFileOutput); FileStream fileStream = singleFileOutput.OpenRead(); mediaById.SetFilesystemMetadata(fileStream); fileStream.Close(); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); } else { throw new NotImplementedException("iso in multi file mode."); } break; case ".xci": if (!string.IsNullOrEmpty(mediaById.DumpStorageSpacePath)) { continue; } if (singleFileMode) { CopyFile(fileInfo, singleFileOutput); mediaById.SetDumpFile(singleFileOutput); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); } else { throw new NotImplementedException("xci in multi file mode."); } break; case ".dvd": if (!string.IsNullOrEmpty(mediaById.CueSheetContent)) { continue; } mediaById.CueSheetContent = File.ReadAllText(fileInfo.FullName); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); break; case ".md5": if (!string.IsNullOrEmpty(mediaById.ChecksumContent)) { continue; } mediaById.ChecksumContent = File.ReadAllText(fileInfo.FullName); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); break; case ".ibg": if (!string.IsNullOrEmpty(mediaById.GraphDataContent)) { continue; } mediaById.GraphDataContent = File.ReadAllText(fileInfo.FullName); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); break; case ".flac": if (singleFileMode) { throw new NotImplementedException("single file .flac"); } else { FileInfo outputFileInfo = new FileInfo(Path.Combine(outputDir.FullName, fileInfo.Name)); CopyFile(fileInfo, outputFileInfo); AttemptDelete(fileInfo); } break; case ".mkv": if (singleFileMode) { CopyFile(fileInfo, singleFileOutput); mediaById.SetDumpFile(singleFileOutput); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); } else { FileInfo outputFileInfo = new FileInfo(Path.Combine(outputDir.FullName, fileInfo.Name)); CopyFile(fileInfo, outputFileInfo); AttemptDelete(fileInfo); } break; case ".log": if (!string.IsNullOrEmpty(mediaById.LogfileContent)) { continue; } mediaById.LogfileContent = File.ReadAllText(fileInfo.FullName); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); break; case ".cdt": mediaById.CdTextContent = File.ReadAllBytes(fileInfo.FullName); databaseDriver.UpdateMedia(mediaById); AttemptDelete(fileInfo); break; case ".m3u8": if (string.IsNullOrEmpty(mediaById.DumpStorageSpacePath)) { FileInfo outputFileInfo = new FileInfo(Path.Combine(outputDir.FullName, fileInfo.Name)); CopyFile(fileInfo, outputFileInfo); } string playlist = File.ReadAllText(fileInfo.FullName); bool deletem3u8 = false; if (string.IsNullOrEmpty(mediaById.PlaylistContent)) { mediaById.PlaylistContent = playlist; deletem3u8 = true; } if (string.IsNullOrEmpty(mediaById.MetaFileContent)) { mediaById.MetaFileContent = playlist; deletem3u8 = true; } if (deletem3u8) { AttemptDelete(fileInfo); } databaseDriver.UpdateMedia(mediaById); break; case ".bin": if (fileExtensionDictionary.HasExtension(".3ds") && fileInfo.Length < 1024) { /*if (fileInfo.Name.ToLowerInvariant().EndsWith("-priv.bin")) * { * mediaById.Priv = File.ReadAllBytes(fileInfo.FullName); * databaseDriver.UpdateMedia(mediaById); * AttemptDelete(fileInfo); * continue; * } * * if (fileInfo.Name.ToLowerInvariant().Equals("jedecid_and_sreg.bin")) * { * mediaById.JedecId = File.ReadAllBytes(fileInfo.FullName); * databaseDriver.UpdateMedia(mediaById); * AttemptDelete(fileInfo); * continue; * }*/ } if (!string.IsNullOrEmpty(mediaById.DumpStorageSpacePath)) { continue; } if (singleFileMode) { CopyFile(fileInfo, singleFileOutput); AttemptDelete(fileInfo); } else { throw new NotImplementedException("bin in multi file mode."); } break; case ".cue": if (string.IsNullOrEmpty(mediaById.DumpStorageSpacePath)) { FileInfo outputFileInfo = new FileInfo(Path.Combine(outputDir.FullName, fileInfo.Name)); if (!isAudiobook) { CopyFile(fileInfo, outputFileInfo); mediaById.SetDumpFile(outputFileInfo); } } string cuefile = File.ReadAllText(fileInfo.FullName); bool deleteCue = false; if (string.IsNullOrEmpty(mediaById.CueSheetContent)) { mediaById.CueSheetContent = cuefile; deleteCue = true; } if (string.IsNullOrEmpty(mediaById.MetaFileContent)) { mediaById.MetaFileContent = cuefile; deleteCue = true; } if (deleteCue) { AttemptDelete(fileInfo); } databaseDriver.UpdateMedia(mediaById); break; case ".txt": if (fileInfo.Name.ToLowerInvariant().Equals("log.txt")) { goto case ".log"; } else { MessageBox.Show("Don't know how to deal with TXT file:" + fileInfo.Name); } break; case ".wav": if (singleFileMode) { string mp3Filename = Path.ChangeExtension(fileInfo.FullName, ".mp3"); string wavFilename = fileInfo.FullName; string lamePath = azusaContext.ReadIniKey("ripkit", "lamePath", null); if (string.IsNullOrEmpty(lamePath)) { MessageBox.Show("LAME ist nicht vorhanden!"); WorkerForm.InvokeClose(); return; } System.Diagnostics.Process lame = new System.Diagnostics.Process(); lame.StartInfo.FileName = lamePath; lame.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", wavFilename, mp3Filename); lame.Start(); WorkerForm.InvokeNextStep(String.Format("Encode: {0}", mp3Filename)); lame.WaitForExit(); WorkerForm.InvokeNextStep("Kopiere MP3 Datei..."); FileInfo mp3Info = new FileInfo(mp3Filename); CopyFile(mp3Info, singleFileOutput); mediaById.SetDumpFile(singleFileOutput); databaseDriver.UpdateMedia(mediaById); System.Threading.Thread.Sleep(100); AttemptDelete(fileInfo); AttemptDelete(mp3Info); } else { throw new NotImplementedException("wav in multi file mode."); } break; default: MessageBox.Show("Don't know how to deal with file:" + fileInfo.Name); break; } } sourceDirectoryInfo.Refresh(); FileInfo[] filesAfterComplete = sourceDirectoryInfo.GetFiles(); if (filesAfterComplete.Length == 0) { sourceDirectoryInfo.Delete(false); } WorkerForm.InvokeClose(); MessageBox.Show("Erfolg!"); }