Example #1
0
        /// <summary>
        /// Ensures the part exists.
        /// </summary>
        /// <param name="zipArchiveEntryItem">Zip archive entry item.</param>
        /// <param name="parts">Parts.</param>
        private void EnsurePartExists(ZipArchiveEntryItem zipArchiveEntryItem, IEnumerable <string> parts, bool isDirectory)
        {
            if (parts.Any())
            {
                var title = parts.First();

                if (!isDirectory && Regex.IsMatch(title, @"^[\w,\s-]+\.[A-Za-z]{3}$"))
                {
                    if (zipArchiveEntryItem.Files == null)
                    {
                        zipArchiveEntryItem.Files = new List <string>();
                    }

                    zipArchiveEntryItem.Files.Add(title);

                    return;
                }

                ZipArchiveEntryItem child = zipArchiveEntryItem.Folders.SingleOrDefault(x => x.Name.Equals(title));

                if (child == null)
                {
                    child = new ZipArchiveEntryItem()
                    {
                        Name    = title,
                        Folders = new List <ZipArchiveEntryItem>()
                    };

                    zipArchiveEntryItem.Folders.Add(child);
                }

                EnsurePartExists(child, parts.Skip(1), isDirectory);
            }
        }
Example #2
0
        /// <summary>
        /// Encrypts the paths.
        /// </summary>
        /// <returns>The paths.</returns>
        /// <param name="paths">Paths.</param>
        public override ZipArchiveEntryItem EncryptPaths(ZipArchiveEntryItem paths)
        {
            if (paths == null)
            {
                throw new Exception("Paths must not be null!");
            }

            EnsureFieldsEncrypted(paths, AesEncryptionHelper);

            return(paths);
        }
Example #3
0
        public Result <string> Process(IFormFile file)
        {
            var result = ValidateFormat(file);

            if (result.IsFailure)
            {
                return(Result.Fail <string>(result.Error));
            }

            ZipArchiveEntryItem paths = PopulatePaths(file);

            ZipArchiveEntryItem encryptedPaths = EncryptPaths(paths);

            string json = FormJSON(encryptedPaths);

            return(Result.Ok(json));
        }
Example #4
0
        private void EnsureFieldsEncrypted(ZipArchiveEntryItem paths, IAesEncryptionHelper aesEncryptionHelper)
        {
            paths.Name = aesEncryptionHelper.Encrypt(paths.Name);

            if (paths.Files != null && paths.Files.Any())
            {
                for (int i = 0; i < paths.Files.Count(); i++)
                {
                    paths.Files[i] = aesEncryptionHelper.Encrypt(paths.Files[i]);
                }
            }

            if (paths.Folders != null && paths.Folders.Any())
            {
                foreach (var folder in paths.Folders)
                {
                    EnsureFieldsEncrypted(folder, aesEncryptionHelper);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Resolves the paths.
        /// </summary>
        /// <returns>The paths.</returns>
        /// <param name="entries">Entries.</param>
        public ZipArchiveEntryItem ResolvePaths(IEnumerable <ZipEntryInfo> entries)
        {
            if (entries == null)
            {
                throw new Exception("Entry collection must not be null");
            }

            List <Tuple <string, bool> > fullNames = PopulateFullNames(entries);

            var root = new ZipArchiveEntryItem()
            {
                Name    = "/",
                Folders = new List <ZipArchiveEntryItem>()
            };

            foreach (var path in fullNames)
            {
                var parts = path.Item1.Split('/');

                EnsurePartExists(root, parts, path.Item2);
            }

            return(root);
        }
Example #6
0
 /// <summary>
 /// Forms the json.
 /// </summary>
 /// <returns>The json.</returns>
 /// <param name="paths">Paths.</param>
 public override string FormJSON(ZipArchiveEntryItem paths)
 {
     return(JsonConvert.SerializeObject(paths));
 }
Example #7
0
 /// <summary>
 /// Forms the json.
 /// </summary>
 /// <returns>The json.</returns>
 /// <param name="paths">Paths.</param>
 public abstract string FormJSON(ZipArchiveEntryItem paths);
Example #8
0
 /// <summary>
 /// Encrypts the paths.
 /// </summary>
 /// <returns>The paths.</returns>
 /// <param name="paths">Paths.</param>
 public abstract ZipArchiveEntryItem EncryptPaths(ZipArchiveEntryItem paths);