Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
            }
        }
        /// <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);
        }