Exemplo n.º 1
0
 /// <summary>
 /// Execute commands from a file with SQL script against the context database
 /// </summary>
 /// <param name="fileProvider">File provider</param>
 /// <param name="filePath">Path to the file</param>
 protected void ExecuteSqlScriptFromFile(IMotelFileProvider fileProvider, string filePath)
 {
     filePath = fileProvider.MapPath(filePath);
     if (!fileProvider.FileExists(filePath))
     {
         return;
     }
     ExecuteSqlScript(fileProvider.ReadAllText(filePath, Encoding.Default));
 }
Exemplo n.º 2
0
 /// <summary>
 /// Get a value indicating whether some file (thumb) already exists
 /// </summary>
 /// <param name="thumbFilePath">Thumb file path</param>
 /// <param name="thumbFileName">Thumb file name</param>
 /// <returns>Result</returns>
 protected virtual bool GeneratedThumbExists(string thumbFilePath, string thumbFileName)
 {
     return(_fileProvider.FileExists(thumbFilePath));
 }
Exemplo n.º 3
0
        public static DataSettings LoadSettings(string filePath = null, bool reloadSettings = false, IMotelFileProvider fileProvider = null)
        {
            if (!reloadSettings && Singleton <DataSettings> .Instance != null)
            {
                return(Singleton <DataSettings> .Instance);
            }
            fileProvider ??= CommonHelper.DefaultFileProvider;
            filePath ??= fileProvider.MapPath(MotelDataSettingsDefaults.FilePath);
            if (!fileProvider.FileExists(filePath))
            {
                filePath = fileProvider.MapPath(MotelDataSettingsDefaults.ObsoleteFilePath);
                if (!fileProvider.FileExists(filePath))
                {
                    return(new DataSettings());
                }
                var dataSettings = new DataSettings();
                using (var reader = new StringReader(fileProvider.ReadAllText(filePath, Encoding.UTF8)))
                {
                    string settingsLine;
                    while ((settingsLine = reader.ReadLine()) != null)
                    {
                        var separatorIndex = settingsLine.IndexOf(':');
                        if (separatorIndex == -1)
                        {
                            continue;
                        }

                        var key   = settingsLine.Substring(0, separatorIndex).Trim();
                        var value = settingsLine.Substring(separatorIndex + 1).Trim();

                        switch (key)
                        {
                        case "DataProvider":
                            dataSettings.DataProvider = Enum.TryParse(value, true, out DataProviderType providerType) ? providerType : DataProviderType.Unknown;
                            continue;

                        case "DataConnectionString":
                            dataSettings.ConnectionString = value;
                            continue;

                        case "SQLCommandTimeout":
                            //If parsing isn't successful, we set a negative timeout, that means the current provider will usе a default value
                            dataSettings.SQLCommandTimeout = int.TryParse(value, out var timeout) ? timeout : -1;
                            continue;

                        default:
                            dataSettings.RawDataSettings.Add(key, value);
                            continue;
                        }
                    }
                }

                SaveSettings(dataSettings, fileProvider);

                //and delete the old one
                fileProvider.DeleteFile(filePath);

                Singleton <DataSettings> .Instance = dataSettings;
                return(Singleton <DataSettings> .Instance);
            }
            var text = fileProvider.ReadAllText(filePath, Encoding.UTF8);

            if (string.IsNullOrEmpty(text))
            {
                return(new DataSettings());
            }

            //get data settings from the JSON file
            Singleton <DataSettings> .Instance = JsonConvert.DeserializeObject <DataSettings>(text);

            return(Singleton <DataSettings> .Instance);
        }