1+ /*
2+ * Copyright (c) 2014-2015, Facebook, Inc.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-style license found in the
6+ * LICENSE file in the root directory of this source tree. An additional grant
7+ * of patent rights can be found in the PATENTS file in the same directory.
8+ */
9+
10+ using System ;
11+ using System . Collections . Generic ;
12+ using System . IO ;
13+ using System . Linq ;
14+ using System . Text ;
15+
16+ using Microsoft . Owin . FileSystems ;
17+
18+ namespace React . Owin
19+ {
20+ /// <summary>
21+ /// Owin file system that serves transformed JSX files.
22+ /// </summary>
23+ internal class JsxFileSystem : Microsoft . Owin . FileSystems . IFileSystem
24+ {
25+ private readonly IJsxTransformer _transformer ;
26+ private readonly Microsoft . Owin . FileSystems . IFileSystem _physicalFileSystem ;
27+ private readonly string [ ] _extenstions ;
28+
29+ public JsxFileSystem ( IJsxTransformer transformer , string root , IEnumerable < string > extenstions )
30+ : this ( transformer , new PhysicalFileSystem ( root ) , extenstions )
31+ {
32+ }
33+
34+ public JsxFileSystem ( IJsxTransformer transformer , Microsoft . Owin . FileSystems . IFileSystem fileSystem , IEnumerable < string > extenstions )
35+ {
36+ _transformer = transformer ;
37+ _physicalFileSystem = fileSystem ;
38+
39+ // Make sure the extensions start with dot
40+ _extenstions = extenstions . Select ( extenstion => extenstion . StartsWith ( "." ) ? extenstion : "." + extenstion ) . ToArray ( ) ;
41+ }
42+
43+ public bool TryGetFileInfo ( string subpath , out IFileInfo fileInfo )
44+ {
45+ IFileInfo internalFileInfo ;
46+ fileInfo = null ;
47+
48+ if ( ! _physicalFileSystem . TryGetFileInfo ( subpath , out internalFileInfo ) )
49+ return false ;
50+
51+ if ( internalFileInfo . IsDirectory || ! _extenstions . Any ( internalFileInfo . Name . EndsWith ) )
52+ return false ;
53+
54+ fileInfo = new JsxFileInfo ( _transformer , internalFileInfo ) ;
55+ return true ;
56+ }
57+
58+ public bool TryGetDirectoryContents ( string subpath , out IEnumerable < IFileInfo > contents )
59+ {
60+ return _physicalFileSystem . TryGetDirectoryContents ( subpath , out contents ) ;
61+ }
62+
63+ private class JsxFileInfo : IFileInfo
64+ {
65+ private readonly IJsxTransformer _jsxTransformer ;
66+ private readonly IFileInfo _fileInfo ;
67+ private readonly Lazy < byte [ ] > _content ;
68+
69+ public JsxFileInfo ( IJsxTransformer jsxTransformer , IFileInfo fileInfo )
70+ {
71+ _jsxTransformer = jsxTransformer ;
72+ _fileInfo = fileInfo ;
73+
74+ _content = new Lazy < byte [ ] > (
75+ ( ) =>
76+ {
77+ return Encoding . UTF8 . GetBytes ( _jsxTransformer . TransformJsxFile ( fileInfo . PhysicalPath ) ) ;
78+ } ) ;
79+ }
80+
81+ public Stream CreateReadStream ( )
82+ {
83+ return new MemoryStream ( _content . Value ) ;
84+ }
85+
86+ public long Length
87+ {
88+ get { return _content . Value . Length ; }
89+ }
90+
91+ public string PhysicalPath
92+ {
93+ get { return _fileInfo . PhysicalPath ; }
94+ }
95+
96+ public string Name
97+ {
98+ get { return _fileInfo . Name ; }
99+ }
100+
101+ public DateTime LastModified
102+ {
103+ get { return _fileInfo . LastModified ; }
104+ }
105+
106+ public bool IsDirectory
107+ {
108+ get { return _fileInfo . IsDirectory ; }
109+ }
110+ }
111+ }
112+ }
0 commit comments