L1SvrFileProvider.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using LJProxy.Services;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.Extensions.FileProviders;
  4. using Microsoft.Extensions.Primitives;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. namespace LJProxy.Providers
  12. {
  13. public class L1SvrFileProvider : IFileProvider
  14. {
  15. private string _contentRoot;
  16. private LJClientPoolService _ljClient;
  17. private string _token;
  18. IHttpContextAccessor _accessor;
  19. public L1SvrFileProvider(string contentRoot, LJClientPoolService ljClient,IHttpContextAccessor accessor)
  20. {
  21. _contentRoot = contentRoot;
  22. _ljClient = ljClient;
  23. _accessor = accessor;
  24. }
  25. public IDirectoryContents GetDirectoryContents(string subpath)
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public IFileInfo GetFileInfo(string subpath)
  30. {
  31. string physicSubpath = subpath.Replace("/","\\");
  32. int suffixIndex = physicSubpath.IndexOf(DocumentSuffix);
  33. if (suffixIndex > 0)
  34. {
  35. string fileExtension = Path.GetExtension(physicSubpath);
  36. physicSubpath = physicSubpath.Substring(0, suffixIndex);
  37. physicSubpath = physicSubpath + fileExtension;
  38. }
  39. physicSubpath = HttpUtility.UrlDecode(physicSubpath);
  40. string directory = @$"{_contentRoot}\wwwroot";
  41. var fullFilepath = @$"{directory}{physicSubpath}";
  42. _token = _accessor.HttpContext.Request.Headers["Authorization"];
  43. var result = new L1SvrFileInfo(fullFilepath, physicSubpath,_ljClient.Pool,_token);
  44. return result;
  45. }
  46. private readonly static string DocumentSuffix = "$27BAljSuffix!";
  47. public IChangeToken Watch(string filter)
  48. {
  49. throw new NotImplementedException();
  50. }
  51. }
  52. }