private void ThrowDebugException(AnkiImportFinishCode code)
 {
     throw new AnkiImportException(code);
 }
        public async override Task Run()
        {
            tempDir = await destCol.Folder.CreateFolderAsync(tempDirName, CreationCollisionOption.ReplaceExisting);

            string collectionName     = "collection.anki2";
            string mapFileName        = "media";
            string mapAnkiU           = "mediaAnkiU";
            AnkiImportFinishCode code = AnkiImportFinishCode.UnableToUnzip;

            try
            {
                using (archive = new ZipArchive(new FileStream(packageFile.Path, FileMode.Open), ZipArchiveMode.Read))
                {
                    // We extract the zip contents into a temporary directory and do a little more
                    // validation than the python client to ensure the extracted collection is an apkg.
                    try
                    {
                        PackageImportStateChangeEvent?.Invoke("Extracting package...");
                        Utils.UnZipNotFolderEntries(archive, tempDir.Path, new string[] { collectionName, mapFileName, mapAnkiU }, null);

                        //Point the sourcefolder and relative path to extracted files
                        sourceFolder       = tempDir;
                        relativePathToFile = collectionName;
                    }
                    catch (IOException e)
                    {
                        code = AnkiImportFinishCode.UnableToUnzip;
                        ThrowDebugException(AnkiImportFinishCode.UnableToUnzip, e.Message);
                        return;
                    }
                    string      colpath = "collection.anki2";
                    StorageFile colFile = await tempDir.TryGetItemAsync("collection.anki2") as StorageFile;

                    if (colFile == null)
                    {
                        code = AnkiImportFinishCode.NotFoundCollection;
                        ThrowDebugException(AnkiImportFinishCode.NotFoundCollection);
                        return;
                    }
                    // we need the media dict in advance, and we'll need a map of fname ->
                    // number to use during the import
                    PackageImportStateChangeEvent?.Invoke("Create mapping file...");
                    numToName = new Dictionary <string, string>();
                    using (Collection col = await Storage.OpenOrCreateCollection(tempDir, colpath))
                    {
                        try
                        {
                            GetAnkiMediaMapping(numToName);
                            await GetAnkiUMediaMappingIfNeeded();
                        }
                        catch (FileNotFoundException)
                        {
                            code = AnkiImportFinishCode.NotFoundMediaFile;
                            ThrowDebugException(AnkiImportFinishCode.NotFoundMediaFile);
                            return;
                        }
                        catch (IOException)
                        {
                            code = AnkiImportFinishCode.MediaFileIsCorrupted;
                            ThrowDebugException(AnkiImportFinishCode.MediaFileIsCorrupted);
                            return;
                        }
                        catch (Exception)
                        {
                            code = AnkiImportFinishCode.UnknownExpception;
                            return;
                        }
                    }
                    try
                    {
                        // run anki2 importer
                        await base.Run();

                        //WARNING: AnkiU does not support static media and Latex
                        //import static media
                        //PackageImportStateChangeEvent?.Invoke("Importing static media...");
                        //foreach (var entry in numToName)
                        //{
                        //    string file = entry.Value;
                        //    string c = entry.Key;
                        //    if (!file.StartsWith("_") && !file.StartsWith("latex-"))
                        //    {
                        //        continue;
                        //    }
                        //    StorageFile path = await destCol.Media.MediaFolder
                        //                             .TryGetItemAsync(file) as StorageFile;
                        //    if (path == null)
                        //    {
                        //        try
                        //        {
                        //            Utils.UnZipNotFolderEntries(archive, destCol.Media.MediaFolder.Path, new string[] { c }, numToName);
                        //        }
                        //        catch (IOException)
                        //        {
                        //            Debug.WriteLine("Failed to extract static media file. Ignoring.");
                        //        }
                        //    }
                        //}
                        destCol.Database.Commit();
                        code = AnkiImportFinishCode.Success;

                        //Only in AnkiU we perform this step to move all mediafiles into DeckIdFolder
                        //when importing the whole collection
                        PackageImportStateChangeEvent?.Invoke("Importing media...");
                        await ExtractSourceMediaFileToAllDeckIdFolderAsync();
                    }
                    catch (AnkiImportException e)
                    {
                        code = e.Error;
                    }
                    catch (Exception)
                    {
                        code = AnkiImportFinishCode.UnknownExpception;
                    }
                }
            }
            finally
            {
                // Clean up our temporary files
                tempDir = await destCol.Folder.TryGetItemAsync(tempDirName) as StorageFolder;

                if (tempDir != null)
                {
                    if (sourceCol != null)
                    {
                        sourceCol.Close(false);
                        sourceCol = null;
                    }
                    sourceFolder = null;
                    await tempDir.DeleteAsync();

                    tempDir = null;
                }
                AnkiPackageImporterFinishedEvent(code, ImportedNoteId.Count.ToString());
            }
        }
示例#3
0
 public AnkiImportException(AnkiImportFinishCode error, string msg) : base(msg)
 {
     this.error = error;
 }
 private void ThrowDebugException(AnkiImportFinishCode code, string message)
 {
     throw new AnkiImportException(code, message);
 }
示例#5
0
 public AnkiImportException(AnkiImportFinishCode error) : base()
 {
     this.error = error;
 }