Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #125

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Dev #125

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Synology.TestWebApplication/.bowerrc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
namespace Synology.TestWebApplication.Models
namespace Synology.TestWebApplication.Classes
{
public class SettingsViewModel
public class SettingsData
{
public string SynologyHost { get; set; }
public int SynologyPort { get; set; }
Expand Down
103 changes: 0 additions & 103 deletions Synology.TestWebApplication/Controllers/HomeController.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Synology.TestWebApplication/Models/ErrorViewModel.cs

This file was deleted.

26 changes: 26 additions & 0 deletions Synology.TestWebApplication/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model ErrorModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
23 changes: 23 additions & 0 deletions Synology.TestWebApplication/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Synology.TestWebApplication.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public class ErrorModel : PageModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@{
ViewData["Title"] = "Home Page";
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}

<div class="container">
<div class="card-deck mb-3 text-center">
<div class="card mb-4 box-shadow">
Expand Down
39 changes: 39 additions & 0 deletions Synology.TestWebApplication/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Synology.TestWebApplication.Classes;
using IOFile = System.IO.File;

namespace Synology.TestWebApplication.Pages
{
public class IndexModel : PageModel
{
public string SynologyHost { get; set; }
public int SynologyPort { get; set; }
public string SynologyUser { get; set; }
public string SynologyPass { get; set; }
public bool UseSsl { get; set; }

public IActionResult OnGet()
{
if (!IOFile.Exists("synosettings.json"))
{
return RedirectToPage("Settings");
}

var settings = JsonConvert.DeserializeObject<SettingsData>(IOFile.ReadAllText("synosettings.json"));

SynologyHost = settings.SynologyHost;
SynologyPort = settings.SynologyPort;
SynologyUser = settings.SynologyUser;
SynologyPass = settings.SynologyPass;
UseSsl = settings.UseSsl;

return Page();
}
}
}
8 changes: 8 additions & 0 deletions Synology.TestWebApplication/Pages/Privacy.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page
@model PrivacyModel
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>
16 changes: 16 additions & 0 deletions Synology.TestWebApplication/Pages/Privacy.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Synology.TestWebApplication.Pages
{
public class PrivacyModel : PageModel
{
public void OnGet()
{
}
}
}
29 changes: 29 additions & 0 deletions Synology.TestWebApplication/Pages/Settings.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@page
@model SettingsModel
@{
}
<div asp-validation-summary="All" class="text-danger">
</div>
<form asp-page="Settings" method="post">
<div class="form-group">
<label asp-for="SynologyHost">Hostname</label>
<input type="text" class="form-control" asp-for="SynologyHost" placeholder="Hostname">
</div>
<div class="form-group">
<label asp-for="SynologyPort">Port</label>
<input type="number" min="1" max="65535" step="1" class="form-control" asp-for="SynologyPort" placeholder="Port">
</div>
<div class="form-group">
<label asp-for="SynologyUser">Username</label>
<input type="text" class="form-control" asp-for="SynologyUser" placeholder="Username">
</div>
<div class="form-group">
<label asp-for="SynologyPass">Password</label>
<input type="password" class="form-control" asp-for="SynologyPass" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="UseSsl">
<label class="form-check-label" asp-for="UseSsl">Use SSL</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
92 changes: 92 additions & 0 deletions Synology.TestWebApplication/Pages/Settings.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Synology.Interfaces;
using Synology.TestWebApplication.Classes;
using IOFile = System.IO.File;

namespace Synology.TestWebApplication.Pages
{
public class SettingsModel : PageModel
{
private readonly IServiceProvider _serviceProvider;

public SettingsModel(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

[Required]
[BindProperty]
public string SynologyHost { get; set; }
[Required]
[BindProperty]
public int SynologyPort { get; set; }
[Required]
[BindProperty]
public string SynologyUser { get; set; }
[Required]
[BindProperty]
public string SynologyPass { get; set; }
[BindProperty]
public bool UseSsl { get; set; }

public void OnGet()
{
SettingsData settings = null;

if (IOFile.Exists("synosettings.json"))
{
settings = JsonConvert.DeserializeObject<SettingsData>(IOFile.ReadAllText("synosettings.json"));

SynologyHost = settings.SynologyHost;
SynologyPort = settings.SynologyPort;
SynologyUser = settings.SynologyUser;
SynologyPass = settings.SynologyPass;
UseSsl = settings.UseSsl;
}
}

public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
var settings = _serviceProvider.GetService<ISynologyConnectionSettings>();

settings.BaseHost = SynologyHost;
settings.Password = SynologyPass;
settings.Port = SynologyPort;
settings.Ssl = UseSsl;
settings.SslPort = SynologyPort;
settings.Username = SynologyUser;

using (var syno = _serviceProvider.GetService<ISynologyConnection>())
{
var result = await syno.Api().Auth().LoginAsync();

if (!result.Success && result.Error.Code != 403)
{
ModelState.AddModelError("", "Invalid connection settings.");
}
else
{
var json = JsonConvert.SerializeObject(settings, Formatting.Indented);

IOFile.WriteAllText("synosettings.json", json);

return RedirectToPage("Index");
}
}
}

return Page();
}
}
}
Loading