initial commit
This commit is contained in:
180
Api/RepositoryApi.cs
Normal file
180
Api/RepositoryApi.cs
Normal file
@@ -0,0 +1,180 @@
|
||||
using BitMiracle.LibTiff.Classic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SkiaSharp;
|
||||
using System.Buffers.Text;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.IO;
|
||||
|
||||
namespace Library.Api;
|
||||
|
||||
[Route( "Api/Repository/[action]/{id?}" )]
|
||||
public class RepositoryApi : APIController
|
||||
{
|
||||
public RepositoryApi( ILogger<UserApi> logger , IConfiguration configuration )
|
||||
: base( logger , configuration )
|
||||
{
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[DisableRequestSizeLimit]
|
||||
public IActionResult UploadCoverImage( int id , List<IFormFile>? file )
|
||||
{
|
||||
Trace( "/Api/Repository/UploadCoverImage" );
|
||||
if ( file == null || file.Count <= 0 )
|
||||
return Error( "Missing file" );
|
||||
|
||||
using SKBitmap? bitmap = PreprocessImage( file[0] );
|
||||
if ( bitmap == null )
|
||||
return Error( "Invalid image" );
|
||||
|
||||
SqlDataReader reader = PrepareSQL(@"update gnm set uuid = REPLACE(NEWID( ),'-',''),data_format='jpg' OUTPUT INSERTED.uuid WHERE id = @id")
|
||||
.AddParameter( "@id" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return Error( "No such book!" );
|
||||
|
||||
string? new_uuid = reader.String( 0 );
|
||||
if ( string.IsNullOrEmpty( new_uuid ) )
|
||||
return Error( "No such book!" );
|
||||
|
||||
string original = GetDataDirectory( ) + new_uuid + ".jpg";
|
||||
|
||||
using SKData bitmap_data = bitmap.Encode( SKEncodedImageFormat.Jpeg , 80 );
|
||||
byte [] bitmap_data_array = bitmap_data.ToArray( );
|
||||
System.IO.File.WriteAllBytes( original , bitmap_data_array );
|
||||
|
||||
return Success( new_uuid );
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[DisableRequestSizeLimit]
|
||||
public IActionResult UploadPDFAttachment( int id , List<IFormFile>? file )
|
||||
{
|
||||
Trace( "/Api/Repository/UploadPDFAttachment" );
|
||||
if ( file == null || file.Count <= 0 )
|
||||
return Error( "Missing file" );
|
||||
|
||||
byte [] ? pdf_data_array = PreprocessPDF( file[0] );
|
||||
if ( pdf_data_array == null )
|
||||
return Error( "Invalid file" );
|
||||
|
||||
SqlDataReader reader = PrepareSQL(@"update gnm_rec set uuid = REPLACE(NEWID( ),'-',''),data_format='pdf' OUTPUT INSERTED.uuid WHERE id = @id")
|
||||
.AddParameter( "@id" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return Error( "No such attachment!" );
|
||||
|
||||
string? new_uuid = reader.String( 0 );
|
||||
if ( string.IsNullOrEmpty( new_uuid ) )
|
||||
return Error( "No such attachment!" );
|
||||
|
||||
string original = GetDataDirectory( ) + new_uuid + ".pdf";
|
||||
System.IO.File.WriteAllBytes( original , pdf_data_array );
|
||||
|
||||
return Success( new_uuid );
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[DisableRequestSizeLimit]
|
||||
public IActionResult UploadImageAttachment( int id , List<IFormFile>? file )
|
||||
{
|
||||
Trace( "/Api/Repository/UploadImageAttachment" );
|
||||
if ( file == null || file.Count <= 0 )
|
||||
return Error( "Missing file" );
|
||||
|
||||
using SKBitmap? bitmap = PreprocessImage( file[0] );
|
||||
if ( bitmap == null )
|
||||
return Error( "Invalid image" );
|
||||
|
||||
SqlDataReader reader = PrepareSQL(@"update gnm_rec set uuid = REPLACE(NEWID( ),'-',''),data_format='jpg' OUTPUT INSERTED.uuid WHERE id = @id")
|
||||
.AddParameter( "@id" , id )
|
||||
.ExecuteReader( );
|
||||
|
||||
if ( !reader.Read( ) )
|
||||
return Error( "No such attachment!" );
|
||||
|
||||
string? new_uuid = reader.String( 0 );
|
||||
if ( string.IsNullOrEmpty( new_uuid ) )
|
||||
return Error( "No such attachment!" );
|
||||
|
||||
string original = GetDataDirectory( ) + new_uuid + ".jpg";
|
||||
|
||||
using SKData bitmap_data = bitmap.Encode( SKEncodedImageFormat.Jpeg , 80 );
|
||||
byte [] bitmap_data_array = bitmap_data.ToArray( );
|
||||
System.IO.File.WriteAllBytes( original , bitmap_data_array );
|
||||
|
||||
return Success( new_uuid );
|
||||
}
|
||||
|
||||
|
||||
private static SKBitmap? PreprocessImage( IFormFile form_file )
|
||||
{
|
||||
SKBitmap? bitmap = SKBitmap.Decode( form_file.OpenReadStream( ) );
|
||||
if ( bitmap == null )
|
||||
{
|
||||
bitmap = ConvertTiffToSKBitmap( form_file.OpenReadStream( ) );
|
||||
if ( bitmap == null )
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( bitmap!.IsEmpty || bitmap.IsNull || bitmap.Width < 16 || bitmap.Height < 16 )
|
||||
return null;
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
private static byte[]? PreprocessPDF( IFormFile form_file )
|
||||
{
|
||||
using MemoryStream file_contents = new( );
|
||||
form_file.CopyTo( file_contents );
|
||||
|
||||
byte[] file_bytes = file_contents.ToArray( );
|
||||
if ( file_bytes.Length < 10 )
|
||||
return null;
|
||||
|
||||
if ( file_bytes[0] != (byte)'%' || file_bytes[1] != (byte)'P' || file_bytes[2] != 'D' || file_bytes[3] != 'F' )
|
||||
return null;
|
||||
|
||||
|
||||
return file_bytes;
|
||||
}
|
||||
|
||||
public static SKBitmap? ConvertTiffToSKBitmap( Stream tifImage )
|
||||
{
|
||||
SKColor[] pixels;
|
||||
int width, height;
|
||||
// open a Tiff stored in the memory stream, and grab its pixels
|
||||
using ( Tiff tifImg = Tiff.ClientOpen( "in-memory" , "r" , tifImage , new TiffStream( ) ) )
|
||||
{
|
||||
FieldValue[] value = tifImg.GetField(TiffTag.IMAGEWIDTH);
|
||||
width = value[0].ToInt( );
|
||||
|
||||
value = tifImg.GetField( TiffTag.IMAGELENGTH );
|
||||
height = value[0].ToInt( );
|
||||
|
||||
// Read the image into the memory buffer
|
||||
int[] raster = new int[width * height];
|
||||
if ( !tifImg.ReadRGBAImageOriented( width , height , raster , Orientation.TOPLEFT ) )
|
||||
{
|
||||
// Not a valid TIF image.
|
||||
}
|
||||
// store the pixels
|
||||
pixels = new SKColor[width * height];
|
||||
for ( int y = 0 ; y < height ; y++ )
|
||||
{
|
||||
for ( int x = 0 ; x < width ; x++ )
|
||||
{
|
||||
int arrayOffset = y * width + x;
|
||||
int rgba = raster[arrayOffset];
|
||||
pixels[arrayOffset] = new SKColor( (byte)Tiff.GetR( rgba ) , (byte)Tiff.GetG( rgba ) , (byte)Tiff.GetB( rgba ) , (byte)Tiff.GetA( rgba ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new SKBitmap( width , height ) {
|
||||
Pixels = pixels
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user