Ejemplo n.º 1
0
        internal static KeyValuePair <string, IExportFormat> GetExporter(string TypeOfFile)
        {
            IExportFormat Output;
            string        BasePath = Export.FullExportFolder;

            if (!Directory.Exists(BasePath))
            {
                Directory.CreateDirectory(BasePath);
            }

            BasePath += Path.DirectorySeparatorChar;

            string FullFileName = BasePath + DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss");

            switch (TypeOfFile)
            {
            case "XML":
                FullFileName = GetUniqueFileName(FullFileName, ".xml");
                FileStream        fs       = new FileStream(FullFileName, FileMode.Create, FileAccess.Write);
                DateTime          Created  = File.GetCreationTime(FullFileName);
                XmlWriterSettings Settings = XML.WriterSettings(true, false);
                Settings.Async = true;
                XmlWriter XmlOutput = XmlWriter.Create(fs, Settings);
                string    FileName  = FullFileName.Substring(BasePath.Length);
                Output = new XmlExportFormat(FileName, Created, XmlOutput, fs);
                break;

            case "Binary":
                FullFileName = GetUniqueFileName(FullFileName, ".bin");
                fs           = new FileStream(FullFileName, FileMode.Create, FileAccess.Write);
                Created      = File.GetCreationTime(FullFileName);
                FileName     = FullFileName.Substring(BasePath.Length);
                Output       = new BinaryExportFormat(FileName, Created, fs, fs);
                break;

            case "Compressed":
                FullFileName = GetUniqueFileName(FullFileName, ".gz");
                fs           = new FileStream(FullFileName, FileMode.Create, FileAccess.Write);
                Created      = File.GetCreationTime(FullFileName);
                FileName     = FullFileName.Substring(BasePath.Length);
                GZipStream gz = new GZipStream(fs, CompressionLevel.Optimal, false);
                Output = new BinaryExportFormat(FileName, Created, gz, fs);
                break;

            case "Encrypted":
                FullFileName = GetUniqueFileName(FullFileName, ".bak");
                fs           = new FileStream(FullFileName, FileMode.Create, FileAccess.Write);
                Created      = File.GetCreationTime(FullFileName);
                FileName     = FullFileName.Substring(BasePath.Length);

                byte[] Key = new byte[32];
                byte[] IV  = new byte[16];

                lock (rnd)
                {
                    rnd.GetBytes(Key);
                    rnd.GetBytes(IV);
                }

                ICryptoTransform AesTransform = aes.CreateEncryptor(Key, IV);
                CryptoStream     cs           = new CryptoStream(fs, AesTransform, CryptoStreamMode.Write);

                gz     = new GZipStream(cs, CompressionLevel.Optimal, false);
                Output = new BinaryExportFormat(FileName, Created, gz, fs, cs, 32);

                string BasePath2 = Export.FullKeyExportFolder;

                if (!Directory.Exists(BasePath2))
                {
                    Directory.CreateDirectory(BasePath2);
                }

                BasePath2 += Path.DirectorySeparatorChar;
                string FullFileName2 = BasePath2 + FullFileName.Substring(BasePath.Length).Replace(".bak", ".key");

                using (XmlOutput = XmlWriter.Create(FullFileName2, XML.WriterSettings(true, false)))
                {
                    XmlOutput.WriteStartDocument();
                    XmlOutput.WriteStartElement("KeyAes256", Export.ExportNamepace);
                    XmlOutput.WriteAttributeString("key", System.Convert.ToBase64String(Key));
                    XmlOutput.WriteAttributeString("iv", System.Convert.ToBase64String(IV));
                    XmlOutput.WriteEndElement();
                    XmlOutput.WriteEndDocument();
                }

                long Size;

                try
                {
                    using (fs = File.OpenRead(FullFileName2))
                    {
                        Size = fs.Length;
                    }
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                    Size = 0;
                }

                Created = File.GetCreationTime(FullFileName2);

                ExportFormat.UpdateClientsFileUpdated(FullFileName2.Substring(BasePath2.Length), Size, Created);
                break;

            default:
                throw new NotSupportedException("Unsupported file type.");
            }

            if (FullFileName.StartsWith(BasePath))
            {
                FullFileName = FullFileName.Substring(BasePath.Length);
            }

            return(new KeyValuePair <string, IExportFormat>(FullFileName, Output));
        }