diff --git a/server/JsDbg.Core/DllImports.cs b/server/JsDbg.Core/DllImports.cs new file mode 100644 index 00000000..220015a0 --- /dev/null +++ b/server/JsDbg.Core/DllImports.cs @@ -0,0 +1,19 @@ +//-------------------------------------------------------------- +// +// MIT License +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +//-------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; + +namespace JsDbg.Core { + internal class DllImports { + [DllImport("shlwapi.dll")] + internal static extern bool PathIsNetworkPath(string path); + } +} diff --git a/server/JsDbg.Core/FileCache.cs b/server/JsDbg.Core/FileCache.cs new file mode 100644 index 00000000..23de013a --- /dev/null +++ b/server/JsDbg.Core/FileCache.cs @@ -0,0 +1,55 @@ +//-------------------------------------------------------------- +// +// MIT License +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +//-------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; + +namespace JsDbg.Core { + internal class FileCache { + internal FileCache() { + this.cachedFiles = new Dictionary(); + this.lastFlushDate = DateTime.Now; + } + + internal static bool PathIsNetworkPath(string path) { + try { + return DllImports.PathIsNetworkPath(path); + } catch (DllNotFoundException) { + return false; + } + } + + internal Stream ReadFile(string path) { + // Flush the cache every hour for the scenario where JsDbg is left open overnight etc. + if ((DateTime.Now - this.lastFlushDate).TotalSeconds > 3600) { + this.lastFlushDate = DateTime.Now; + this.cachedFiles = new Dictionary(); + } + + if (FileCache.PathIsNetworkPath(path)) { + if (!cachedFiles.ContainsKey(path)) { + cachedFiles[path] = File.ReadAllText(path); + } + + MemoryStream stream = new MemoryStream(); + StreamWriter writer = new StreamWriter(stream); + writer.Write(cachedFiles[path]); + writer.Flush(); + stream.Position = 0; + return stream; + } else { + return File.OpenRead(path); + } + } + + private DateTime lastFlushDate; + private Dictionary cachedFiles; + } +}