示例#1
0
        private IPackageConfiguration ExtractAndReadConfiguration(FileStorage.IFileStore fileStore,
                                                                  Configuration.IConfigurationSerializer <StringBuilder> serializer,
                                                                  string filename)
        {
            if (!fileStore.Contains(filename))
            {
                return(null);
            }
            var extractedFilename = fileStore.Get(filename).Extract(System.IO.Path.GetTempPath(), true);

            if (!System.IO.File.Exists(extractedFilename))
            {
                return(null);
            }
            try
            {
                var config = serializer.Deserialize(new StringBuilder(System.IO.File.ReadAllText(extractedFilename)));
                return((IPackageConfiguration)Core.Configuration.ConfigurationHelper.InstantiateTypeFromConfigurationGroup(typeof(PackageConfiguration), config));
            }
            catch
            {
                // swallow any exception; return null
                return(null);
            }
            finally
            {
                FileStorage.FileStorageHelper.DeleteFile(extractedFilename);
            }
        }
示例#2
0
        /// <summary>
        /// Will connect to an existing package.
        /// </summary>
        /// <param name="packageRootFilename">The package filename.</param>
        /// <param name="configurationFilename">The filename to use when reading the <paramref name="configurationFilename"/> from the package.</param>
        /// <returns>An <see cref="IConnectedPackage"/>, which represents the desired package.</returns>
        /// <remarks>
        /// See <see cref="ConnectedPackage"/> concerning proper handling of the <see cref="ConnectedPackage"/>.
        /// </remarks>
        public IConnectedPackage Connect(string packageRootFilename,
                                         string configurationFilename)
        {
            // find package
            if (!_AllPackagesFileStore.Contains(packageRootFilename))
            {
                throw new ArgumentException($"Package not found. Package Filename={packageRootFilename}");
            }
            // extract to temp location
            var extractedPackageFilename = _AllPackagesFileStore.Get(packageRootFilename).Extract(System.IO.Path.GetTempPath(), true);

            if (!System.IO.File.Exists(extractedPackageFilename))
            {
                throw new Exception($"PackageProvider; Unable to extract Package. Filename={packageRootFilename}");
            }
            // connect IFileStore
            var packageStore = PackageFileStoreProvider.Connect(extractedPackageFilename);
            // extract PackageConfiguration
            IPackageConfiguration config = RetrieveConfiguration(packageStore, configurationFilename);

            if (config != null)
            {
                // create IPackage
                return(new ConnectedPackage(_AllPackagesFileStore, packageRootFilename, extractedPackageFilename, packageStore, config));
            }
            //
            return(null);
        }
        /// <summary>
        /// Reads the contents of the file, specified by the <paramref name="rootFilename"/>, from the <paramref name="fileStore"/> and converts it from binary data into an instance of type <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">The type to convert the contents of the <paramref name="rootFilename"/> into.</typeparam>
        /// <param name="rootFilename">The name of the file to convert.</param>
        /// <param name="fileStore">The <see cref="FileStorage.IFileStore"/> to read the file from.</param>
        /// <returns>The contents of the file, specified by the <paramref name="rootFilename"/>, from the <paramref name="fileStore"/> and converted from binary data into an instance of type <typeparamref name="T"/>.</returns>
        public T Load <T>(string rootFilename, FileStorage.IFileStore fileStore)
        {
            // check for errors
            if (string.IsNullOrWhiteSpace(rootFilename))
            {
                throw new ArgumentNullException(nameof(rootFilename));
            }
            if (fileStore == null)
            {
                throw new ArgumentNullException(nameof(fileStore));
            }
            if (!fileStore.Contains(rootFilename))
            {
                throw new System.IO.FileNotFoundException(string.Format("File not found. Filename={0}", rootFilename), rootFilename);
            }
            //
            var results       = default(T);
            var fileStoreItem = fileStore.Get(rootFilename);
            var extractedFile = "";

            if (fileStoreItem != null)
            {
                try
                {
                    extractedFile = fileStore.Extract(System.IO.Path.GetTempPath(), fileStoreItem, true);
                    results       = Load <T>(extractedFile);
                }
                finally
                {
                    // delete temp file
                    if (System.IO.File.Exists(extractedFile))
                    {
                        System.IO.File.Delete(extractedFile);
                    }
                }
            }
            return(results);
        }