public Task ExportPackageAsync() { var exporter = new AnkiPackageExporter(collection, deckShowContextMenu.Id); if (exportMediaCheckBox.IsChecked == null || exportMediaCheckBox.IsChecked == false) { exporter.IncludeMedia = false; } else { exporter.IncludeMedia = true; } if (exportScheduleCheckBox.IsChecked == null || exportScheduleCheckBox.IsChecked == false) { exporter.IncludeSched = false; } else { exporter.IncludeSched = true; } exporter.ExportFinishedEvent += ExporterExportFinishedEventHandler; var split = deckShowContextMenu.Name.Split(UIHelper.ILLEGAL_NAME_CHAR, StringSplitOptions.RemoveEmptyEntries); var fileName = String.Join("_", split) + ".apkg"; Task task = Task.Run(async() => { await exporter.ExportInto(exportFolder, fileName); }); return(task); }
public async Task TestExportAnkiPkgSimpleCollection() { string fileName = "今日.mp3"; string path = sourceCollection.Media.MediaFolder.Path + "\\" + fileName; //Add a test file to the media folder using (FileStream file = new FileStream(path, FileMode.Create)) { byte[] buf = Encoding.UTF8.GetBytes("test"); file.Write(buf, 0, buf.Length); } var n = sourceCollection.NewNote(); n.SetItem("Front", "[sound:今日.mp3]"); sourceCollection.AddNote(n); var export = new AnkiPackageExporter(sourceCollection); await export.ExportInto(tempExport, "ankitest.apkg"); var tempfolder2 = await Utils.localFolder.CreateFolderAsync("tempfolder2"); using (Collection col2 = await Utils.GetEmptyCollection(tempfolder2)) { var fileToImport = await tempExport.GetFileAsync("ankitest.apkg"); var imp = new AnkiPackageImporter(col2, fileToImport); await imp.Run(); Assert.AreEqual(3, col2.CardCount()); StorageFile mediaFile = await col2.Media.MediaFolder.TryGetItemAsync(fileName) as StorageFile; Assert.IsNotNull(mediaFile); Assert.AreEqual(fileName, mediaFile.Name); } }
public async Task TestExportAnkiPkgFullCollection() { //Copy apkg file to test folder string assetFileName = @"ms-appx:///TestAssets/collection.apkg"; StorageFile assetFile = await StorageFile.GetFileFromApplicationUriAsync( new Uri(assetFileName)); await assetFile.CopyAsync(Utils.localFolder, "collection.apkg"); //Empty cards and notes var listNids = sourceCollection.Database.QueryColumn <NoteTable>("select id from notes"); var nids = (from s in listNids select s.Id).ToArray(); sourceCollection.RemoveNotesAndCards(nids); Assert.AreEqual(0, sourceCollection.CardCount()); //First import all cards and notes var fileToImport = await tempExport.GetFileAsync("collection.apkg"); var import = new AnkiPackageImporter(sourceCollection, fileToImport); await import.Run(); import.Close(); //Get number of cards and mdeia files int numberOfCardBefore = sourceCollection.CardCount(); int numberOfMediaFileBefore = (await sourceCollection.Media.MediaFolder.GetFilesAsync()).Count(); //Export it var export = new AnkiPackageExporter(sourceCollection); await export.ExportInto(tempExport, "collection.apkg"); //Reimport it again var tempfolder2 = await Utils.localFolder.CreateFolderAsync("tempfolder2"); using (Collection col2 = await Utils.GetEmptyCollection(tempfolder2)) { import = new AnkiPackageImporter(col2, fileToImport); await import.Run(); import.Close(); //Get number of cards and mdeia files int numberOfCardAfter = col2.CardCount(); int numberOfMediaFileAfter = (await col2.Media.MediaFolder.GetFilesAsync()).Count(); Assert.AreEqual(numberOfCardBefore, numberOfCardAfter); Assert.AreEqual(numberOfMediaFileBefore, numberOfMediaFileAfter); } await tempfolder2.DeleteAsync(); }