SchemeHandler.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using System.Windows.Forms;
  5. using CefSharp;
  6. using CefSharp.Callback;
  7. namespace prBrowser
  8. {
  9. internal class SchemeHandler : IResourceHandler, IDisposable
  10. {
  11. private static string appPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
  12. private string mimeType;
  13. private Stream stream;
  14. private MainForm myForm;
  15. private Uri uri;
  16. private string fileName;
  17. public SchemeHandler(MainForm form)
  18. {
  19. myForm = form;
  20. }
  21. public void Dispose()
  22. {
  23. }
  24. public bool Open(IRequest request, out bool handleRequest, ICallback callback)
  25. {
  26. uri = new Uri(request.Url);
  27. fileName = uri.AbsolutePath;
  28. if (uri.Host == "storage")
  29. {
  30. fileName = appPath + uri.Host + fileName;
  31. if (File.Exists(fileName))
  32. {
  33. Task.Factory.StartNew(delegate
  34. {
  35. using (callback)
  36. {
  37. FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  38. mimeType = ResourceHandler.GetMimeType(Path.GetExtension(fileName));
  39. stream = fileStream;
  40. callback.Continue();
  41. }
  42. });
  43. handleRequest = false;
  44. return true;
  45. }
  46. }
  47. if (uri.Host == "fileicon")
  48. {
  49. Task.Factory.StartNew(delegate
  50. {
  51. using (callback)
  52. {
  53. callback.Continue();
  54. }
  55. });
  56. handleRequest = false;
  57. return true;
  58. }
  59. callback.Dispose();
  60. handleRequest = true;
  61. return false;
  62. }
  63. public void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
  64. {
  65. responseLength = ((stream != null) ? stream.Length : 0);
  66. redirectUrl = null;
  67. response.StatusCode = 200;
  68. response.StatusText = "OK";
  69. response.MimeType = mimeType;
  70. }
  71. public bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
  72. {
  73. callback.Dispose();
  74. if (stream == null)
  75. {
  76. bytesRead = 0;
  77. return false;
  78. }
  79. byte[] buffer = new byte[dataOut.Length];
  80. bytesRead = stream.Read(buffer, 0, buffer.Length);
  81. dataOut.Write(buffer, 0, buffer.Length);
  82. return bytesRead > 0;
  83. }
  84. public bool Read(Stream dataOut, out int bytesRead, IResourceReadCallback callback)
  85. {
  86. bytesRead = -1;
  87. return false;
  88. }
  89. public bool Skip(long bytesToSkip, out long bytesSkipped, IResourceSkipCallback callback)
  90. {
  91. bytesSkipped = -2L;
  92. return false;
  93. }
  94. public void Cancel()
  95. {
  96. }
  97. public bool CanGetCookie(Cookie cookie)
  98. {
  99. return true;
  100. }
  101. public bool CanSetCookie(Cookie cookie)
  102. {
  103. return true;
  104. }
  105. public bool ProcessRequest(IRequest request, ICallback callback)
  106. {
  107. return false;
  108. }
  109. }
  110. }