175 lines
5.6 KiB
C#
175 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Serilog;
|
|
using LCU.Api.Internal.FTP.Extensions;
|
|
using LCU.Api.Internal.FTP.Helpers;
|
|
using LCU.Api.Internal.FTP.Services;
|
|
using LCU.Api.Internal.FTPLib.Models;
|
|
using LCU.Api.Internal.FTP.Services.FtpOpertionServices;
|
|
using LCU.Library.Internal.Exceptions;
|
|
namespace LCU.Api.Internal.FTP.Controllers;
|
|
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class SftpController : ControllerBase
|
|
{
|
|
private readonly IQueueFtpService _queueFtpService;
|
|
private readonly IWinScpListDirectoryService _winScpSftpListDirectoryService;
|
|
|
|
public SftpController(IQueueFtpService queueFtpService,
|
|
IWinScpListDirectoryService winScpSftpListDirectoryService)
|
|
{
|
|
_queueFtpService = queueFtpService;
|
|
_winScpSftpListDirectoryService = winScpSftpListDirectoryService;
|
|
}
|
|
|
|
[Obsolete("01/04/2022 - Please use QueueSftpUploadRequest Endpoint for Sftp Request and SshFtpController QueueSshFtpRequest Endpoint")]
|
|
[HttpPost]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult Post([FromBody] SftpRequest request)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
if (!WinScpSessionOptionValidators.IsValidSessionOptions(request))
|
|
{
|
|
Log.Error("Sftp request can not be created into a valid session options.");
|
|
return BadRequest("Sftp request can not be created into a valid session options.");
|
|
}
|
|
|
|
|
|
_queueFtpService.QueueSftp(request);
|
|
return Ok();
|
|
}
|
|
catch (ServiceException)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[Obsolete("01/04/2022 - Please use QueueSftpUploadRequest Endpoint")]
|
|
[HttpPost("QueueSftpDynamicFileRequest")]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult QueueSftpDynamicFileRequest([FromBody] SftpDynamicFileRequest request)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
if (!WinScpSessionOptionValidators.IsValidSessionOptions(request))
|
|
{
|
|
return BadRequest("Sftp request can not be created into a valid session options.");
|
|
}
|
|
|
|
_queueFtpService.QueueSftpDynamicFileRequest(request);
|
|
return Ok();
|
|
}
|
|
catch (ServiceException)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueueSftpUploadRequest")]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult QueueSftpUploadRequest([FromBody] SftpUploadFileRequest request)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
_queueFtpService.QueueSftpUploadRequest(request);
|
|
return Ok();
|
|
}
|
|
catch (ServiceException)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueueSftpDownloadRequest")]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult QueueSftpDownloadRequest([FromBody] SftpDownloadFileRequest request)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
_queueFtpService.QueueSftpDownloadRequest(request);
|
|
return Ok();
|
|
}
|
|
catch (ServiceException)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("ListFilesInDirectory")]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult ListFilesInDirectory([FromQuery] ListDirectoryRequest request)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
var result = _winScpSftpListDirectoryService.GetListOfFilesInDirectory(request);
|
|
return Ok(result);
|
|
}
|
|
catch (ServiceException)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
} |