thumbnails, better css

master
Avril 5 years ago
parent de451c0566
commit d21ce94703
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -16,10 +16,20 @@ namespace ndview
{ {
public napdump.BoardInfo Board { get; } public napdump.BoardInfo Board { get; }
public DirectoryInfo Images { get; } public DirectoryInfo Images { get; }
private ThumbnailGenerator Thumbnailer {get;} = new ThumbnailGenerator(200);
public PageGenerator(napdump.BoardInfo board, DirectoryInfo images) public PageGenerator(napdump.BoardInfo board, DirectoryInfo images)
{ {
Board = board; Board = board;
Images = images; Images = images;
}
private DirectoryInfo ThumbnailDir(DirectoryInfo imageDir)
{
var exp = new DirectoryInfo(Path.Combine(imageDir.FullName, "thumb"));
if(!exp.Exists)
exp.Create();
return exp;
} }
private async Task<string> ExtractImage(PostInfo post, DirectoryInfo imgOutput, bool encrypted, CancellationToken token) private async Task<string> ExtractImage(PostInfo post, DirectoryInfo imgOutput, bool encrypted, CancellationToken token)
@ -31,19 +41,27 @@ namespace ndview
var ifn = post.ImageURL.Split('/').Last(); var ifn = post.ImageURL.Split('/').Last();
await Task.Yield(); await Task.Yield();
var ofn = Path.Join(imgOutput.FullName, ifn); var ofn = Path.Join(imgOutput.FullName, ifn);
var thumbofn = Path.Join(ThumbnailDir(imgOutput).FullName, ifn);
if (encrypted && Program.DeletedKey!=null) if (encrypted && Program.DeletedKey!=null)
{ {
Console.WriteLine($"Extracting encrypted image {post.PostNumber} -> {ofn}"); Console.WriteLine($"Extracting encrypted image {post.PostNumber} -> {ofn}");
using (var inp = new FileStream(imageFn, FileMode.Open, FileAccess.Read)) using (var inp = new FileStream(imageFn, FileMode.Open, FileAccess.Read))
{ {
using (var oup = new FileStream(ofn, FileMode.Create)) await using (var oup = new FileStream(ofn, FileMode.Create))
{ {
using (var dec = new encaes.AesEncryptor(inp) { KeepAlive = true }) using (var dec = new encaes.AesEncryptor(inp) { KeepAlive = true })
{ {
dec.Key = Program.DeletedKey.Value; dec.Key = Program.DeletedKey.Value;
await dec.Decrypt(oup, token); await dec.Decrypt(oup, token);
} }
await oup.FlushAsync();
oup.Position=0;
await using(var thumbo = new FileStream(thumbofn, FileMode.Create)) {
await Thumbnailer.Thumbnail(oup, thumbo, token);
}
} }
} }
} }
else else
@ -54,6 +72,14 @@ namespace ndview
WriteYellowLine("Warning: Image for post "+post.PostNumber+" will be garbage: Cannot be decrypted."); WriteYellowLine("Warning: Image for post "+post.PostNumber+" will be garbage: Cannot be decrypted.");
} }
await CopyFileAsync(imageFn, ofn, token); await CopyFileAsync(imageFn, ofn, token);
await using(var inp = new FileStream(imageFn, FileMode.Open, FileAccess.Read))
{
await using(var oup = new FileStream(thumbofn, FileMode.Create))
{
await Thumbnailer.Thumbnail(inp, oup, token);
}
}
} }
return ifn; return ifn;
} }
@ -141,7 +167,7 @@ namespace ndview
} }
} }
await index.TagSelfClosingAsync("img", token, ("src", $"i/{ifn}"), ("height", "250"), ("width", "250")); await index.TagSelfClosingAsync("img", token, ("src", $"i/thumb/{ifn}"), ("width", Thumbnailer.Width.ToString()));
} }
} }
@ -234,6 +260,8 @@ namespace ndview
await using (await index.TagAsync("section", token, ("id", thread.PostNumber.ToString()), ("class", "thread"))) await using (await index.TagAsync("section", token, ("id", thread.PostNumber.ToString()), ("class", "thread")))
{ {
await index.AppendHtml($@"<a href='#{thread.PostNumber}' class='script expand'></a>");
await WriteHeader(index, thread, token); await WriteHeader(index, thread, token);
if (thread.ImageURL != null) if (thread.ImageURL != null)
await WriteImageFigure(index, thread, token); await WriteImageFigure(index, thread, token);

@ -62,3 +62,46 @@ figcaption > i > a {
margin-left: 5px; margin-left: 5px;
margin-top: 5px; margin-top: 5px;
} }
section.hidden > a.expand::before {
content: "+"!important;
}
section > a.expand::before {
content: "-";
}
a.expand {
display: inline;
float: left;
text-decoration: none;
}
/* make it look not shit */
html {
background-color: #eff3ee;
height: 100%;
}
h1 {
font: bolder 28px Tahoma;
text-align: center;
}
a.expand {
padding-top: 1px;
}
* {
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
}
h3 {
font-size: 1em;
}
a {
text-decoration: none;
}

@ -1,4 +1,10 @@
window.addEventListener('load', () => { window.addEventListener('load', () => {
document.querySelectorAll(".thread").forEach(x=> x.classList.toggle("hidden")); document.querySelectorAll(".thread").forEach(x=> x.classList.toggle("hidden"));
document.querySelectorAll(".script").forEach(x=> x.style=""); //unhide script-specific elements document.querySelectorAll(".script").forEach(x=> x.style=""); //unhide script-specific elements
document.querySelectorAll(".expand").forEach(x=> {
x.addEventListener("click", ()=> {
document.querySelector("[id='"+ x.getAttribute("href").slice(1) +"']").classList.toggle("hidden");
});
});
}); });

@ -0,0 +1,34 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Tools;
using System.IO;
using ImageMagick;
namespace ndview
{
public class ThumbnailGenerator
{
public readonly int Width,Height;
public ThumbnailGenerator(int width) : this(width,0){}
public ThumbnailGenerator(int width, int height)
{
(Width, Height) = (width, height);
}
public Task Thumbnail(Stream from, Stream to, CancellationToken token=default)
=> Task.Run(()=> {
using var image= new MagickImage();
image.Read(from);
token.ThrowIfCancellationRequested();
image.Resize(200, 0);
token.ThrowIfCancellationRequested();
image.Write(to);
token.ThrowIfCancellationRequested();
});
}
}

@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Magick.NET-Q8-x64" Version="7.16.0" />
<PackageReference Include="SharpZipLib" Version="1.2.0" /> <PackageReference Include="SharpZipLib" Version="1.2.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="5.0.0-preview.2.20160.6" /> <PackageReference Include="System.Text.Encodings.Web" Version="5.0.0-preview.2.20160.6" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save