Exemplo n.º 1
0
        private IPersistenceConfigurer CreatePersistenceConfigurer(string connectionString)
        {
            IPersistenceConfigurer configurer;

            SetCustomSqlTypes = true;

            if (connectionString.Contains("##CurrentDirectory##") || connectionString.Contains(":memory:"))
            {
                var currentDirectory = QBicUtils.GetCurrentDirectory();
                connectionString = connectionString.Replace("##CurrentDirectory##", currentDirectory); // for my sqlite connectiontion string

                configurer = SQLiteConfiguration.Standard.ConnectionString(connectionString).IsolationLevel(IsolationLevel.ReadCommitted);
                DataStore.SetCustomSqlTypes = false;
            }
            //else if (providerName.Contains("MySql"))
            //{
            //    configurer = MySQLConfiguration.Standard.ConnectionString(connectionString).IsolationLevel(IsolationLevel.ReadCommitted);
            //}
            else
            {
                configurer = MsSqlConfiguration.MsSql2012.ConnectionString(connectionString).IsolationLevel(IsolationLevel.ReadCommitted);
            }

            return(configurer);
        }
Exemplo n.º 2
0
        public async override Task <FileActionResult> ProcessEvent(int eventId)
        {
            var data = GetRequestData();

            if (String.IsNullOrWhiteSpace(data))
            {
                data = HttpContext.Current.Request.Params["requestData"];
            }

            if (!String.IsNullOrWhiteSpace(data))
            {
                data = QBicUtils.Base64Decode(data);
            }

            var eventItem = Container.Resolve <EventService>().GetEventItem(eventId) as OpenFile;

            if (eventItem == null)
            {
                throw new Exception("No OpenFile action has been found for event number: " + eventId);
            }

            //var eventItem = EventList[eventId] as OpenFile;
            //var eventItemType = EventList[eventId];
            //var eventItem = Container.Resolve(eventItemType) as OpenFile;


            //var __ignore__ = eventItem.FileName; /* Leave this here -> this initializes the filename */
            var fileInfo = await eventItem.GetFileInfo(data);

            //return fileInfo;
            return(new FileActionResult(fileInfo));
        }
Exemplo n.º 3
0
        private byte[] CreateCsvFile(string data)
        {
            using (var session = DataService.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    ProcessCsvData(data, session);

                    transaction.Commit();
                }

            var result = QBicUtils.GetBytes(ReportData.ToString());

            return(result);
        }
Exemplo n.º 4
0
        private byte[] CreateCsvFile(string info)
        {
            var data = new StringBuilder();

            data.AppendLine("TEST FILE DOWNLOAD");
            data.AppendFormat("User{0}News Read{0}New Releases Read", DELIMITER);
            data.AppendLine();
            data.Append(info);
            data.AppendLine();

            var result = QBicUtils.GetBytes(data.ToString());

            return(result);
        }
Exemplo n.º 5
0
        public IHttpActionResult Test(string path)
        {
            //TODO: Not sure if I even need this. Maybe the other request can set the file in the correct path.
            //      No! Do it this way, can clear challenge path and response after request is made

            var request   = System.Web.HttpContext.Current.Request.Url.ToString();
            var challenge = request.Split("/".ToCharArray()).Last();

            Logger.Info("Received ACME challenge on path: " + path + " and challenge = " + challenge);

            //Todo: Challenge should = path also.
            //      But, I should also validate/verify both the path and the request. Could be a hijacker/MiM

            if (challenge != ChallengePath)
            {
                //return BadRequest(); TODO: Put this in and test.
            }

            var resp = challenge + "." + ChallengeResponse;

            if (ChallengeResponse == "xxx") // Not sure if i need this. Why would this code run like this and not be set??
            {
                var dir           = QBicUtils.GetCurrentDirectory();
                var physicallPath = dir + "\\.well-known\\acme-challenge\\" + challenge;
                if (File.Exists(physicallPath))
                {
                    resp = File.ReadAllText(physicallPath);
                }
                else
                {
                    resp = string.Format("Path {0} does not exist", physicallPath);
                }
            }

            //TODO: Not sure if this is called only once or twice per challenge.
            //      But need to allow below code

            //ChallengePath = String.Empty;
            //ChallengeResponse = String.Empty;

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(resp)
            };

            return(ResponseMessage(response));
        }
Exemplo n.º 6
0
        public override async Task <IList <IEvent> > ProcessAction()
        {
            var url  = "https://localhost/websitetemplate/api/v1/performBackup";
            var json = new JsonHelper();

            json.Add("abc", "xyz");
            var bytes = QBicUtils.GetBytes(json.ToString());

            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
            //using (var client = new WebClient())
            using (var client = new HttpClient())
            {
                //var responseData = client.UploadData(url, bytes);
                //var tmpString = XXXUtils.GetString(responseData);

                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var resp = await client.PostAsJsonAsync(url, bytes);

                var backupTypeString = resp.Headers.GetValues(BackupService.BACKUP_HEADER_KEY).FirstOrDefault();
                //var byteContent = await resp.Content.ReadAsByteArrayAsync();
                //var byteString = XXXUtils.GetString(byteContent);

                var stringContent = await resp.Content.ReadAsStringAsync();

                //stringContent = stringContent.Substring(1, stringContent.Length - 2);
                stringContent = stringContent.Replace("\0", "");

                var data         = Convert.FromBase64String(stringContent);
                var responseData = CompressionHelper.InflateByte(data);

                var itemsString = QBicUtils.GetString(responseData);

                itemsString = "[" + itemsString + "]";
                itemsString = itemsString.Replace("}{", "},{");
                var itemsList = JsonHelper.DeserializeObject <List <BaseClass>[]>(itemsString, true);
                var items     = itemsList.SelectMany(i => i).ToList();
                Console.WriteLine(items.Count);
            }

            return(new List <IEvent>()
            {
                new ShowMessage("Test done")
            });
        }
Exemplo n.º 7
0
 public DbConnection CreateConnection(string connectionString)
 {
     if (connectionString.Contains("##CurrentDirectory##") || connectionString.Contains(":memory:"))
     {
         connectionString = connectionString.Replace("##CurrentDirectory##", QBicUtils.GetCurrentDirectory());
         return(new SQLiteConnection(connectionString));
     }
     else if (ConfigurationManager.ConnectionStrings["MainDataStore"]?.ProviderName.Contains("MySql") == true)
     {
         throw new NotImplementedException("Code for MySql has not been enabled due to many problems with concurrent database access using MySql");
         //return new MySql.Data.MySqlClient.MySqlConnection(connectionString);
     }
     else
     {
         return(new System.Data.SqlClient.SqlConnection(connectionString));
     }
 }
Exemplo n.º 8
0
        public override async Task <IList <IEvent> > ProcessAction(int actionNumber)
        {
            var result = new List <IEvent>();

            if (actionNumber != 0)
            {
                result.Add(new CancelInputDialog());
                return(result);
            }

            var file             = GetValue <WebsiteTemplate.Menus.InputItems.FileInfo>("File");
            var connectionString = ConfigurationManager.ConnectionStrings["MainDataStore"]?.ConnectionString;

            var bytes  = file.Data;
            var base64 = QBicUtils.GetString(bytes);

            base64 = base64.Replace("data:;base64,", "").Replace("\0", "");
            bytes  = Convert.FromBase64String(base64);

            bytes = CompressionHelper.InflateByte(bytes, Ionic.Zlib.CompressionLevel.BestCompression);

            if (connectionString.Contains("##CurrentDirectory##"))
            {
                var currentDirectory = HttpRuntime.AppDomainAppPath;
                var filePath         = currentDirectory + "\\Data\\appData_test.db";
                File.WriteAllBytes(filePath, bytes);

                result.Add(new ShowMessage("Done"));
            }
            else
            {
                var json = QBicUtils.GetString(bytes);

                json = "[" + json + "]";
                json = json.Replace("}{", "},{");

                var itemsList = JsonHelper.DeserializeObject <List <BaseClass>[]>(json, true);
                var items     = itemsList.SelectMany(i => i).ToList();
                Console.WriteLine(items.Count());
            }

            result.Add(new ShowMessage("Fail"));

            return(result);
        }
Exemplo n.º 9
0
        public override async Task <IList <IEvent> > ProcessAction(int actionNumber)
        {
            if (actionNumber == 1)
            {
                return(new List <IEvent>()
                {
                    new CancelInputDialog(),
                });
            }
            else if (actionNumber == 0)
            {
                var file = GetValue <FileInfo>("File");
                if (file == null || file.Data == null)
                {
                    return(new List <IEvent>()
                    {
                        new ShowMessage("File empty or contains no data")
                    });
                }
                var separator   = GetValue("Separator");
                var isQuoted    = GetValue <bool>("IsQuoted");
                var linesToSkip = GetValue <int>("Skip");
                if (linesToSkip < 0)
                {
                    return(new List <IEvent>()
                    {
                        new ShowMessage("Number of lines to skip cannot be less than zero")
                    });
                }
                var mappings = GetValue <List <JToken> >("Mappings") ?? new List <JToken>();

                if (mappings.Count == 0)
                {
                    return(new List <IEvent>()
                    {
                        new ShowMessage("Mappings are mandatory and must be provided, unable to process file.")
                    });
                }

                var columnSettings = new List <ColumnSetting>();
                foreach (JObject mapping in mappings)
                {
                    var field   = mapping.GetValue("Field")?.ToString();
                    var columns = mapping.GetValue("Columns")?.ToString();
                    columnSettings.Add(new ColumnSetting(field, columns));
                }

                var parser = new TextFieldParser(new System.IO.StringReader(QBicUtils.GetString(file.Data)));
                parser.HasFieldsEnclosedInQuotes = isQuoted;
                parser.SetDelimiters(separator);

                var errors     = new List <string>();
                var mappedData = MapData(parser, linesToSkip, columnSettings, errors);
                parser.Close();

                var fileData = ProcessMappingResults(mappedData, errors);

                var tempFile = System.IO.Path.GetTempFileName();
                System.IO.File.WriteAllBytes(tempFile, fileData.Data);

                var jsonData = new
                {
                    filePath  = tempFile,
                    fileName  = fileData.FileName,
                    extension = fileData.FileExtension,
                    mimeType  = fileData.MimeType
                };
                var data = JsonHelper.SerializeObject(jsonData);

                return(new List <IEvent>()
                {
                    new CancelInputDialog(),
                    new ExecuteAction(EventNumber.ShowCsvProcessResult, data),
                });
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 10
0
        public bool RestoreFullBackup(bool removeExistingItemsFirst, byte[] data, string dbConnectionString, params Type[] typesToIgnore)
        {
            if (removeExistingItemsFirst)
            {
                RemoveExistingData(dbConnectionString, typesToIgnore);
            }

            var stopwatch        = new Stopwatch();
            var cnt              = 1;
            var backupName       = "Restore_" + DateTime.Now.ToString("dd_MM_yyyy") + ".db";
            var currentDirectory = QBicUtils.GetCurrentDirectory() + "\\Data\\";

            if (!Directory.Exists(currentDirectory))
            {
                Directory.CreateDirectory(currentDirectory);
            }
            while (File.Exists(currentDirectory + backupName))
            {
                backupName = "Restore_" + DateTime.Now.ToString("dd_MM_yyyy") + "_" + cnt + ".db";
                cnt++;
            }

            try
            {
                File.WriteAllBytes(currentDirectory + backupName, data);

                var connectionString = String.Format(@"Data Source=##CurrentDirectory##\Data\{0};Version=3;Journal Mode=Off;Connection Timeout=12000", backupName);
                var store            = DataStore.GetInstance(false);
                var backupConfig     = store.CreateNewConfigurationUsingConnectionString(connectionString);
                var backupFactory    = backupConfig.BuildSessionFactory();

                DynamicClass.SetIdsToBeAssigned = true;
                var config  = store.CreateNewConfigurationUsingConnectionString(dbConnectionString);
                var factory = config.BuildSessionFactory();

                var ids = SystemTypes.Keys.ToList().OrderBy(i => i);

                var totalItems = 0;

                using (var backupSession = backupFactory.OpenStatelessSession())
                    using (var session = factory.OpenStatelessSession())
                    {
                        foreach (var id in ids)
                        {
                            var type = SystemTypes[id];

                            //if ((type == typeof(Models.SystemSettings) || (type == typeof(SystemSettingValue))) && restoreSystemSettings == false)
                            if (typesToIgnore.Contains(type))
                            {
                                var tmpItems = GetItems(type, backupSession);

                                continue;
                            }

                            var items = GetItems(type, backupSession);

                            var sameTypeProperties = type.GetProperties().Where(p => p.PropertyType == type).ToList();
                            if (sameTypeProperties.Count > 0)
                            {
                                var totalItemsToAdd = items.Where(i => i.GetType() == type).ToList();

                                var addedItems = new List <string>();
                                while (addedItems.Count < totalItemsToAdd.Count)
                                {
                                    foreach (var prop in sameTypeProperties)
                                    {
                                        var itemsToAdd = totalItemsToAdd.Where(i => ShouldAddItem(i, prop, totalItemsToAdd, addedItems) == true).ToList();

                                        totalItems += itemsToAdd.Count();

                                        InsertItems(itemsToAdd, factory, type);
                                        addedItems.AddRange(itemsToAdd.Select(i => i.Id));
                                    }
                                }
                            }
                            else
                            {
                                var itemsToAdd = items.Where(i => i.GetType() == type).ToList();

                                totalItems += itemsToAdd.Count();

                                InsertItems(itemsToAdd, factory, type);
                            }
                        }
                    }

                factory.Close();
                backupFactory.Close();

                using (var session = factory.OpenSession())
                {
                    var count = session
                                .CreateCriteria <BaseClass>()
                                .SetProjection(
                        Projections.Count(Projections.Id())
                        )
                                .List <int>()
                                .Sum();

                    //if (restoreSystemSettings == false)
                    //{
                    //    var settingsCount = session.QueryOver<Models.SystemSettings>().RowCount() +
                    //                        session.QueryOver<SystemSettingValue>().RowCount();
                    //    count -= settingsCount;
                    //}

                    var queryOverMethodInfo = typeof(ISession).GetMethods().FirstOrDefault(m => m.Name == "QueryOver" && m.GetParameters().Count() == 0);
                    foreach (var t in typesToIgnore)
                    {
                        var tCount = GetItemCount(t, session);
                        count -= tCount;
                    }

                    if (count != totalItems)
                    {
                        return(false);
                    }
                }

                stopwatch.Stop();

                Logger.Info("Full restore took " + stopwatch.ElapsedMilliseconds + " ms");

                return(true);
            }
            catch (Exception e)
            {
                SystemLogger.LogError <BackupService>("Error restoring full backup", e);
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                DynamicClass.SetIdsToBeAssigned = false;
                if (File.Exists(currentDirectory + backupName))
                {
                    File.Delete(currentDirectory + backupName);
                }
            }
        }
Exemplo n.º 11
0
        public byte[] CreateFullBackup()
        {
            var cnt        = 1;
            var backupName = "Backup_" + DateTime.Now.ToString("dd_MM_yyyy") + ".db";

            var currentDirectory = QBicUtils.GetCurrentDirectory() + "\\Data\\";

            if (!Directory.Exists(currentDirectory))
            {
                Directory.CreateDirectory(currentDirectory); // try create the directory if it doesn't exist, else it would crash anyway
            }

            while (File.Exists(currentDirectory + backupName))
            {
                backupName = "Backup_" + DateTime.Now.ToString("dd_MM_yyyy") + "_" + cnt + ".db";
                cnt++;
            }

            try
            {
                DynamicClass.SetIdsToBeAssigned = true;

                Logger.Info("About to create backup");

                CreateBackupFile(currentDirectory + backupName);

                var connectionString = String.Format(@"Data Source=##CurrentDirectory##\Data\{0};Version=3;Journal Mode=Off;Connection Timeout=12000", backupName);
                var store            = DataStore.GetInstance(false);
                var config           = store.CreateNewConfigurationUsingConnectionString(connectionString);
                new SchemaUpdate(config).Execute(false, true); // Build the tables etc.
                var factory = config.BuildSessionFactory();

                var ids = SystemTypes.Keys.ToList().OrderBy(i => i);

                var total = 0;

                using (var backupSession = factory.OpenStatelessSession())
                    using (var session = DataService.OpenStatelessSession())
                    {
                        foreach (var id in ids)
                        {
                            var type = SystemTypes[id];
                            Logger.Info("Backing up " + type.ToString());

                            var items = GetItems(type, session);
                            Logger.Info("Got items");

                            var sameTypeProperties = type.GetProperties().Where(p => p.PropertyType == type).ToList();
                            if (sameTypeProperties.Count > 0)
                            {
                                var totalItemsToAdd = items.Where(i => i.GetType() == type).ToList();

                                var addedItems = new List <string>();
                                while (addedItems.Count < totalItemsToAdd.Count)
                                {
                                    foreach (var prop in sameTypeProperties)
                                    {
                                        var itemsToAdd = totalItemsToAdd.Where(i => ShouldAddItem(i, prop, totalItemsToAdd, addedItems) == true).ToList();

                                        total += itemsToAdd.Count();
                                        InsertItems(itemsToAdd, factory, type);
                                        addedItems.AddRange(itemsToAdd.Select(i => i.Id));
                                    }
                                }
                            }
                            else
                            {
                                var itemsToAdd = items.Where(i => i.GetType() == type).ToList();

                                total += itemsToAdd.Count();
                                InsertItems(itemsToAdd, factory, type);
                            }
                            Logger.Info("Items added to backup");
                        }
                    }

                Logger.Info("Closing factory");
                factory.Close();

                using (var session = factory.OpenSession())
                {
                    //var users = session.QueryOver<Models.User>().List().ToList();
                    var count = session
                                .CreateCriteria <BaseClass>()
                                .SetProjection(
                        Projections.Count(Projections.Id())
                        )
                                .List <int>()
                                .Sum();

                    if (count != total)
                    {
                        Logger.Info("Backup did not complete successfully.");
                        throw new Exception("Backup did not complete successfully. Try again or contact support.");
                    }
                }
                Logger.Info("Closing store session");

                return(File.ReadAllBytes(currentDirectory + backupName));
            }
            catch (Exception e)
            {
                SystemLogger.LogError <BackupService>("Error creating full backup", e);
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                DynamicClass.SetIdsToBeAssigned = false; // change it back.

                File.Delete(currentDirectory + backupName);
            }
        }