initial commit
This commit is contained in:
65
Controllers/HomeController.cs
Normal file
65
Controllers/HomeController.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Diagnostics;
|
||||
using Library.Models;
|
||||
|
||||
namespace Library.Controllers;
|
||||
|
||||
public class HomeController : PageController
|
||||
{
|
||||
public HomeController( ILogger<HomeController> logger , IConfiguration configuration )
|
||||
: base( logger , configuration )
|
||||
{
|
||||
}
|
||||
|
||||
[Route( "" )]
|
||||
[Route( "Home/Index" )]
|
||||
public IActionResult Index( )
|
||||
{
|
||||
Trace( "/Home/Index" );
|
||||
return View( new BasePageModel( GetDBConnection( ) ) );
|
||||
}
|
||||
|
||||
[Route( "Login" )]
|
||||
public IActionResult Login( )
|
||||
{
|
||||
Trace( "/Home/Login" );
|
||||
return View( new BasePageModel( GetDBConnection( ) ) );
|
||||
}
|
||||
|
||||
[Route( "Sitemap" )]
|
||||
public IActionResult Sitemap( )
|
||||
{
|
||||
Trace( "/Home/Sitemap" );
|
||||
return View( new BasePageModel( GetDBConnection( ) ) );
|
||||
}
|
||||
|
||||
[Route( "Search" )]
|
||||
public IActionResult Search( string q , int page )
|
||||
{
|
||||
Trace( "/Home/Search" );
|
||||
return View( new SearchPageModel( GetDBConnection( ) , q , page ) );
|
||||
}
|
||||
|
||||
[Route( "Repository" )]
|
||||
[Route( "Repository/{id?}" )]
|
||||
public IActionResult Repository( int id , int page )
|
||||
{
|
||||
Trace( "/Home/Repository" );
|
||||
return View( new RepositoryPageModel( GetDBConnection( ) , id , page ) );
|
||||
}
|
||||
|
||||
[Route( "Repository/Details" )]
|
||||
[Route( "Repository/Details/{id?}" )]
|
||||
public IActionResult RepositoryDetails( int id )
|
||||
{
|
||||
Trace( "/Home/RepositoryDetails" );
|
||||
return View( new RepositoryDetailsPageModel( GetDBConnection( ) , id ) );
|
||||
}
|
||||
|
||||
[ResponseCache( Duration = 0 , Location = ResponseCacheLocation.None , NoStore = true )]
|
||||
public IActionResult Error( )
|
||||
{
|
||||
Trace( "/Home/Error" );
|
||||
return View( new ErrorPageModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier } );
|
||||
}
|
||||
}
|
||||
212
Controllers/RepositoryController.cs
Normal file
212
Controllers/RepositoryController.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Docnet.Core;
|
||||
using Docnet.Core.Models;
|
||||
using Docnet.Core.Readers;
|
||||
using SkiaSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Library.Controllers;
|
||||
|
||||
public class RepositoryController : PageController
|
||||
{
|
||||
public RepositoryController( ILogger<RepositoryController> logger , IConfiguration configuration )
|
||||
: base( logger , configuration )
|
||||
{
|
||||
}
|
||||
|
||||
public IActionResult Cover( string? id )
|
||||
{
|
||||
Trace( "/Repository/Cover" );
|
||||
if ( id?.Length != 32 )
|
||||
return RawText( "No such cover!" );
|
||||
|
||||
using SqlDataReader reader = PrepareSQL( @"SELECT uuid,data_format FROM gnm WHERE uuid=@uuid" )
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return RawText( "No such cover!" );
|
||||
if ( System.IO.File.Exists( GetDataDirectory( ) + id + ".jpg" ) )
|
||||
{
|
||||
byte [] bytes = System.IO.File.ReadAllBytes( GetDataDirectory( ) + id + ".jpg");
|
||||
HttpContext.Response.Headers.ContentDisposition = "inline;filename=" + id + ".jpg";
|
||||
return File( bytes , System.Net.Mime.MediaTypeNames.Image.Jpeg );
|
||||
}
|
||||
return RawText( "No such cover!" );
|
||||
}
|
||||
|
||||
public IActionResult Video( string? id )
|
||||
{
|
||||
Trace( "/Repository/Video" );
|
||||
if ( id?.Length != 32 )
|
||||
return RawText( "No such video!" );
|
||||
|
||||
if ( GetUserData( ) != null && !GetUserData( )!.IsAdmin )
|
||||
{
|
||||
SqlDataReader reader_u = PrepareSQL( @"update gnm_rec set cnt = isnull(cnt,0) + 1 where gnm_rec.uuid = @uuid")
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
reader_u.Close( );
|
||||
}
|
||||
using SqlDataReader reader = PrepareSQL( @"SELECT uuid,data_format FROM gnm_rec WHERE uuid=@uuid and data_format='mp4'" )
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return RawText( "No such file!" );
|
||||
|
||||
byte [] bytes = System.IO.File.ReadAllBytes( GetDataDirectory( ) + id + ".mp4");
|
||||
|
||||
HttpContext.Response.Headers.ContentDisposition = "inline;filename=" + id + ".mp4";
|
||||
return File( bytes , "video/mp4" );
|
||||
}
|
||||
|
||||
private string? ReadPDFTitle( string id )
|
||||
{
|
||||
AssertLogin( id );
|
||||
if ( id.Length != 32 )
|
||||
return null;
|
||||
|
||||
using SqlDataReader reader = PrepareSQL( @"SELECT gnm.ime FROM gnm_rec left join gnm on gnm.id = gnm_rec.idgnm WHERE gnm_rec.uuid = @uuid and gnm_rec.data_format = 'pdf'" )
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return null;
|
||||
|
||||
return reader.String( 0 );
|
||||
}
|
||||
|
||||
private byte[]? ReadPDFBytes( string id )
|
||||
{
|
||||
// return System.IO.File.ReadAllBytes( "c:/Users/gbromov/Downloads/3950.pdf" );
|
||||
return System.IO.File.ReadAllBytes( GetDataDirectory( ) + id + ".pdf" );
|
||||
}
|
||||
|
||||
private IActionResult RenderPDFPage( string id , int page , int width , int height )
|
||||
{
|
||||
if ( ReadPDFTitle( id ) == null )
|
||||
return RawText( "No such file!" );
|
||||
|
||||
byte[]? bytes = ReadPDFBytes( id );
|
||||
if ( bytes == null )
|
||||
return RawText( "No such file!" );
|
||||
|
||||
using IDocReader doc_reader = DocLib.Instance.GetDocReader( bytes , new PageDimensions( width , height ) );
|
||||
|
||||
using IPageReader page_reader = doc_reader.GetPageReader( page );
|
||||
byte [] rgb_bytes = page_reader.GetImage( );
|
||||
width = page_reader.GetPageWidth( );
|
||||
height = page_reader.GetPageHeight( );
|
||||
|
||||
GCHandle data_handle = GCHandle.Alloc( rgb_bytes , GCHandleType.Pinned );
|
||||
SKImageInfo info = new( width , height , SKColorType.Bgra8888 , SKAlphaType.Unpremul );
|
||||
|
||||
using SKBitmap bitmap = new( );
|
||||
bitmap.InstallPixels( info , data_handle.AddrOfPinnedObject( ) , info.RowBytes , delegate { data_handle.Free( ); } , null );
|
||||
|
||||
using SKBitmap background = new( width , height , SKColorType.Bgra8888 , SKAlphaType.Opaque );
|
||||
background.Erase( new SKColor( 255 , 255 , 255 ) );
|
||||
|
||||
using SKCanvas canvas = new(background);
|
||||
canvas.DrawBitmap( bitmap , 0 , 0 , SKSamplingOptions.Default );
|
||||
|
||||
using SKData bitmap_data = background.Encode( SKEncodedImageFormat.Jpeg , 80 );
|
||||
byte [] bitmap_data_array = bitmap_data.ToArray( );
|
||||
|
||||
return File( bitmap_data_array , System.Net.Mime.MediaTypeNames.Image.Jpeg );
|
||||
}
|
||||
|
||||
[Route( "Repository/PDFPage/{id}/{page?}" )]
|
||||
public IActionResult PDFPage( string id , int page = 0 )
|
||||
{
|
||||
Trace( "/Repository/PDFPage" );
|
||||
return RenderPDFPage( id , page , 1080 , 1920 );
|
||||
}
|
||||
|
||||
[Route( "Repository/PDFThumb/{id}/{page?}" )]
|
||||
public IActionResult PDFThumb( string id , int page = 0 )
|
||||
{
|
||||
Trace( "/Repository/PDFPage" );
|
||||
return RenderPDFPage( id , page , 135 , 240 );
|
||||
}
|
||||
|
||||
[Route( "Repository/PDFView/{id}" )]
|
||||
public IActionResult PDFView( string id )
|
||||
{
|
||||
Trace( "/Repository/PDFView" );
|
||||
|
||||
string? pdf_title = ReadPDFTitle( id );
|
||||
if ( pdf_title == null )
|
||||
throw new RedirectException( "/" );
|
||||
|
||||
byte[]? bytes = ReadPDFBytes( id );
|
||||
if ( bytes == null )
|
||||
throw new RedirectException( "/" );
|
||||
|
||||
// if ( GetUserData( ) != null && !GetUserData( )!.IsAdmin ) --- è àäìèíà, è âñåêè ìîæå äà îòâàðÿ PDF --- increment counter
|
||||
{
|
||||
SqlDataReader reader_u = PrepareSQL( @"update gnm_rec set cnt = isnull(cnt,0) + 1 where gnm_rec.uuid = @uuid")
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
reader_u.Close( );
|
||||
}
|
||||
|
||||
using IDocReader doc_reader = DocLib.Instance.GetDocReader( bytes , new PageDimensions( 1080 , 1920 ) );
|
||||
int page_count = doc_reader.GetPageCount( );
|
||||
|
||||
return View( new Models.PDFViewModel( GetDBConnection( ) , id , page_count )
|
||||
{
|
||||
Title = pdf_title
|
||||
} );
|
||||
}
|
||||
|
||||
public IActionResult PDF( string? id )
|
||||
{
|
||||
Trace( "/Repository/PDF" );
|
||||
AssertLogin( id );
|
||||
|
||||
if ( id?.Length != 32 )
|
||||
return RawText( "No such file!" );
|
||||
|
||||
using SqlDataReader reader = PrepareSQL( @"SELECT gnm.access, gnm_rec.uuid, gnm_rec.data_format FROM gnm_rec left join gnm on gnm.id = gnm_rec.idgnm WHERE gnm_rec.uuid = @uuid and gnm_rec.data_format = 'pdf'" )
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return RawText( "No such file!" );
|
||||
|
||||
byte [] bytes = System.IO.File.ReadAllBytes( GetDataDirectory( ) + id + ".pdf");
|
||||
|
||||
HttpContext.Response.Headers.ContentDisposition = "inline;filename=" + id + ".pdf";
|
||||
return File( bytes , System.Net.Mime.MediaTypeNames.Application.Pdf );
|
||||
}
|
||||
|
||||
public IActionResult Image( string? id )
|
||||
{
|
||||
Trace( "/Repository/Image" );
|
||||
if ( id?.Length != 32 )
|
||||
return RawText( "No such image!" );
|
||||
|
||||
if ( GetUserData( ) != null && !GetUserData( )!.IsAdmin )
|
||||
{
|
||||
SqlDataReader reader_u = PrepareSQL( @"update gnm_rec set cnt = isnull(cnt,0) + 1 where gnm_rec.uuid = @uuid")
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
reader_u.Close( );
|
||||
}
|
||||
using SqlDataReader reader = PrepareSQL( @"SELECT uuid,data_format FROM gnm_rec WHERE uuid=@uuid and data_format='jpg'" )
|
||||
.AddParameter( "@uuid" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return RawText( "No such file!" );
|
||||
|
||||
byte [] bytes = System.IO.File.ReadAllBytes( GetDataDirectory( ) + id + ".jpg");
|
||||
|
||||
HttpContext.Response.Headers.ContentDisposition = "inline;filename=" + id + ".jpg";
|
||||
return File( bytes , System.Net.Mime.MediaTypeNames.Image.Jpeg );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user