EmbeddedFilePathHelper.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Text;
  3. namespace Abp.Resources.Embedded
  4. {
  5. public static class EmbeddedResourcePathHelper
  6. {
  7. public static string NormalizePath(string fullPath)
  8. {
  9. return fullPath?.Replace("/", ".").TrimStart('.');
  10. }
  11. public static string EncodeAsResourcesPath(string subPath)
  12. {
  13. var builder = new StringBuilder(subPath.Length);
  14. // does the subpath contain directory portion - if so we need to encode it.
  15. var indexOfLastSeperator = subPath.LastIndexOf('/');
  16. if (indexOfLastSeperator != -1)
  17. {
  18. // has directory portion to encode.
  19. for (int i = 0; i <= indexOfLastSeperator; i++)
  20. {
  21. var currentChar = subPath[i];
  22. if (currentChar == '/')
  23. {
  24. if (i != 0) // omit a starting slash (/), encode any others as a dot
  25. {
  26. builder.Append('.');
  27. }
  28. continue;
  29. }
  30. if (currentChar == '-')
  31. {
  32. builder.Append('_');
  33. continue;
  34. }
  35. builder.Append(currentChar);
  36. }
  37. }
  38. // now append (and encode as necessary) filename portion
  39. if (subPath.Length > indexOfLastSeperator + 1)
  40. {
  41. // has filename to encode
  42. for (int c = indexOfLastSeperator + 1; c < subPath.Length; c++)
  43. {
  44. var currentChar = subPath[c];
  45. // no encoding to do on filename - so just append
  46. builder.Append(currentChar);
  47. }
  48. }
  49. return builder.ToString();
  50. }
  51. }
  52. }