1

I made an ASP.NET MVC project that downloads videos and audios from YouTube, I didn't need to use a database. I made it in VisualStudio Code and I'm using Somee.com's free plan to test this site, but when I upload the file and log in, I get a 403 access denied error. Can someone give me a light please

I want to know if I need to add some permission string in some project file

For anyone wanting to review my MVC code, this is the repository: https://github.com/GabrielAstra/YtbDownlMvc.git

Archive in Somee.com

Error 403

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
ChessDev
  • 31
  • 2
  • 1
    The folder you are using to upload file you do not have permission to write. Most times this error is solver by changing the default folder before uploading. – jdweng Aug 23 '23 at 12:57
  • 1
    Could you provide a minimal Code example to check? 403 usually means that you are not authorized to the server maybe your a missing a token in header. – faebzz Aug 23 '23 at 12:58
  • Post the code that is causing the error. No one here is going to peruse a repository to try to find the problem. – mxmissile Aug 24 '23 at 14:24
  • After testing and I found the issue, you can try to use `Path.Combine( _env.WebRootPath, "Downloads");` to fix the issue. – Jason Pan Aug 24 '23 at 14:32

2 Answers2

0

I have downloaded your repo and test it. After I deploy to azure web app and I found some details.

You should put your Downloads folder inside wwwroot. Official doc: Serve static files in asp.net core

It's my test result in azure app service. enter image description here

You should change your code like below:

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using YtbDown.Models;
using YtbDown.Util;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;


namespace YtbDown.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly YoutubeClient _youtube;
        private readonly IWebHostEnvironment _env;

        public HomeController(ILogger<HomeController> logger, IWebHostEnvironment env)
        {
            _logger = logger;
            _youtube = new YoutubeClient();
            _env = env;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }
        public IActionResult About()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        [HttpPost]
        public async Task<IActionResult> BaixarArquivo(string tipoMedia, string mediaUrl)
        {
            try
            {
                var videoInformacao = await _youtube.Videos.GetAsync(mediaUrl);
                var mediaTitulo = videoInformacao.Title;
                string diretorioDeSaida = Path.Combine(
                _env.WebRootPath, "Downloads");

                mediaTitulo = FormNome.FormatarNomeArquivo(mediaTitulo, 100);
                string extensao = tipoMedia == "audio" ? "mp3" : "mp4";
                var diretorioSaida = Path.Combine(diretorioDeSaida, $"{mediaTitulo}.{extensao}");

                Directory.CreateDirectory(diretorioDeSaida);

                var informacaoUrl = await _youtube.Videos.Streams.GetManifestAsync(mediaUrl);
                IStreamInfo infoFluxo = null;

                if (tipoMedia == "audio")
                {
                    infoFluxo = informacaoUrl.GetAudioOnlyStreams().GetWithHighestBitrate();
                }
                else if (tipoMedia == "video")
                {
                    infoFluxo = informacaoUrl.GetMuxedStreams().GetWithHighestVideoQuality();
                }

                if (infoFluxo != null)
                {
                    await _youtube.Videos.Streams.DownloadAsync(infoFluxo, diretorioSaida);
                }
                else
                {
                    throw new Exception(tipoMedia == "audio" ? "Nenhuma stream de áudio disponível." : "Nenhuma stream de vídeo disponível.");
                }

                string contentType = tipoMedia == "audio" ? "audio/mpeg" : "video/mp4";
                return PhysicalFile(diretorioSaida, contentType, Path.GetFileName(diretorioSaida));
            }
            catch (Exception ex)
            {
                return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
            }
        }
    }
}
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
0

I apologize, but I ended up finding the error. And it was the method I deployed the files to the server. It was nothing related to the project itself, but thank you all for your attention.

Solution: I deployed all the files on the server, before I was doing just the folder that contained the files...

ChessDev
  • 31
  • 2
  • 1
    Please notice, the using of your `Downloads` folder is not correct, you should follow the official doc to serve your static files. – Jason Pan Aug 28 '23 at 07:23