Skip to content

Commit

Permalink
Merge pull request #395 from nvisionative/develop
Browse files Browse the repository at this point in the history
Merged `develop` into `main` for `2.4.0` release
  • Loading branch information
david-poindexter authored Jan 6, 2025
2 parents 40ac358 + 25865ab commit ad88489
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 347 deletions.
18 changes: 12 additions & 6 deletions nvQuickSite/Controllers/DatabaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ namespace nvQuickSite.Controllers
{
using System;
using System.Data;
using System.Data.SqlClient;

using Microsoft.Data.SqlClient;
using nvQuickSite.Controllers.Exceptions;
using Serilog;

Expand Down Expand Up @@ -77,6 +77,7 @@ public void DropDatabase()
{
Log.Logger.Information("Dropping database {dbName}", this.dbName);
string myDBServerName = this.dbServerName;
string connectionStringTrustServerCertificate = "TrustServerCertificate=True;";
string connectionStringAuthSection = string.Empty;
if (this.usesWindowsAuthentication)
{
Expand All @@ -87,11 +88,14 @@ public void DropDatabase()
connectionStringAuthSection = "User ID=" + this.dbUserName + ";Password=" + this.dbPassword + ";";
}

using (SqlConnection myConn = new SqlConnection("Server=" + myDBServerName + "; Initial Catalog=master;" + connectionStringAuthSection))
using (SqlConnection myConn = new SqlConnection($"Server={myDBServerName}; Initial Catalog=master; {connectionStringTrustServerCertificate} {connectionStringAuthSection}"))
{
string useMaster = @"USE master";
string dropDatabase = $@"IF EXISTS(SELECT name FROM sys.databases WHERE name = '{this.dbName}') " +
$"DROP DATABASE [{this.dbName}]";
$"BEGIN " +
$"ALTER DATABASE [{this.dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; " +
$"DROP DATABASE [{this.dbName}]; " +
$"END";

SqlCommand useMasterCommand = new SqlCommand(useMaster, myConn);
SqlCommand dropDatabaseCommand = new SqlCommand(dropDatabase, myConn);
Expand Down Expand Up @@ -128,6 +132,7 @@ public void DropDatabase()
public void CreateDatabase()
{
Log.Logger.Information("Creating database {dbName}", this.dbName);
string connectionStringTrustServerCertificate = "TrustServerCertificate=True;";
string connectionStringAuthSection = string.Empty;
string connectionTimeout = "Connection Timeout=5;";
if (this.usesWindowsAuthentication)
Expand All @@ -139,7 +144,7 @@ public void CreateDatabase()
connectionStringAuthSection = $"User ID={this.dbUserName};Password={this.dbPassword};";
}

using (SqlConnection myConn = new SqlConnection($"Server={this.dbServerName}; Initial Catalog=master;{connectionStringAuthSection}{connectionTimeout}"))
using (SqlConnection myConn = new SqlConnection($"Server={this.dbServerName}; Initial Catalog=master; {connectionStringTrustServerCertificate} {connectionStringAuthSection} {connectionTimeout}"))
{
string str = $"CREATE DATABASE [{this.dbName}] ON PRIMARY " +
$"(NAME = [{this.dbName}_Data], " +
Expand Down Expand Up @@ -183,6 +188,7 @@ public void CreateDatabase()
internal void SetDatabasePermissions()
{
Log.Logger.Information("Setting permissions on database {dbName}", this.dbName);
string connectionStringTrustServerCertificate = "TrustServerCertificate=True;";
string connectionStringAuthSection = string.Empty;
if (this.usesWindowsAuthentication)
{
Expand All @@ -193,7 +199,7 @@ internal void SetDatabasePermissions()
connectionStringAuthSection = $"User ID={this.dbUserName};Password={this.dbPassword};";
}

using (SqlConnection myConn = new SqlConnection($"Server={this.dbServerName}; Initial Catalog=master;{connectionStringAuthSection}"))
using (SqlConnection myConn = new SqlConnection($"Server={this.dbServerName}; Initial Catalog=master; {connectionStringTrustServerCertificate} {connectionStringAuthSection}"))
{
var appPoolNameFull = @"IIS APPPOOL\DefaultAppPool";
var appPoolName = "DefaultAppPool";
Expand All @@ -208,7 +214,7 @@ internal void SetDatabasePermissions()
SqlCommand grantLogin = new SqlCommand($"sp_grantlogin '{appPoolNameFull}'", myConn);
SqlCommand useDb = new SqlCommand($"USE [{this.dbName}]", myConn);
SqlCommand grantDbAccess = new SqlCommand($"sp_grantdbaccess '{appPoolNameFull}', '{appPoolName}'", myConn);
SqlCommand addRoleMember = new SqlCommand($"sp_addrolemember 'db_owner', '{appPoolName}'", myConn);
SqlCommand addRoleMember = new SqlCommand($"ALTER ROLE [db_owner] ADD MEMBER [{appPoolName}] ", myConn);

try
{
Expand Down
5 changes: 3 additions & 2 deletions nvQuickSite/Controllers/DialogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ internal enum DialogButtons
/// <param name="icon">Icon for the bitmap image (e.g., SystemIcons.Error, SystemIcons.Warning, SystemIcons.Information).</param>
/// <param name="dialogButtons">Type of buttons to use for the custom message box.</param>
/// <param name="doNotWarnAgain">User preference for not warning again with a dialog (i.e., no longer show the dialog).</param>
/// <param name="exception">A value indicating whether the dialog is for an exception.</param>
/// <returns>DialogResult.</returns>
internal static DialogResult ShowMessage(string title, string message, Icon icon, DialogButtons dialogButtons, bool doNotWarnAgain = false)
internal static DialogResult ShowMessage(string title, string message, Icon icon, DialogButtons dialogButtons, bool exception = false, bool doNotWarnAgain = false)
{
var dialogTitle = title;
var dialogMessage = message;
Expand All @@ -71,7 +72,7 @@ internal static DialogResult ShowMessage(string title, string message, Icon icon
switch (dialogButtons)
{
case DialogButtons.OK:
using (var messageBox = new MsgBoxOk(dialogTitle, dialogMessage, dialogIcon))
using (var messageBox = new MsgBoxOk(dialogTitle, dialogMessage, dialogIcon, exception))
{
result = messageBox.ShowDialog();
}
Expand Down
111 changes: 111 additions & 0 deletions nvQuickSite/Controllers/PackageExtractorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) 2016-2020 nvisionative, Inc.
//
// This file is part of nvQuickSite.
//
// nvQuickSite is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// nvQuickSite is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with nvQuickSite. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;

using nvQuickSite.Exceptions;
using Serilog;

/// <summary>
/// Controller for extracting a package.
/// </summary>
public class PackageExtractorController
{
/// <summary>
/// Action to be executed when the progress is updated.
/// </summary>
public event Action<string> ProgressUpdated;

/// <summary>
/// Action to be executed when the progress value is updated.
/// </summary>
public event Action<int> ProgressValueUpdated;

/// <summary>
/// Extracts a package.
/// </summary>
/// <param name="openPath">Open Path.</param>
/// <param name="savePath">Save Path.</param>
/// <exception cref="ReadAndExtractException">Read and Extract Exception.</exception>
public void ExtractPackage(string openPath, string savePath)
{
try
{
Log.Logger.Information("Extracting package");

using (var zip = ZipFile.OpenRead(openPath))
{
long totalSize = zip.Entries.Sum(entry => entry.Length);

long progressSize = 0;

foreach (var entry in zip.Entries)
{
var entryPath = Path.Combine(savePath, entry.FullName);

if (string.IsNullOrEmpty(entry.Name)) // Directory
{
Directory.CreateDirectory(entryPath);
}
else // File
{
var directoryPath = Path.GetDirectoryName(entryPath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

this.ExtractEntryWithProgress(entry, entryPath, ref progressSize, totalSize);
}
}
}

this.ProgressUpdated?.Invoke("Congratulations! Your new site is now ready to visit!");
}
catch (Exception ex)
{
var message = "Error attempting to read and extract the package";
Log.Error(ex, message);
throw new ReadAndExtractException(message, ex) { Source = "Read And Extract Package" };
}

Log.Logger.Information("Extracted package from {openPath} to {savePath}", openPath, savePath);
}

private void ExtractEntryWithProgress(ZipArchiveEntry entry, string destinationPath, ref long progressSize, long totalSize)
{
this.ProgressUpdated?.Invoke("Copying: " + entry.FullName);

using (var inputStream = entry.Open())
using (var outputStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
{
var buffer = new byte[81920];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, bytesRead);
progressSize += bytesRead;

int progressPercentage = (int)((progressSize * 100) / totalSize);
this.ProgressValueUpdated?.Invoke(progressPercentage);
}
}
}
}
56 changes: 47 additions & 9 deletions nvQuickSite/Controls/Dialogs/MsgBoxOk.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion nvQuickSite/Controls/Dialogs/MsgBoxOk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace nvQuickSite.Controls.Dialogs
{
using System;
using System.Drawing;

using MetroFramework.Forms;
Expand All @@ -32,12 +33,23 @@ public partial class MsgBoxOk : MetroForm
/// <param name="dialogTitle">The title to show in the dialog.</param>
/// <param name="dialogMessage">The message to show in the dialog.</param>
/// <param name="dialogIconImage">The image to use as the dialog icon.</param>
public MsgBoxOk(string dialogTitle, string dialogMessage, Image dialogIconImage)
/// <param name="exception">Whether or not the dialog is an exception.</param>
public MsgBoxOk(string dialogTitle, string dialogMessage, Image dialogIconImage, bool exception = false)
{
this.InitializeComponent();
this.lblTitle.Text = dialogTitle;
this.lblMessage.Text = dialogMessage;
this.dialogIcon.Image = dialogIconImage;
this.btnGetHelp.Visible = exception;
this.btnGetHelp.Click += (sender, e) =>
{
this.linkLabelReportIssue.Visible = true;
System.Diagnostics.Process.Start($"https://github.com/nvisionative/nvQuickSite/issues?q=is%3Aissue+{dialogTitle}+{dialogMessage}");
};
this.linkLabelReportIssue.Click += (sender, e) =>
{
System.Diagnostics.Process.Start($"https://github.com/nvisionative/nvQuickSite/issues/new?title=[In-App%20Issue]{dialogTitle}&body={dialogMessage}");
};
}
}
}
Loading

0 comments on commit ad88489

Please sign in to comment.