134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using Blog3000.Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using System.Text;
|
|
using System.IO;
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Blog3000.Client;
|
|
using System.Text.Json;
|
|
using System.Xml.Linq;
|
|
using System.Threading;
|
|
|
|
namespace Blog3000.Server.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class PublicDlController : ControllerBase
|
|
{
|
|
|
|
private readonly ILogger<BlogEnv> blogEnv;
|
|
private readonly ILogger<BlogPostsController> logger;
|
|
|
|
private readonly string redirectPrefix;
|
|
//private readonly string dlPath;
|
|
|
|
private static readonly SemaphoreSlim myLock = new SemaphoreSlim(1);
|
|
|
|
public PublicDlController(ILogger<BlogPostsController> logger, BlogEnv blogEnv)
|
|
{
|
|
this.logger = logger;
|
|
|
|
if (!String.IsNullOrWhiteSpace(blogEnv.BlogConfig.PublicDlUri))
|
|
{
|
|
try
|
|
{
|
|
Uri u = new Uri(blogEnv.BlogConfig.PublicDlUri);
|
|
//if ("file".Equals(u.Scheme))
|
|
//{
|
|
// dlPath = u.AbsolutePath;
|
|
//}
|
|
//else
|
|
{
|
|
redirectPrefix = blogEnv.BlogConfig.PublicDlUri;
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
logger.LogWarning("Error parsing Program.publicDlUrl: " + ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
[HttpGet("{file}")]
|
|
public async Task<ActionResult<Dictionary<string, int>>> GetAsync(string file)
|
|
{
|
|
logger.LogInformation($"PublicDl|{HttpContext.Connection.RemoteIpAddress}|GetAsync|{file}");
|
|
|
|
// TODO: Implmenent download counter
|
|
|
|
try
|
|
{
|
|
var fn = System.IO.Path.GetFileName(file);
|
|
|
|
if (!String.IsNullOrWhiteSpace(redirectPrefix))
|
|
{
|
|
// TODO: How to prevent logging bad file requests (logging unknown files, etc)
|
|
// -> Check if in Db, otherwise probe target url (range-dl 1 byte, modified?)
|
|
return Redirect($"{redirectPrefix}/{fn}");
|
|
}
|
|
//else if (!String.IsNullOrWhiteSpace(dlPath))
|
|
//{
|
|
// var path = System.IO.Path.Combine(dlPath, fn);
|
|
// var stream = System.IO.File.OpenRead(path);
|
|
// // File exists, can be logged
|
|
|
|
// return new FileStreamResult(stream, "application/octect-stream")
|
|
// {
|
|
// FileDownloadName = fn
|
|
// };
|
|
//}
|
|
else
|
|
{
|
|
logger.LogWarning($"PublicDl|{HttpContext.Connection.RemoteIpAddress}|Local download not ^configured");
|
|
return StatusCode(500);
|
|
}
|
|
}
|
|
catch(IOException ioex)
|
|
{
|
|
logger.LogWarning($"PublicDl|{HttpContext.Connection.RemoteIpAddress}|{ioex.Message}");
|
|
return StatusCode(400);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{file}/getredirurl")]
|
|
public ActionResult<string> Get(string file)
|
|
{
|
|
logger.LogInformation($"PublicDlGetUrl|{HttpContext.Connection.RemoteIpAddress}|GetAsync|{file}");
|
|
|
|
// TODO: Implmenent download counter
|
|
|
|
try
|
|
{
|
|
|
|
if (!String.IsNullOrWhiteSpace(redirectPrefix))
|
|
{
|
|
// TODO: How to prevent logging bad file requests (logging unknown files, etc)
|
|
// -> Check if in Db, otherwise probe target url (range-dl 1 byte, modified?)
|
|
var fn = System.IO.Path.GetFileName(file);
|
|
return $"{redirectPrefix}/{fn}";
|
|
|
|
}
|
|
else
|
|
{
|
|
logger.LogWarning($"PublicDlGetUrl|{HttpContext.Connection.RemoteIpAddress}|Local download not ^configured");
|
|
return StatusCode(500);
|
|
}
|
|
}
|
|
catch (IOException ioex)
|
|
{
|
|
logger.LogWarning($"PublicDlGetUrl|{HttpContext.Connection.RemoteIpAddress}|{ioex.Message}");
|
|
return StatusCode(400);
|
|
}
|
|
}
|
|
}
|
|
}
|