Exemplo n.º 1
0
 public ITransactable WriteContent(ContentRecord.Builder builder, byte[] contents)
 {
     byte[] compressed;
     builder.SetContentLength((uint)contents.Length);
     if (contents.TryCompress(out compressed))
     {
         builder.SetHashContents(Hash.SHA256(compressed).ToString());
         builder.SetCompressedLength((uint)compressed.Length);
         return(WriteBytes(builder, compressed));
     }
     else
     {
         builder.SetHashContents(Hash.SHA256(contents).ToString());
         builder.ClearCompressedLength();
         return(WriteBytes(builder, contents));
     }
 }
Exemplo n.º 2
0
        public void Deduplicate(
            [Argument("site", "s", Description = "The root http address of the website copy.")]
            string site,
            [Argument("remove", "r", DefaultValue = false, Description = "True to remove the page and modify source links, otherwise inserts a redirect.")]
            bool remove,
            [Argument("noprompt", "q", DefaultValue = false, Description = "True to stop prompt for confirmation before changing content.")]
            bool noPrompt)
        {
            using (ContentStorage store = new ContentStorage(StoragePath(site), false))
            {
                Dictionary <string, string> replacements = new Dictionary <string, string>(StringComparer.Ordinal);
                Dictionary <string, string> hashes       = new Dictionary <string, string>(StringComparer.Ordinal);
                foreach (KeyValuePair <string, ContentRecord> item in store)
                {
                    if (item.Value.HasHashContents)
                    {
                        string original;
                        if (hashes.TryGetValue(item.Value.HashContents, out original))
                        {
                            replacements[item.Key] = original;
                            Console.WriteLine("{0,-38} => {1,-38}", item.Key, original);
                        }
                        else
                        {
                            hashes.Add(item.Value.HashContents, item.Key);
                        }
                    }
                }

                if (replacements.Count > 0 &&
                    (noPrompt || new ConfirmPrompt().Continue("Replace all of the above links")))
                {
                    Uri baseUri = new Uri(site, UriKind.Absolute);

                    if (remove)
                    {
                        ContentParser parser = new ContentParser(store, baseUri);
                        parser.RewriteUri += u =>
                        {
                            string target;
                            if (u.IsSameHost(baseUri) && replacements.TryGetValue(u.NormalizedPathAndQuery(), out target))
                            {
                                return(new Uri(baseUri, target));
                            }
                            return(u);
                        };
                        parser.ProcessAll();
                    }
                    foreach (string removed in replacements.Keys)
                    {
                        ContentRecord rec = store[removed];
                        store.Remove(removed);
                        if (!remove)
                        {
                            ContentRecord.Builder builder = rec.ToBuilder();
                            builder
                            .ClearCompressedLength()
                            .ClearContentLength()
                            .ClearContentStoreId()
                            .ClearContentType()
                            .ClearHashContents()
                            .SetHttpStatus((uint)HttpStatusCode.Redirect)
                            .SetContentRedirect(replacements[removed])
                            ;
                            store.Add(removed, builder.Build());
                        }
                    }
                }
            }
        }