Example #1
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);
        }
Example #2
0
        /// <summary>
        /// Will create a new package.
        /// </summary>
        /// <param name="packageRootFilename">The package filename.</param>
        /// <param name="configurationFilename">The filename to use when writing the <paramref name="packageConfiguration"/> to the package.</param>
        /// <param name="packageConfiguration">The configuration for this package.</param>
        /// <param name="overwrite"><b>True</b> to write-over the package file if it already exists, otherwise, <b>false</b>.</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 Create(string packageRootFilename,
                                        string configurationFilename,
                                        IPackageConfiguration packageConfiguration,
                                        bool overwrite)
        {
            var tempConfigFilename = "";

            try
            {
                // test for existing package
                if (_AllPackagesFileStore.Contains(packageRootFilename) && !overwrite)
                {
                    throw new Exception($"File already exists. Filename={packageRootFilename}");
                }
                // create package file store at temp location
                var backendStorageZipFilename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "packStore_" + Guid.NewGuid().ToString());
                var packageFileStore          = PackageFileStoreProvider.Create(backendStorageZipFilename);
                // save configuration to package file store
                tempConfigFilename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "configuration_" + Guid.NewGuid().ToString());
                System.IO.File.WriteAllText(tempConfigFilename, _ConfigurationSerializer.Serialize(packageConfiguration.Configuration).ToString());
                packageFileStore.Add(configurationFilename, tempConfigFilename, DateTime.Now, new System.IO.FileInfo(tempConfigFilename).Length);
                // must save now to store the configuration file before it is deleted
                packageFileStore.Save(false);
                // adds package file store to file store
                _AllPackagesFileStore.Add(packageRootFilename, backendStorageZipFilename, DateTime.Now, new System.IO.FileInfo(tempConfigFilename).Length);
                // create IPackage
                return(new ConnectedPackage(_AllPackagesFileStore, packageRootFilename, backendStorageZipFilename, packageFileStore, packageConfiguration));
            }
            finally
            {
                // delete temp configuration file
                FileStorage.FileStorageHelper.DeleteFile(tempConfigFilename);
            }
        }