|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using TelegramDownloader.Data.db; |
| 3 | +using TelegramDownloader.Data; |
| 4 | +using TelegramDownloader.Models; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace TelegramDownloader.Controllers |
| 8 | +{ |
| 9 | + [Route("api/nodes")] |
| 10 | + [ApiController] |
| 11 | + public class WebDavController : ControllerBase |
| 12 | + { |
| 13 | + IDbService _db { get; set; } |
| 14 | + |
| 15 | + public WebDavController(IDbService db) |
| 16 | + { |
| 17 | + _db = db; |
| 18 | + } |
| 19 | + [HttpGet] |
| 20 | + public async Task<List<WebDavFileModel>> webDavPaths([FromQuery] string path, [FromQuery] string depth) |
| 21 | + { |
| 22 | + Console.WriteLine("Peticion de webDavPaths: " + path); |
| 23 | + var isFile = Path.HasExtension(path); |
| 24 | + if (!path.EndsWith("/") && !isFile) |
| 25 | + path = path + "/"; |
| 26 | + if (String.IsNullOrEmpty(path)) |
| 27 | + throw new BadHttpRequestException("Path is null or empty"); |
| 28 | + string channel = path.Split("/")[1]; |
| 29 | + if (String.IsNullOrEmpty(channel)) |
| 30 | + throw new BadHttpRequestException("Channel is empty"); |
| 31 | + path = path.Remove(0, path.IndexOf("/", 1)); |
| 32 | + List<WebDavFileModel> files = new List<WebDavFileModel>(); |
| 33 | + if (isFile) |
| 34 | + { |
| 35 | + var bsonFile = await _db.getFileByPath(channel, path); |
| 36 | + if (!(bsonFile == null) && bsonFile.IsFile) |
| 37 | + { |
| 38 | + WebDavFileModel file = bsonFile.toWebDavFileModel(channel); |
| 39 | + files = new List<WebDavFileModel>(); |
| 40 | + files.Add(file); |
| 41 | + } |
| 42 | + } |
| 43 | + if ((!isFile) || files.Count == 0) |
| 44 | + { |
| 45 | + if (depth == "0") |
| 46 | + { |
| 47 | + var bsonFile = await _db.getFileByPath(channel, path[..^1]); |
| 48 | + if (bsonFile != null) |
| 49 | + { |
| 50 | + WebDavFileModel file = bsonFile.toWebDavFileModel(channel); |
| 51 | + files = new List<WebDavFileModel>(); |
| 52 | + files.Add(file); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + else |
| 57 | + files = (await _db.getAllFilesInDirectoryPath(channel, path)).Select(file => file.toWebDavFileModel()).ToList(); |
| 58 | + } |
| 59 | + |
| 60 | + // Console.WriteLine(JsonSerializer.Serialize(files)); |
| 61 | + return files; |
| 62 | + } |
| 63 | + |
| 64 | + [HttpGet("meta")] |
| 65 | + public async Task<object> webDavMetadata([FromQuery] string path) |
| 66 | + { |
| 67 | + Console.WriteLine("Peticion de webDavMetadata: " + path); |
| 68 | + if (String.IsNullOrEmpty(path)) |
| 69 | + throw new Exception("Path is null or empty"); |
| 70 | + string channel = path.Split("/")[1]; |
| 71 | + if (String.IsNullOrEmpty(channel)) |
| 72 | + throw new Exception("Channel is empty"); |
| 73 | + path = path.Remove(0, path.IndexOf("/", 1)); |
| 74 | + var bsonFile = await _db.getFileByPath(channel, path); |
| 75 | + if (bsonFile == null || !bsonFile.IsFile) |
| 76 | + throw new FileNotFoundException(); |
| 77 | + WebDavFileModel files = bsonFile.toWebDavFileModel(channel); |
| 78 | + return files; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments