127 lines
4.4 KiB
C#
127 lines
4.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using WhatsappPromo.Core.Models;
|
|
using WhatsappPromo.Core.Services;
|
|
|
|
namespace WhatsappPromo.Worker.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class ConfigController : ControllerBase
|
|
{
|
|
private readonly IConfigService _configService;
|
|
|
|
public ConfigController(IConfigService configService)
|
|
{
|
|
_configService = configService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetConfig()
|
|
{
|
|
var config = await _configService.GetConfigAsync();
|
|
return Ok(config);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> UpdateConfig([FromBody] SystemConfig config)
|
|
{
|
|
if (!string.IsNullOrEmpty(config.DownloadPath))
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(config.DownloadPath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest($"Ruta inválida: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
await _configService.UpdateConfigAsync(config);
|
|
return Ok(config);
|
|
}
|
|
|
|
[HttpPost("start")]
|
|
public async Task<IActionResult> Start()
|
|
{
|
|
var config = await _configService.GetConfigAsync();
|
|
config.IsActive = true;
|
|
await _configService.UpdateConfigAsync(config);
|
|
return Ok(new { status = "Starting" });
|
|
}
|
|
|
|
[HttpPost("stop")]
|
|
public async Task<IActionResult> Stop()
|
|
{
|
|
var config = await _configService.GetConfigAsync();
|
|
config.IsActive = false;
|
|
await _configService.UpdateConfigAsync(config);
|
|
return Ok(new { status = "Stopping" });
|
|
}
|
|
|
|
[System.Runtime.InteropServices.DllImport("user32.dll")]
|
|
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[System.Runtime.InteropServices.DllImport("user32.dll")]
|
|
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
|
|
|
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
|
private const uint SWP_NOSIZE = 0x0001;
|
|
private const uint SWP_NOMOVE = 0x0002;
|
|
private const uint SWP_SHOWWINDOW = 0x0040;
|
|
|
|
[HttpGet("browse")]
|
|
public IActionResult Browse()
|
|
{
|
|
string? selectedPath = null;
|
|
|
|
var thread = new Thread(() =>
|
|
{
|
|
using (var dummyForm = new Form())
|
|
{
|
|
dummyForm.TopMost = true;
|
|
// El form debe ser "real" para que Windows lo respete
|
|
dummyForm.Width = 1; dummyForm.Height = 1;
|
|
dummyForm.Opacity = 0.05;
|
|
dummyForm.ShowInTaskbar = false;
|
|
dummyForm.FormBorderStyle = FormBorderStyle.None;
|
|
dummyForm.StartPosition = FormStartPosition.CenterScreen;
|
|
|
|
dummyForm.Show();
|
|
|
|
// Forzamos la posición al frente de todo
|
|
SetWindowPos(dummyForm.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
|
|
SetForegroundWindow(dummyForm.Handle);
|
|
dummyForm.Activate();
|
|
dummyForm.Focus();
|
|
|
|
using (var fbd = new FolderBrowserDialog())
|
|
{
|
|
fbd.Description = "Selecciona la carpeta para descargas de WhatsApp";
|
|
fbd.UseDescriptionForTitle = true;
|
|
fbd.ShowNewFolderButton = true;
|
|
|
|
if (fbd.ShowDialog(dummyForm) == DialogResult.OK)
|
|
{
|
|
selectedPath = fbd.SelectedPath;
|
|
}
|
|
}
|
|
dummyForm.Close();
|
|
}
|
|
});
|
|
|
|
thread.SetApartmentState(ApartmentState.STA);
|
|
thread.Start();
|
|
thread.Join();
|
|
|
|
return Ok(new { path = selectedPath });
|
|
}
|
|
}
|
|
}
|