コード例 #1
0
ファイル: FilePackagerTests.cs プロジェクト: heran/DekiWiki
        public void Pack_and_unpack_manifest() {

            // Arrange
            string directory = Path.Combine(Path.GetTempPath(), StringUtil.CreateAlphaNumericKey(6));
            Directory.CreateDirectory(directory);
            List<XDoc> docs = new List<XDoc>();
            docs.Add(new XDoc("doc1").Attr("dataid", "a"));
            docs.Add(new XDoc("doc2").Attr("dataid", "b"));
            docs.Add(new XDoc("doc3").Attr("dataid", "c"));
            List<Tuplet<string, MemoryStream>> data = new List<Tuplet<string, MemoryStream>>();
            foreach(XDoc doc in docs) {
                string id = doc["@dataid"].AsText;
                data.Add(new Tuplet<string, MemoryStream>(id, new MemoryStream(doc.ToBytes())));
            }
            XDoc manifest = new XDoc("manifest")
                .Start("page").Attr("dataid", "a").End()
                .Start("page").Attr("dataid", "b").End()
                .Start("page").Attr("dataid", "c").End();

            // Act
            using(FilePackageWriter packageWriter = new FilePackageWriter(directory)) {
                foreach(Tuplet<string, MemoryStream> tuple in data) {
                    var item = new ExportItem(tuple.Item1, tuple.Item2, tuple.Item2.Length, new XDoc("item").Elem("path", "abc"));
                    packageWriter.WriteDataAsync(item, new Result()).Wait();
                }
                packageWriter.WriteManifest(manifest, new Result()).Wait();
            }

            XDoc manifest2;
            List<XDoc> docs2 = new List<XDoc>();
            using(FilePackageReader packageReader = new FilePackageReader(directory)) {
                manifest2 = packageReader.ReadManifest(new Result<XDoc>()).Wait();
                foreach(XDoc id in manifest2["*/@dataid"]) {
                    using(ImportItem item = packageReader.ReadData(new ImportItem(id.AsText, null, null), new Result<ImportItem>()).Wait()) {
                        using(StreamReader reader = new StreamReader(item.Data)) {
                            docs2.Add(XDocFactory.From(reader, MimeType.TEXT_XML));
                        }
                    }
                }
            }

            // Assert
            Assert.IsTrue(File.Exists(Path.Combine(directory, "package.xml")));
            Assert.AreEqual(ToCanonical(manifest), ToCanonical(manifest2));
            Assert.AreEqual(docs.Count, docs2.Count);
            foreach(var doc in docs) {
                Assert.IsTrue(docs2.Select(x => x == doc).Any());
            }
        }
コード例 #2
0
ファイル: ImportProgram.cs プロジェクト: heran/DekiWiki
 private static void Import(Opts opts) {
     bool createdTempFile = false;
     if(opts.FilePath.StartsWith("http:") || opts.FilePath.StartsWith("https:")) {
         DreamMessage packageMessage = Plug.New(opts.FilePath).InvokeEx("GET", DreamMessage.Ok(), new Result<DreamMessage>()).Wait();
         if(!packageMessage.IsSuccessful) {
             throw new Exception(string.Format("Unable to download package from '{0}'", opts.FilePath));
         }
         opts.FilePath = Path.GetTempFileName();
         opts.Archive = true;
         createdTempFile = true;
         using(Stream tempStream = File.Create(opts.FilePath)) {
             packageMessage.ToStream().CopyTo(tempStream, packageMessage.ContentLength, new Result<long>()).Wait();
         }
     }
     IPackageReader packageReader;
     if(opts.Archive) {
         if(!File.Exists(opts.FilePath)) {
             throw new ConfigurationException("No such file: {0}", opts.FilePath);
         }
         if(opts.Test) {
             return;
         }
         packageReader = new ArchivePackageReader(opts.FilePath);
     } else {
         if(!Directory.Exists(opts.FilePath)) {
             throw new ConfigurationException("No such directory: {0}", opts.FilePath);
         }
         if(opts.Test) {
             return;
         }
         packageReader = new FilePackageReader(opts.FilePath);
     }
     ImportManager manager;
     try {
         var manifest = packageReader.ReadManifest(new Result<XDoc>()).Wait();
         FixupManifest(manifest, opts);
         var forceOverwrite = !(opts.PreserveLocalChanges ?? true);
         var importer = opts.ImportRelto.HasValue
             ? Importer.CreateAsync(opts.DekiApi, manifest, opts.ImportRelto.Value, forceOverwrite, new Result<Importer>()).Wait()
             : Importer.CreateAsync(opts.DekiApi, manifest, opts.ImportReltoPath, forceOverwrite, new Result<Importer>()).Wait();
         manager = new ImportManager(importer, packageReader);
     } catch(Exception e) {
         if(createdTempFile) {
             File.Delete(opts.FilePath);
         }
         throw new Exception(string.Format("Import failed: {0}", e.Message), e);
     }
     manager.MaxRetries = opts.Retries;
     Result result = manager.ImportAsync(new Result());
     int completed = 0;
     Console.WriteLine("Importing:");
     while(!result.HasFinished) {
         Thread.Sleep(200);
         if(manager.CompletedItems <= completed) {
             continue;
         }
         if(SysUtil.IsUnix) {
             Console.WriteLine("  {0} of {1} files ({2:0}%)", manager.CompletedItems, manager.TotalItems, 100.0 * manager.CompletedItems / manager.TotalItems);
         } else {
             Console.Write("  {0} of {1} files ({2:0}%)      \r", manager.CompletedItems, manager.TotalItems, 100.0 * manager.CompletedItems / manager.TotalItems);
         }
         completed = manager.CompletedItems;
     }
     if(!SysUtil.IsUnix) {
         Console.WriteLine();
     }
     if(createdTempFile) {
         File.Delete(opts.FilePath);
     }
     if(result.HasException) {
         ImportException importException = result.Exception as ImportException;
         if(importException != null) {
             Console.WriteLine("Import failed on Item:\r\n{0}", importException.ManifestItem.ToPrettyString());
         }
         throw new Exception(string.Format("Import failed: {0}", result.Exception.Message), result.Exception);
     }
 }
コード例 #3
0
 public static Result<ImportManager> CreateFileImportManagerAsync(Plug dekiApi, string reltopath, string packageDirectory, Result<ImportManager> result) {
     var filePackager = new FilePackageReader(packageDirectory);
     return CreateAsync(dekiApi, reltopath, filePackager, result);
 }
コード例 #4
0
        public static Result <ImportManager> CreateFileImportManagerAsync(Plug dekiApi, string reltopath, string packageDirectory, Result <ImportManager> result)
        {
            var filePackager = new FilePackageReader(packageDirectory);

            return(CreateAsync(dekiApi, reltopath, filePackager, result));
        }