Ejemplo n.º 1
0
        internal EmbeddedBlobStoreNamespace(
            StringKeySequence baseName,
            StringKeySequence name,
            [NotNull] Assembly assembly,
            [NotNull] IDictionary <StringKey, EmbeddedBlobStoreNamespace> children,
            [NotNull] IEnumerable <StringKey> names)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            if (children == null)
            {
                throw new ArgumentNullException(nameof(children));
            }

            if (names == null)
            {
                throw new ArgumentNullException(nameof(names));
            }

            mAssembly = assembly;
            mName     = baseName.Concat(name);
            mChildren = children;
            mNames    = new HashSet <StringKey>(names);
        }
Ejemplo n.º 2
0
        private CompressedBlobStoreSection CreateFolderStructure(StringKeySequence baseName = default(StringKeySequence))
        {
            var folder = new CompressedBlobStoreSection(baseName, mArchive);

            var childFolderKeys =
                from entry in mEntries

                where entry.Path.Segments.Length == baseName.Segments.Length + 1

                let m1                   = baseName.IsEmpty && entry.Path.Segments.Length == 1
                                  let m2 = baseName.Segments.Select((k, i) => entry.Path.Segments[i].Equals(k)).All(x => x)

                                           where m1 || m2
                                           select entry.Path;

            foreach (var key in childFolderKeys)
            {
                folder.AddChildFolder(key.Segments.Last(), CreateFolderStructure(key));
            }

            var elementKeys = new List <StringKey>(

                from entry in mEntries
                where baseName.Equals(entry.Path)
                select entry.Key);

            foreach (var element in elementKeys)
            {
                folder.AddElement(element);
            }

            return(folder);
        }
Ejemplo n.º 3
0
        public EmbeddedBlobStore([NotNull] Assembly assembly, StringKeySequence basePath = default(StringKeySequence))
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            mData = CreateHierarchicalStoreFromManifestResourceNames(assembly, basePath);
        }
Ejemplo n.º 4
0
        public ImageSource GetIcon(StringKey key, StringKey category)
        {
            if (key.IsEmpty || category.IsEmpty)
            {
                return(null);
            }

            var compositeKey = new StringKeySequence(category, key);

            if (mCache.ContainsKey(compositeKey))
            {
                return(mCache[compositeKey]);
            }

            foreach (var store in mStores.AsEnumerable().Reverse())
            {
                if (!store.HasChildStore(category))
                {
                    continue;
                }

                var categoryStore = store.GetChildStore(category, true);
                var elementKey    = new StringKey(key.RawData + ".png");

                if (!categoryStore.ContainsKey(elementKey.RawData))
                {
                    continue;
                }

                try
                {
                    using (var binaryData = categoryStore.Open(elementKey).Read())
                    {
                        var decoder = new PngBitmapDecoder(binaryData, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                        if (!decoder.Frames.Any())
                        {
                            continue;
                        }

                        var frame        = (ImageSource)decoder.Frames.First();
                        var frozenSource = (ImageSource)frame.GetAsFrozen();

                        mCache.Add(compositeKey, frozenSource);
                        return(frozenSource);
                    }
                }
                catch
                {
                    // ignore
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        public void KeySequence_Set_Correctly()
        {
            StringKeySequence keySequence = new StringKeySequence("password");

            Assert.AreEqual(8, keySequence.Length);
            Assert.AreEqual(883, keySequence.TotalStepSum);

            GetSequenceTest(keySequence, 0, 0, new byte[0]);
            GetSequenceTest(keySequence, 0, 1, new byte[] { 0x70 });
            GetSequenceTest(keySequence, 0, 5, new byte[] { 0x70, 0x61, 0x73, 0x73, 0x77 });
        }
Ejemplo n.º 6
0
        public FileSystemStore([NotNull] string directoryName, [CanBeNull] Encoding encoding = null, bool isReadOnly = false)
        {
            if (directoryName == null)
            {
                throw new ArgumentNullException(nameof(directoryName));
            }

            if (string.IsNullOrWhiteSpace(directoryName))
            {
                directoryName = Directory.GetCurrentDirectory();
            }

            if (Path.GetInvalidPathChars().Any(x => directoryName.ToString().Contains(x)))
            {
                var sb = new StringBuilder();

                sb.AppendLine($"The directory name contains invalid characters: {directoryName}.");
                sb.Append("None of the following characters are allowed:");

                foreach (var c in Path.GetInvalidPathChars())
                {
                    sb.Append($" {c}");
                }

                throw new ArgumentException(sb.ToString(), nameof(directoryName));
            }

            var tokens = directoryName.Split(Path.PathSeparator);

            mDirectoryName = directoryName;
            Identifier     = new StringKeySequence(tokens.Select(x => new StringKey(x)).ToArray());

            mEncoding   = encoding;
            mIsReadOnly = isReadOnly;

            if (!Directory.Exists(directoryName))
            {
                try
                {
                    if (!isReadOnly)
                    {
                        Directory.CreateDirectory(directoryName);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    mIsReadOnly = true;
                }
            }
        }
        internal CompressedBlobStoreSection(StringKeySequence thisFolderName, [NotNull] ZipArchive archive)
        {
            if (archive == null)
            {
                throw new ArgumentNullException(nameof(archive));
            }

            mArchive                  = archive;
            mThisFolderName           = thisFolderName;
            mChildFolders             = new Dictionary <StringKey, CompressedBlobStoreSection>();
            mElementNames             = new HashSet <StringKey>();
            mOriginalNames            = new List <StringKey>();
            mOriginalChildFolderNames = new List <StringKey>();
        }
Ejemplo n.º 8
0
 public NullStorage(Guid identifier)
 {
     mIdentifier = identifier;
     Identifier  = new StringKeySequence(new StringKey(mIdentifier.ToString("N").Substring(8)));
 }
Ejemplo n.º 9
0
 public NullStorage()
 {
     mIdentifier = Guid.NewGuid();
     Identifier  = new StringKeySequence(new StringKey(mIdentifier.ToString("N").Substring(8)));
 }
Ejemplo n.º 10
0
        private EmbeddedBlobStoreNamespace CreateHierarchicalStoreFromManifestResourceNames(Assembly assembly, StringKeySequence basePath)
        {
            var resourceNames = assembly.GetManifestResourceNames();
            var baseName      = assembly.DefinedTypes
                                .Select(x => x.Namespace)
                                .Where(x => !string.IsNullOrEmpty(x))
                                .Distinct()
                                .Where(x => resourceNames.Any(y => y.StartsWith(x)))
                                .OrderBy(x => x.Length)
                                .FirstOrDefault();

            if (!basePath.IsEmpty)
            {
                baseName += "." + basePath.ToString(".");
            }

            StringKeySequence rootName;

            if (string.IsNullOrWhiteSpace(baseName))
            {
                rootName = new StringKeySequence();
            }
            else
            {
                rootName = new StringKeySequence(baseName.Split('.'));
            }

            var names = new HashSet <StringKeySequence>(resourceNames.Select(x => CreateKeySequence(baseName, x)).OrderBy(x => x.Segments.Length));

            if (names.Count == 0)
            {
                return(new EmbeddedBlobStoreNamespace(rootName, new StringKeySequence(), assembly, new Dictionary <StringKey, EmbeddedBlobStoreNamespace>(), new StringKey[0]));
            }

            var leafNames = new HashSet <StringKeySequence>(
                from ancestor in names

                let directDescendants =
                    from descendant in names

                    let isNextLevelDescendant = descendant.Segments.Length == ancestor.Segments.Length + 1
                                                let isDescendantInAncestorSet = descendant.ToString().StartsWith(ancestor.ToString())

                                                                                where isNextLevelDescendant && isDescendantInAncestorSet

                                                                                select descendant

                                                                                where !directDescendants.Any()
                                                                                select ancestor);

            var branchNames = new HashSet <StringKeySequence>();

            foreach (var leafName in leafNames)
            {
                for (var i = 0; i < leafName.Segments.Length - 1; i++)
                {
                    var partialName = new StringKeySequence(leafName.Segments.Take(i + 1).ToArray());

                    branchNames.Add(partialName);
                }
            }

            if (branchNames.Count == 0)
            {
                return(new EmbeddedBlobStoreNamespace(rootName, new StringKeySequence(), assembly, new Dictionary <StringKey, EmbeddedBlobStoreNamespace>(), leafNames.Select(x => x.ToString(".").AsKey())));
            }

            var structure =
                from branch in branchNames

                let childBranches =
                    from childBranch in branchNames

                    let isNextLevelDescendant = childBranch.Segments.Length == branch.Segments.Length + 1
                                                let isDescendantInAncestorSet = childBranch.ToString().StartsWith(branch.ToString())

                                                                                where isNextLevelDescendant && isDescendantInAncestorSet

                                                                                select childBranch

                                                                                let childLeaves =
                        from childLeaf in leafNames

                        let isNextLevelDescendant = childLeaf.Segments.Length == branch.Segments.Length + 1
                                                    let isDescendantInAncestorSet = childLeaf.ToString().StartsWith(branch.ToString())

                                                                                    where isNextLevelDescendant && isDescendantInAncestorSet

                                                                                    select childLeaf

                                                                                    let sortKey                         = childBranches.Any() ? 0 : 1
                                                                                                              let level = branch.Segments.Length

                                                                                                                          // ReSharper disable once RedundantQueryOrderByAscendingKeyword
                                                                                                                          orderby sortKey ascending

                                                                                                                          // ReSharper disable once MultipleOrderBy
                                                                                                                          orderby level descending

                                                                                                                          select new
            {
                Name     = branch,
                Branches = childBranches,
                Leaves   = childLeaves,
                Level    = level
            };

            var branchDictionary = new Dictionary <StringKeySequence, EmbeddedBlobStoreNamespace>();

            foreach (var element in structure)
            {
                var directChildren = element.Branches.ToDictionary(x => x.Segments.Last(), x => branchDictionary[x]);
                var childLeaves    = element.Leaves.Select(x => x.Segments.Last()).ToArray();
                var container      = new EmbeddedBlobStoreNamespace(rootName, element.Name, assembly, directChildren, childLeaves);

                branchDictionary.Add(element.Name, container);
            }

            var minLength           = branchDictionary.Min(x => x.Key.Segments.Length);
            var bottomLevelBranches = branchDictionary.Where(x => x.Key.Segments.Length == minLength);
            var bottomLevelLeaves   = leafNames.Where(x => x.Segments.Length == minLength);

            return(new EmbeddedBlobStoreNamespace(
                       rootName,
                       new StringKeySequence(),
                       assembly,
                       bottomLevelBranches.ToDictionary(x => x.Key.Segments.Last(), x => x.Value),
                       bottomLevelLeaves.Select(x => x.Segments.Last())));
        }