瀏覽代碼

added action for raw files; added test app config dbo; removed use static files

tags/v0.5.2
Robin Thoni 8 年之前
父節點
當前提交
79c42335a3

+ 30
- 2
Luticate2.Utils/Controllers/LuFsFilesCrudController.cs 查看文件

@@ -1,4 +1,5 @@
1 1
 using System.ComponentModel.DataAnnotations;
2
+using System.IO;
2 3
 using Luticate2.Utils.Dbo.Basic;
3 4
 using Luticate2.Utils.Dbo.FsFiles;
4 5
 using Luticate2.Utils.Dbo.PaginatedRequest;
@@ -6,20 +7,47 @@ using Luticate2.Utils.Interfaces;
6 7
 using Luticate2.Utils.Utils;
7 8
 using Microsoft.AspNetCore.Http;
8 9
 using Microsoft.AspNetCore.Mvc;
10
+using Microsoft.AspNetCore.StaticFiles;
9 11
 using Microsoft.Extensions.Options;
10 12
 
11 13
 namespace Luticate2.Utils.Controllers
12 14
 {
13
-    public class LuFsFilesCrudController<TBusiness> : LuController
15
+    public abstract class LuFsFilesCrudController<TBusiness> : LuController
14 16
         where TBusiness : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
15 17
     {
16 18
         protected readonly TBusiness Business;
17 19
 
18
-        public LuFsFilesCrudController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, TBusiness business) : base(luUtilsOptionsDbo)
20
+        protected LuFsFilesCrudController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, TBusiness business) : base(luUtilsOptionsDbo)
19 21
         {
20 22
             Business = business;
21 23
         }
22 24
 
25
+        protected abstract string GetBasePath();
26
+
27
+        [HttpGet]
28
+        [Route("[controller]/{id}/raw")]
29
+        public virtual IActionResult GetSingleByIdRaw([Required]string id, bool forceDownload = false)
30
+        {
31
+            var get = Business.GetSingleById(id);
32
+            if (!get)
33
+            {
34
+                throw new LuResultException(get.To<object>());
35
+            }
36
+            string type;
37
+            var contentTypeProvider = new FileExtensionContentTypeProvider();
38
+            if (!contentTypeProvider.TryGetContentType(id, out type))
39
+            {
40
+                type = "application/octet-stream";
41
+            }
42
+            var fullPath = Path.Combine(new FileInfo(GetBasePath()).FullName, get.Data.Path);
43
+            var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
44
+            if (forceDownload)
45
+            {
46
+                return File(stream, type, get.Data.Path);
47
+            }
48
+            return new FileStreamResult(stream, type);
49
+        }
50
+
23 51
         [HttpGet]
24 52
         [Route("[controller]/{id}")]
25 53
         public virtual LuApiWrapperDbo<LuFsFilesDbo> GetSingleById([Required]string id)

+ 2
- 1
Luticate2.Utils/project.json 查看文件

@@ -13,7 +13,8 @@
13 13
     "Microsoft.AspNetCore.WebSockets": "1.0.0",
14 14
     "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
15 15
     "NETStandard.Library": "1.6.1",
16
-    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0"
16
+    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0",
17
+    "Microsoft.AspNetCore.StaticFiles": "1.1.0"
17 18
   },
18 19
   "frameworks": {
19 20
     "netstandard1.6": {

+ 11
- 1
WebApiUtils/Controllers/UploadController.cs 查看文件

@@ -2,13 +2,23 @@
2 2
 using Luticate2.Utils.Dbo.Basic;
3 3
 using Microsoft.Extensions.Options;
4 4
 using WebApiUtils.Business;
5
+using WebApiUtils.Dbo;
5 6
 
6 7
 namespace WebApiUtils.Controllers
7 8
 {
8 9
     public class UploadController : LuFsFilesCrudController<UploadBusiness>
9 10
     {
10
-        public UploadController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, UploadBusiness business) : base(luUtilsOptionsDbo, business)
11
+        private readonly AppConfigDbo _options;
12
+
13
+        public UploadController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo, IOptions<AppConfigDbo> options,
14
+            UploadBusiness business) : base(luUtilsOptionsDbo, business)
15
+        {
16
+            _options = options.Value;
17
+        }
18
+
19
+        protected override string GetBasePath()
11 20
         {
21
+            return _options.UploadDir;
12 22
         }
13 23
     }
14 24
 }

+ 10
- 1
WebApiUtils/DataAccess/UploadDataAccess.cs 查看文件

@@ -1,12 +1,21 @@
1 1
 using Luticate2.Utils.DataAccess;
2
+using Microsoft.Extensions.Options;
3
+using WebApiUtils.Dbo;
2 4
 
3 5
 namespace WebApiUtils.DataAccess
4 6
 {
5 7
     public class UploadDataAccess : LuFsFilesCrudDataAccess
6 8
     {
9
+        private readonly AppConfigDbo _options;
10
+
11
+        public UploadDataAccess(IOptions<AppConfigDbo> options)
12
+        {
13
+            _options = options.Value;
14
+        }
15
+
7 16
         public override string GetBasePath()
8 17
         {
9
-            return "/tmp/luticate2/";
18
+            return _options.UploadDir;
10 19
         }
11 20
     }
12 21
 }

+ 7
- 0
WebApiUtils/Dbo/AppConfigDbo.cs 查看文件

@@ -0,0 +1,7 @@
1
+namespace WebApiUtils.Dbo
2
+{
3
+    public class AppConfigDbo
4
+    {
5
+        public string UploadDir { get; set; }
6
+    }
7
+}

+ 15
- 7
WebApiUtils/Startup.cs 查看文件

@@ -11,6 +11,7 @@ using Microsoft.Extensions.Logging;
11 11
 using TestUtils.DataAccess;
12 12
 using WebApiUtils.Business;
13 13
 using WebApiUtils.DataAccess;
14
+using WebApiUtils.Dbo;
14 15
 
15 16
 namespace WebApiUtils
16 17
 {
@@ -47,7 +48,14 @@ namespace WebApiUtils
47 48
             services.AddScoped<LuUtilsPkGuidDataAccess>();
48 49
             services.AddScoped<UploadBusiness>();
49 50
             services.AddScoped<UploadDataAccess>();
50
-            services.AddDbContext<LuUtilsDbContext>(options =>
51
+
52
+            services.Configure<AppConfigDbo>(dbo =>
53
+            {
54
+                dbo.UploadDir = "/tmp/luticate2";
55
+                Directory.CreateDirectory(dbo.UploadDir);
56
+            });
57
+
58
+                services.AddDbContext<LuUtilsDbContext>(options =>
51 59
             {
52 60
                 options.UseNpgsql(Configuration.GetConnectionString("default"));
53 61
                 options.UseInternalServiceProvider(new ServiceCollection()
@@ -69,12 +77,12 @@ namespace WebApiUtils
69 77
 //            app.UseApplicationInsightsRequestTelemetry();
70 78
 //
71 79
 //            app.UseApplicationInsightsExceptionTelemetry();
72
-            app.UseStaticFiles(new StaticFileOptions
73
-            {
74
-                ServeUnknownFileTypes = true,
75
-                FileProvider = new PhysicalFileProvider("/tmp/luticate2"),
76
-                RequestPath = new PathString("/uploadraw")
77
-            });
80
+//            app.UseStaticFiles(new StaticFileOptions
81
+//            {
82
+//                ServeUnknownFileTypes = true,
83
+//                FileProvider = new PhysicalFileProvider("/tmp/luticate2"),
84
+//                RequestPath = new PathString("/uploadraw")
85
+//            });
78 86
 
79 87
             app.UseLuticateUtils();
80 88
 

+ 0
- 1
WebApiUtils/project.json 查看文件

@@ -13,7 +13,6 @@
13 13
         "Microsoft.Extensions.Logging.Console": "1.1.0",
14 14
         "Microsoft.Extensions.Logging.Debug": "1.1.0",
15 15
         "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
16
-        "Microsoft.AspNetCore.StaticFiles": "1.1.0",
17 16
         "Microsoft.NETCore.App": {
18 17
             "version": "1.0.1",
19 18
             "type": "platform"

Loading…
取消
儲存