3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using System . Threading . Tasks ;
6
+ using GitAttributesWeb . Utils ;
6
7
using Microsoft . AspNet . Hosting ;
7
8
using Microsoft . AspNet . Mvc ;
8
9
using Microsoft . Framework . Runtime ;
@@ -13,25 +14,20 @@ namespace GitAttributesWeb.Controllers
13
14
[ Route ( "api" ) ]
14
15
public class ApiController : Controller
15
16
{
16
- private readonly IHostingEnvironment env ;
17
+ private readonly AppData data ;
17
18
18
- public ApiController ( IHostingEnvironment env )
19
+ public ApiController ( AppData data )
19
20
{
20
- this . env = env ;
21
+ this . data = data ;
21
22
}
22
23
23
24
// GET: api/list
24
25
[ HttpGet ]
25
26
[ Route ( "list" ) ]
26
27
public IEnumerable < string > Get ( )
27
28
{
28
- var dataPath = Path . Combine ( this . env . WebRootPath , "data" ) ;
29
- var files = PathResolver . PerformWildcardSearch ( dataPath , "*.gitattributes" ) ;
30
-
31
- var q = from file in files
32
- let name = Path . GetFileNameWithoutExtension ( file ) . ToLowerInvariant ( )
33
- orderby name
34
- select name ;
29
+ var q = from file in this . data . Files
30
+ select file . Id ;
35
31
36
32
return q . ToList ( ) ;
37
33
}
@@ -41,12 +37,8 @@ orderby name
41
37
[ Route ( "{id}" ) ]
42
38
public IActionResult Get ( string id )
43
39
{
44
- var dataPath = Path . Combine ( this . env . WebRootPath , "data" ) ;
45
- var files = PathResolver . PerformWildcardSearch ( dataPath , "*.gitattributes" ) ;
46
-
47
- var q = from file in files
48
- let name = Path . GetFileNameWithoutExtension ( file ) . ToLowerInvariant ( )
49
- where name == id
40
+ var q = from file in this . data . Files
41
+ where file . Id == id
50
42
select file ;
51
43
52
44
var validFile = q . FirstOrDefault ( ) ;
@@ -55,7 +47,7 @@ public IActionResult Get(string id)
55
47
return new NoContentResult ( ) ;
56
48
}
57
49
58
- string content = System . IO . File . ReadAllText ( validFile ) ;
50
+ string content = System . IO . File . ReadAllText ( validFile . Path ) ;
59
51
60
52
return Content ( content ) ;
61
53
}
@@ -65,12 +57,8 @@ public IActionResult Get(string id)
65
57
[ Route ( "f/{id}" ) ]
66
58
public IActionResult GetFile ( string id )
67
59
{
68
- var dataPath = Path . Combine ( this . env . WebRootPath , "data" ) ;
69
- var files = PathResolver . PerformWildcardSearch ( dataPath , "*.gitattributes" ) ;
70
-
71
- var q = from file in files
72
- let name = Path . GetFileNameWithoutExtension ( file ) . ToLowerInvariant ( )
73
- where name == id
60
+ var q = from file in this . data . Files
61
+ where file . Id == id
74
62
select file ;
75
63
76
64
var validFile = q . FirstOrDefault ( ) ;
@@ -79,7 +67,7 @@ public IActionResult GetFile(string id)
79
67
return new NoContentResult ( ) ;
80
68
}
81
69
82
- var content = System . IO . File . ReadAllBytes ( validFile ) ;
70
+ var content = System . IO . File . ReadAllBytes ( validFile . Path ) ;
83
71
84
72
return File ( content , contentType : "text/plain" , fileDownloadName : "gitattributes" ) ;
85
73
}
0 commit comments