Last active
September 27, 2016 15:18
-
-
Save mikebrind/dfc9be08054bb999f8a669042763e735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.FileProviders; | |
using System; | |
using System.Data.SqlClient; | |
using System.IO; | |
using System.Text; | |
namespace RazorEngineViewOptionsFileProviders | |
{ | |
public class DatabaseFileInfo : IFileInfo | |
{ | |
private string _viewPath; | |
private byte[] _viewContent; | |
private DateTimeOffset _lastModified; | |
private bool _exists; | |
public DatabaseFileInfo(string connection, string viewPath) | |
{ | |
_viewPath = viewPath; | |
GetView(connection, viewPath); | |
} | |
public bool Exists => _exists; | |
public bool IsDirectory => false; | |
public DateTimeOffset LastModified => _lastModified; | |
public long Length | |
{ | |
get | |
{ | |
using (var stream = new MemoryStream(_viewContent)) | |
{ | |
return stream.Length; | |
} | |
} | |
} | |
public string Name => Path.GetFileName(_viewPath); | |
public string PhysicalPath => null; | |
public Stream CreateReadStream() | |
{ | |
return new MemoryStream(_viewContent); | |
} | |
private void GetView(string connection, string viewPath) | |
{ | |
var query = @"SELECT Content, LastModified FROM Views WHERE Location = @Path; | |
UPDATE Views SET LastRequested = GetUtcDate() WHERE Location = @Path"; | |
try | |
{ | |
using (var conn = new SqlConnection(connection)) | |
using (var cmd = new SqlCommand(query, conn)) | |
{ | |
cmd.Parameters.AddWithValue("@Path", viewPath); | |
conn.Open(); | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
_exists = reader.HasRows; | |
if (_exists) | |
{ | |
reader.Read(); | |
_viewContent = Encoding.UTF8.GetBytes(reader["Content"].ToString()); | |
_lastModified = Convert.ToDateTime(reader["LastModified"]); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
// if something went wrong, Exists will be false | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment