using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Nop.Core.Domain.Catalog; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; using Nop.Plugin.Misc.FruitBankPlugin.Services; using Nop.Services.Catalog; using Nop.Services.Security; using Nop.Web.Framework; using Nop.Web.Framework.Controllers; using Nop.Web.Framework.Mvc.Filters; using PDFtoImage; using SkiaSharp; namespace Nop.Plugin.Misc.FruitBank.Controllers { [AuthorizeAdmin] [Area(AreaNames.ADMIN)] [AutoValidateAntiforgeryToken] public class FileManagerController : BasePluginController { private readonly IPermissionService _permissionService; private readonly OpenAIApiService _aiApiService; private readonly IProductService _productService; private readonly FruitBankDbContext _dbContext; private readonly PdfToImageService _pdfToImageService; public FileManagerController( IPermissionService permissionService, OpenAIApiService aiApiService, IProductService productService, FruitBankDbContext fruitBankDbContext, PdfToImageService pdfToImageService) { _permissionService = permissionService; _aiApiService = aiApiService; _productService = productService; _dbContext = fruitBankDbContext; _pdfToImageService = pdfToImageService; } /// /// Display the image text extraction page /// public async Task ImageTextExtraction() { if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL)) return AccessDeniedView(); return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Extras/ImageTextExtraction.cshtml"); } /// /// Endpoint to extract text from uploaded image /// [HttpPost] public async Task ExtractTextFromImage(IFormFile imageFile, string customPrompt = null) { if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL)) return Json(new { success = false, message = "Access denied" }); if (imageFile == null || imageFile.Length == 0) { return Json(new { success = false, message = "No file received" }); } // Validate file type - now including PDF var extension = Path.GetExtension(imageFile.FileName).ToLowerInvariant(); if (extension != ".jpg" && extension != ".jpeg" && extension != ".png" && extension != ".gif" && extension != ".webp" && extension != ".pdf") { return Json(new { success = false, message = "Invalid file type. Please upload JPG, PNG, GIF, WebP, or PDF." }); } try { // Define the uploads folder var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", "ocr"); // Create directory if it doesn't exist if (!Directory.Exists(uploadsFolder)) { Directory.CreateDirectory(uploadsFolder); } string processedFilePath; string processedFileName; // Handle PDF conversion if (extension == ".pdf") { // Save the PDF temporarily var tempPdfFileName = $"temp_pdf_{DateTime.Now:yyyyMMdd_HHmmss}.pdf"; var tempPdfPath = Path.Combine(uploadsFolder, tempPdfFileName); using (var stream = new FileStream(tempPdfPath, FileMode.Create)) { await imageFile.CopyToAsync(stream); } // Convert PDF to JPG using our service var convertedImages = await _pdfToImageService.ConvertPdfToJpgAsync(tempPdfPath, uploadsFolder); if (convertedImages == null || convertedImages.Count == 0) { // Clean up temp PDF if (System.IO.File.Exists(tempPdfPath)) System.IO.File.Delete(tempPdfPath); return Json(new { success = false, message = "Failed to convert PDF or PDF is empty" }); } // Use the first page processedFilePath = convertedImages[0]; processedFileName = Path.GetFileName(processedFilePath); // Clean up temp PDF if (System.IO.File.Exists(tempPdfPath)) System.IO.File.Delete(tempPdfPath); } else { // Handle regular image files processedFileName = $"ocr_image_{DateTime.Now:yyyyMMdd_HHmmss}{extension}"; processedFilePath = Path.Combine(uploadsFolder, processedFileName); using (var stream = new FileStream(processedFilePath, FileMode.Create)) { await imageFile.CopyToAsync(stream); } } // Extract text from the processed image using OpenAI Vision API string extractedText; using (var imageStream = new FileStream(processedFilePath, FileMode.Open, FileAccess.Read)) { extractedText = await _aiApiService.ExtractTextFromImageAsync( imageStream, processedFileName, customPrompt ); } if (string.IsNullOrEmpty(extractedText)) { return Json(new { success = false, message = "Failed to extract text. The API may have returned an empty response." }); } return Json(new { success = true, message = extension == ".pdf" ? "PDF converted and text extracted successfully" : "Text extracted successfully", extractedText = extractedText, fileName = processedFileName, filePath = processedFilePath, fileSize = imageFile.Length, wasConverted = extension == ".pdf" }); } catch (Exception ex) { Console.Error.WriteLine($"Error in ExtractTextFromImage: {ex}"); return Json(new { success = false, message = $"Error processing file: {ex.Message}" }); } } } }