using System; using System.IO; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.Callback; namespace prBrowser { internal class SchemeHandler : IResourceHandler, IDisposable { private static string appPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\"; private string mimeType; private Stream stream; private MainForm myForm; private Uri uri; private string fileName; public SchemeHandler(MainForm form) { myForm = form; } public void Dispose() { } public bool Open(IRequest request, out bool handleRequest, ICallback callback) { uri = new Uri(request.Url); fileName = uri.AbsolutePath; if (uri.Host == "storage") { fileName = appPath + uri.Host + fileName; if (File.Exists(fileName)) { Task.Factory.StartNew(delegate { using (callback) { FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); mimeType = ResourceHandler.GetMimeType(Path.GetExtension(fileName)); stream = fileStream; callback.Continue(); } }); handleRequest = false; return true; } } if (uri.Host == "fileicon") { Task.Factory.StartNew(delegate { using (callback) { callback.Continue(); } }); handleRequest = false; return true; } callback.Dispose(); handleRequest = true; return false; } public void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl) { responseLength = ((stream != null) ? stream.Length : 0); redirectUrl = null; response.StatusCode = 200; response.StatusText = "OK"; response.MimeType = mimeType; } public bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback) { callback.Dispose(); if (stream == null) { bytesRead = 0; return false; } byte[] buffer = new byte[dataOut.Length]; bytesRead = stream.Read(buffer, 0, buffer.Length); dataOut.Write(buffer, 0, buffer.Length); return bytesRead > 0; } public bool Read(Stream dataOut, out int bytesRead, IResourceReadCallback callback) { bytesRead = -1; return false; } public bool Skip(long bytesToSkip, out long bytesSkipped, IResourceSkipCallback callback) { bytesSkipped = -2L; return false; } public void Cancel() { } public bool CanGetCookie(Cookie cookie) { return true; } public bool CanSetCookie(Cookie cookie) { return true; } public bool ProcessRequest(IRequest request, ICallback callback) { return false; } } }