using FruitBank.Common.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.FruitBankPlugin.Services.FileStorage;
using Nop.Services.Security;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Nop.Plugin.Misc.FruitBank.Controllers
{
[AuthorizeAdmin]
[Area(AreaNames.ADMIN)]
[AutoValidateAntiforgeryToken]
public class FileStorageController : BasePluginController
{
private readonly FileStorageService _fileStorageService;
private readonly FruitBankDbContext _dbContext;
private readonly IPermissionService _permissionService;
private readonly IWorkContext _workContext;
public FileStorageController(
FileStorageService fileStorageService,
FruitBankDbContext dbContext,
IPermissionService permissionService,
IWorkContext workContext)
{
_fileStorageService = fileStorageService;
_dbContext = dbContext;
_permissionService = permissionService;
_workContext = workContext;
}
#region Upload Files
///
/// Upload a single file
///
/// The uploaded file
/// Feature name (e.g., "AIdocumentprocessing")
/// Entity type (e.g., "ShippingDocuments")
/// Entity ID
/// Optional raw text for searchable documents
[HttpPost]
public async Task UploadFile(
IFormFile file,
string featureName,
string entityType,
int entityId,
string rawText = null)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return Json(new { success = false, message = "Access denied" });
if (file == null || file.Length == 0)
return Json(new { success = false, message = "No file uploaded" });
if (string.IsNullOrWhiteSpace(featureName))
return Json(new { success = false, message = "Feature name is required" });
if (string.IsNullOrWhiteSpace(entityType))
return Json(new { success = false, message = "Entity type is required" });
if (entityId <= 0)
return Json(new { success = false, message = "Valid entity ID is required" });
try
{
var currentUser = await _workContext.GetCurrentCustomerAsync();
var userId = currentUser.Id;
using (var stream = file.OpenReadStream())
{
var fileEntity = await _fileStorageService.SaveFileAsync(
fileStream: stream,
fileName: file.FileName,
userId: userId,
featureName: featureName,
entityType: entityType,
entityId: entityId,
rawText: rawText
);
return Json(new
{
success = true,
message = "File uploaded successfully",
file = new
{
id = fileEntity.Id,
fileName = fileEntity.FileName,
fileExtension = fileEntity.FileExtension,
created = fileEntity.Created,
hasRawText = !string.IsNullOrEmpty(fileEntity.RawText)
}
});
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error uploading file: {ex}");
return Json(new
{
success = false,
message = $"Error uploading file: {ex.Message}"
});
}
}
///
/// Upload multiple files at once
///
[HttpPost]
public async Task UploadMultipleFiles(
List files,
string featureName,
string entityType,
int entityId)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
return Json(new { success = false, message = "Access denied" });
if (files == null || files.Count == 0)
return Json(new { success = false, message = "No files uploaded" });
try
{
var currentUser = await _workContext.GetCurrentCustomerAsync();
var userId = currentUser.Id;
var uploadedFiles = new List