1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Issue #98 - Fixed vaults not loading paste content or syntax highlighting.

This commit is contained in:
Uncled1023 2019-01-23 00:45:04 -08:00
parent 81d2a28ca2
commit 09e2249604
3 changed files with 41 additions and 11 deletions

View File

@ -5,7 +5,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; using System.Web;
using Teknik.Areas.Paste; using Teknik.Areas.Paste;
using Teknik.Areas.Users.Models; using Teknik.Areas.Users.Models;
@ -20,6 +23,7 @@ using Teknik.Filters;
using Teknik.Logging; using Teknik.Logging;
using Teknik.Models; using Teknik.Models;
using Teknik.Utilities; using Teknik.Utilities;
using Teknik.Utilities.Cryptography;
namespace Teknik.Areas.Vault.Controllers namespace Teknik.Areas.Vault.Controllers
{ {
@ -30,7 +34,7 @@ namespace Teknik.Areas.Vault.Controllers
public VaultController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { } public VaultController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
[AllowAnonymous] [AllowAnonymous]
public IActionResult ViewVault(string id) public async Task<IActionResult> ViewVault(string id)
{ {
Models.Vault foundVault = _dbContext.Vaults.Where(v => v.Url == id).FirstOrDefault(); Models.Vault foundVault = _dbContext.Vaults.Where(v => v.Url == id).FirstOrDefault();
if (foundVault != null) if (foundVault != null)
@ -94,7 +98,28 @@ namespace Teknik.Areas.Vault.Controllers
pasteModel.Title = item.Title; pasteModel.Title = item.Title;
pasteModel.Description = item.Description; pasteModel.Description = item.Description;
pasteModel.DateAdded = item.DateAdded; pasteModel.DateAdded = item.DateAdded;
pasteModel.Paste = paste.Paste;
pasteModel.PasteId = paste.Paste.PasteId;
pasteModel.Url = paste.Paste.Url;
pasteModel.DatePosted = paste.Paste.DatePosted;
pasteModel.Syntax = paste.Paste.Syntax;
pasteModel.HasPassword = !string.IsNullOrEmpty(paste.Paste.HashedPassword);
if (!pasteModel.HasPassword)
{
// Read in the file
string subDir = paste.Paste.FileName[0].ToString();
string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, paste.Paste.FileName);
byte[] ivBytes = Encoding.Unicode.GetBytes(paste.Paste.IV);
byte[] keyBytes = AesCounterManaged.CreateKey(paste.Paste.Key, ivBytes, paste.Paste.KeySize);
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (AesCounterStream cs = new AesCounterStream(fs, false, keyBytes, ivBytes))
using (StreamReader sr = new StreamReader(cs, Encoding.Unicode))
{
pasteModel.Content = await sr.ReadToEndAsync();
}
}
model.Items.Add(pasteModel); model.Items.Add(pasteModel);
} }
} }

View File

@ -8,6 +8,11 @@ namespace Teknik.Areas.Vault.ViewModels
{ {
public class PasteItemViewModel : VaultItemViewModel public class PasteItemViewModel : VaultItemViewModel
{ {
public Paste.Models.Paste Paste { get; set; } public int PasteId { get; set; }
public string Url { get; set; }
public string Content { get; set; }
public string Syntax { get; set; }
public DateTime DatePosted { get; set; }
public bool HasPassword { get; set; }
} }
} }

View File

@ -1,7 +1,7 @@
@model Teknik.Areas.Vault.ViewModels.PasteItemViewModel @model Teknik.Areas.Vault.ViewModels.PasteItemViewModel
@{ @{
string format = Model.Paste.Syntax; string format = Model.Syntax;
string formatName = "Text"; string formatName = "Text";
if (!string.IsNullOrEmpty(format) && HighlightHelper.Languages.ContainsKey(format)) if (!string.IsNullOrEmpty(format) && HighlightHelper.Languages.ContainsKey(format))
@ -13,10 +13,10 @@
<div class="panel-heading"> <div class="panel-heading">
<div class="row"> <div class="row">
<div class="col-sm-10 text-left"> <div class="col-sm-10 text-left">
<h4><b>@((string.IsNullOrEmpty(Model.Title)) ? "Paste" : Model.Title)</b> <small>Pasted on <time datetime="@Model.Paste.DatePosted.ToString("s")">@Model.Paste.DatePosted.ToString("dddd, MMMM d, yyyy") at @Model.Paste.DatePosted.ToString("h:mm:ss tt")</time> - Format: <b id="syntaxLanguage_@(Model.Paste.PasteId)">@formatName</b></small></h4> <h4><b>@((string.IsNullOrEmpty(Model.Title)) ? "Paste" : Model.Title)</b> <small>Pasted on <time datetime="@Model.DatePosted.ToString("s")">@Model.DatePosted.ToString("dddd, MMMM d, yyyy") at @Model.DatePosted.ToString("h:mm:ss tt")</time> - Format: <b id="syntaxLanguage_@(Model.PasteId)">@formatName</b></small></h4>
</div> </div>
<div class="col-sm-2 text-right"> <div class="col-sm-2 text-right">
<h4><small><a href="@Url.SubRouteUrl("p", "Paste.View", new { url = Model.Paste.Url })" target="_blank">Direct Link</a></small></h4> <h4><small><a href="@Url.SubRouteUrl("p", "Paste.View", new { url = Model.Url })" target="_blank">Direct Link</a></small></h4>
</div> </div>
</div> </div>
</div> </div>
@ -24,22 +24,22 @@
<div class="row"> <div class="row">
<div class="col-sm-12 pull-left"> <div class="col-sm-12 pull-left">
<div class="btn-group" role="group"> <div class="btn-group" role="group">
<a role="button" class="btn btn-default" href="@Url.SubRouteUrl("p", "Paste.Simple", new { url = Model.Paste.Url })" target="_blank">Simple</a> <a role="button" class="btn btn-default" href="@Url.SubRouteUrl("p", "Paste.Simple", new { url = Model.Url })" target="_blank">Simple</a>
<a role="button" class="btn btn-default" href="@Url.SubRouteUrl("p", "Paste.Raw", new { url = Model.Paste.Url })" target="_blank">Raw</a> <a role="button" class="btn btn-default" href="@Url.SubRouteUrl("p", "Paste.Raw", new { url = Model.Url })" target="_blank">Raw</a>
<a role="button" class="btn btn-default" href="@Url.SubRouteUrl("p", "Paste.Download", new { url = Model.Paste.Url })">Download</a> <a role="button" class="btn btn-default" href="@Url.SubRouteUrl("p", "Paste.Download", new { url = Model.Url })">Download</a>
</div> </div>
</div> </div>
</div> </div>
<br /> <br />
<div class="row paste-item"> <div class="row paste-item">
<div class="col-sm-12 paste-content hideContent" id="@Model.VaultItemId"> <div class="col-sm-12 paste-content hideContent" id="@Model.VaultItemId">
@if (!string.IsNullOrEmpty(Model.Paste.HashedPassword)) @if (Model.HasPassword)
{ {
<p class="text-center">Password Required</p> <p class="text-center">Password Required</p>
} }
else else
{ {
<pre class="line-numbers" id="code-@(Model.VaultItemId)"><code class="language-@(format)">@Model.Paste.Content</code></pre> <pre class="line-numbers" id="code-@(Model.VaultItemId)"><code class="language-@(format)">@Model.Content</code></pre>
} }
</div> </div>
<div class="show-more" id="show-more-bottom-@Model.VaultItemId"> <div class="show-more" id="show-more-bottom-@Model.VaultItemId">