黄贺彬 6 vuotta sitten
vanhempi
commit
9a2f42ce71

+ 29 - 13
HaBookCms.AzureBlobStorageTest/Controllers/ValuesController.cs

@@ -5,6 +5,11 @@ using System.Linq;
 using System.Net.Http.Headers;
 using System.Threading.Tasks;
 using HaBookCms.AzureBlobStorageTest.Models;
+using HaBookCms.AzureStorage.AzureBlob.Container;
+using HaBookCms.AzureStorage.AzureBlob.Implements;
+using HaBookCms.AzureStorage.AzureBlob.Interfaces;
+using HaBookCms.AzureStorage.AzureBlob.Models;
+using HaBookCms.AzureStorage.AzureTable.Interfaces;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.WindowsAzure.Storage;
@@ -16,6 +21,14 @@ namespace HaBookCms.AzureBlobStorageTest.Controllers
     [ApiController]
     public class ValuesController : ControllerBase
     {
+        public IAzureBlobDBRepository _azureBlobDBRepository;
+        public IAzureTableDBRepository  _azureTableDBRepository;
+        public ValuesController(IAzureBlobDBRepository azureBlobDBRepository, IAzureTableDBRepository azureTableDBRepository) {
+            _azureBlobDBRepository = azureBlobDBRepository;
+            _azureTableDBRepository = azureTableDBRepository;
+        }
+
+
         // GET api/values
         [HttpGet]
         public ActionResult<IEnumerable<string>> Get()
@@ -65,20 +78,23 @@ namespace HaBookCms.AzureBlobStorageTest.Controllers
         [HttpPost("Upload")]
         //[Produces("application/json")]
         //[Consumes("application/json", "multipart/form-data")]//此处为新增
-        public async Task<IActionResult> Upload(IFormFile[] file  )
+        public async Task<List<AzureBlobModel>> Upload(IFormFile[] file  )
         {
-            var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn");
-            var blobClient = storageAccount.CreateCloudBlobClient();
-            var container = blobClient.GetContainerReference("staticcontent");
-            //var file = picture.File;
-            foreach (IFormFile f in file) {
-                var parsedContentDisposition =
-                ContentDispositionHeaderValue.Parse(f.ContentDisposition);
-                var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
-                var blockBlob = container.GetBlockBlobReference(filename+Guid.NewGuid());
-                await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
-            }
-            return Ok();
+
+            //var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn");
+            //var blobClient = storageAccount.CreateCloudBlobClient();
+            //var container = blobClient.GetContainerReference("staticcontent");
+            ////var file = picture.File;
+            //foreach (IFormFile f in file) {
+            //    var parsedContentDisposition =
+            //    ContentDispositionHeaderValue.Parse(f.ContentDisposition);
+            //    var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
+            //    var blockBlob = container.GetBlockBlobReference(filename+Guid.NewGuid());
+            //    await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
+            //}
+           List<AzureBlobModel> blobModels=   _azureBlobDBRepository.UploadFiles(file, new FileContainer()).Result.ToList<AzureBlobModel>();
+            await _azureTableDBRepository.SaveAll<AzureBlobModel>(blobModels);
+            return blobModels;
         }
         [HttpGet("Download")]
         //[Produces("application/json")]

+ 4 - 0
HaBookCms.AzureBlobStorageTest/HaBookCms.AzureBlobStorageTest.csproj

@@ -11,4 +11,8 @@
     <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
   </ItemGroup>
 
+  <ItemGroup>
+    <ProjectReference Include="..\HaBookCms.AzureStorage\HaBookCms.AzureStorage.csproj" />
+  </ItemGroup>
+
 </Project>

+ 8 - 0
HaBookCms.AzureBlobStorageTest/Startup.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
+using HaBookCms.AzureStorage.ServiceExtension;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.HttpsPolicy;
@@ -31,6 +32,13 @@ namespace HaBookCms.AzureBlobStorageTest
         public void ConfigureServices(IServiceCollection services)
         {
             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
+            services.AddAzureBlobStorage().AddConnection(new AzureStorageOptions() {
+                ConnectionString=Configuration["AppSettings:Azure:StorageConnection"]
+            });
+            services.AddAzureTableStorage().AddConnection(new AzureStorageOptions()
+            {
+                ConnectionString = Configuration["AppSettings:Azure:StorageConnection"]
+            });
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

+ 19 - 1
HaBookCms.AzureBlobStorageTest/appsettings.json

@@ -5,5 +5,23 @@
       "Default": "Warning"
     }
   },
-  "AllowedHosts": "*"
+  "AllowedHosts": "*",
+  "AppSettings": {
+    //Redis»º´æ
+    "RedisCaching": {
+      "KeyPrefix": "habook:cms",
+      "Enabled": true,
+      "DataBase": 1,
+      "ConnectionString": "teammodel.redis.cache.chinacloudapi.cn:6380,password=9wcTVDiAWiAf0IF5NOjZBvYrZe9JlbEw1E3F1QFcNbo=,ssl=True,abortConnect=False"
+    },
+    //Êý¾Ý¿âÅäÖÃ
+    "Azure": {
+      "StorageConnection": "DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn",
+      "CosmosDBConnection": {
+        "AccountEndpoint": "https://teammodelostest.documents.azure.cn:443/",
+        "AccountKey": "ReGoiHuTbU4Q31YYq4NaiormE6Ci71piT7OrvTzAuhrlgt63ajdtDZmwOZKzcz6gnwR326mJp53InY7rohepQQ==",
+        "Database": "BaseActivty"
+      }
+    }
+  }
 }

+ 0 - 2
HaBookCms.AzureStorage/AzureBlob/BlobClientSingleton.cs

@@ -13,8 +13,6 @@ namespace HaBookCms.AzureStorage.AzureBlob
 
         private BlobClientSingleton() { }
 
-
-
         public CloudBlobClient GetBlobClient()
         {
             if (BlobClient != null)

+ 1 - 1
HaBookCms.AzureStorage/AzureBlob/Container/BlobContainer.cs

@@ -6,6 +6,6 @@ namespace HaBookCms.AzureStorage.AzureBlob.Container
 {
     public abstract class BlobContainer
     {
-        public string Container { get; private set; } = "DefaultContainer";
+        public string Container { get; private set; } = "defaultcontainer";
     }
 }

+ 1 - 1
HaBookCms.AzureStorage/AzureBlob/Container/FileContainer.cs

@@ -6,6 +6,6 @@ namespace HaBookCms.AzureStorage.AzureBlob.Container
 {
     public class FileContainer :BlobContainer
     {
-        public string Container { get; private set; } = "FileContainer";
+        public string Container { get; private set; } = "filecontainer";
     }
 }

+ 1 - 1
HaBookCms.AzureStorage/AzureBlob/Container/ImageContainer.cs

@@ -6,6 +6,6 @@ namespace HaBookCms.AzureStorage.AzureBlob.Container
 {
    public class ImageContainer :BlobContainer
     {
-        public string Container { get; private set; } = "ImageContainer";
+        public string Container { get; private set; } = "imagecontainer";
     }
 }

+ 11 - 8
HaBookCms.AzureStorage/AzureBlob/Implements/AzureBlobDBRepository.cs

@@ -1,5 +1,6 @@
 using HaBookCms.AzureStorage.AzureBlob.Container;
 using HaBookCms.AzureStorage.AzureBlob.Interfaces;
+using HaBookCms.AzureStorage.AzureBlob.Models;
 using HaBookCms.AzureStorage.ServiceExtension;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Options;
@@ -37,21 +38,23 @@ namespace HaBookCms.AzureStorage.AzureBlob.Implements
                 blobContainer = blobClient.GetContainerReference(container);
                 await blobContainer.CreateIfNotExistsAsync();
             }
-           await UploadFiles(null, new FileContainer() );
+           //await UploadFiles(null, new FileContainer() );
         }
-        public async Task<IEnumerable<string>> UploadFiles(IFormFile[] file, BlobContainer container) {
+
+     public async Task<List<AzureBlobModel>> UploadFiles(IFormFile[] file, BlobContainer container)
+        {
             await InitializeBlob(container.Container);
-            IEnumerable<string> list = new List<string>();
+            List<AzureBlobModel> list = new List<AzureBlobModel>();
             foreach (IFormFile f in file)
             {
-                var parsedContentDisposition =
-                ContentDispositionHeaderValue.Parse(f.ContentDisposition);
+                var parsedContentDisposition = ContentDispositionHeaderValue.Parse(f.ContentDisposition);
                 var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
-                var blockBlob = blobContainer.GetBlockBlobReference(f.FileName + Guid.NewGuid());
+                var blockBlob = blobContainer.GetBlockBlobReference(f.FileName );
                 await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
+                AzureBlobModel model = new AzureBlobModel(f, container.Container);
+                list.Add(model);
             }
-            return null;
+            return list;
         }
-
     }
 }

+ 5 - 2
HaBookCms.AzureStorage/AzureBlob/Interfaces/IAzureBlobDBRepository.cs

@@ -1,10 +1,13 @@
-using System;
+using HaBookCms.AzureStorage.AzureBlob.Container;
+using HaBookCms.AzureStorage.AzureBlob.Models;
+using Microsoft.AspNetCore.Http;
 using System.Collections.Generic;
-using System.Text;
+using System.Threading.Tasks;
 
 namespace HaBookCms.AzureStorage.AzureBlob.Interfaces
 {
     public interface  IAzureBlobDBRepository
     {
+        Task<List<AzureBlobModel>> UploadFiles(IFormFile[] file, BlobContainer container);
     }
 }

+ 28 - 10
HaBookCms.AzureStorage/AzureBlob/Models/AzureBlobModel.cs

@@ -1,37 +1,55 @@
 using Microsoft.AspNetCore.Http;
+using Microsoft.WindowsAzure.Storage.Table;
 using System;
-using System.Collections.Generic;
-using System.Text;
 
 namespace HaBookCms.AzureStorage.AzureBlob.Models
 {
-    public  class AzureBlobModel
+    public  class AzureBlobModel :TableEntity
     {
+
+
+        public AzureBlobModel() {
+        }
+
+        public AzureBlobModel(IFormFile f ,string Container) {
+            ContentType = f.ContentType;
+            ContentDisposition = f.ContentDisposition;
+            FileName = f.FileName;
+            Headers = f.Headers;
+            Length = f.Length;
+            Name = f.Name;
+            UploadTime = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
+            PartitionKey = Container;
+            RowKey = Guid.NewGuid().ToString();
+            Extension = f.FileName.Substring(f.FileName.LastIndexOf(".") + 1, (f.FileName.Length - f.FileName.LastIndexOf(".") - 1)); //扩展名
+        }
         //
         // 摘要:
         //     Gets the raw Content-Type header of the uploaded file.
-        string ContentType { get; }
+        public   string ContentType { get; set; }
         //
         // 摘要:
         //     Gets the raw Content-Disposition header of the uploaded file.
-        string ContentDisposition { get; }
+        public string ContentDisposition { get; set; }
         //
         // 摘要:
         //     Gets the header dictionary of the uploaded file.
-        IHeaderDictionary Headers { get; }
+        public IHeaderDictionary Headers { get; set; }
         //
         // 摘要:
         //     Gets the file length in bytes.
-        long Length { get; }
+        public long Length { get; set; }
         //
         // 摘要:
         //     Gets the form field name from the Content-Disposition header.
-        string Name { get; }
+        public string Name { get; set; }
         //
         // 摘要:
         //     Gets the file name from the Content-Disposition header.
-        string FileName { get; }
+        public string FileName { get; set; }
+        //上传时间戳
+        public long UploadTime { get; set; }
         //上传时间戳
-        long UploadTime { get; set; } = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
+        public string Extension { get; set; }
     }
 }

+ 8 - 2
HaBookCms.AzureStorage/ServiceExtension/AzureStorageServiceCollectionExtensions.cs

@@ -9,6 +9,7 @@ namespace HaBookCms.AzureStorage.ServiceExtension
 {
     public static class AzureStorageServiceCollectionExtensions
     {
+        public static AzureStorageServiceBuilder builder { get; set; }
 
         /// <summary>
         /// 
@@ -27,7 +28,9 @@ namespace HaBookCms.AzureStorage.ServiceExtension
         /// <returns></returns>
         public static AzureStorageServiceBuilder AddAzureTableStorage(this IServiceCollection services)
         {
-            var builder = services.AddServerBuilder();
+            if (builder == null) {
+                builder = services.AddServerBuilder();
+            }
             services.AddSingleton<IAzureTableDBRepository, AzureTableDBRepository>();
             return builder;
         }
@@ -51,7 +54,10 @@ namespace HaBookCms.AzureStorage.ServiceExtension
         /// <returns></returns>
         public static AzureStorageServiceBuilder AddAzureBlobStorage(this IServiceCollection services)
         {
-            var builder = services.AddServerBuilder();
+            if (builder == null)
+            {
+                builder = services.AddServerBuilder();
+            }
             services.AddSingleton<IAzureBlobDBRepository, AzureBlobDBRepository>();
             return builder;
         }

+ 24 - 4
HaBookCms.Contest/Controllers/SyllabusController.cs

@@ -24,17 +24,37 @@ namespace HaBookCms.Contest.Controllers
         [Route("save")]
         [EnableCors("any")]
         [AllowAnonymous]
-        public async Task<object> Save(SyllabusRoot root) {
-            return await _aAzureCosmosDBRepository.Save<SyllabusRoot>(root);
+        public async Task<object> Save(Syllabus root) {
+            return await _aAzureCosmosDBRepository.Save<Syllabus>(root);
         }
 
         [HttpGet]
         [Route("getById")]
         [EnableCors("any")]
         [AllowAnonymous]
-        public object GetById()
+        public object GetById(string id )
         {
-            return _aAzureCosmosDBRepository.FindAll<SyllabusRoot>().Result.FirstOrDefault();
+            var ss = _aAzureCosmosDBRepository.FindAll<Syllabus>().Result.Where(s => s.id == id).ToList().SingleOrDefault();
+            if (ss != null) {
+                return ss;
+            }
+            else { return "";  }
+             
+        }
+
+        [HttpGet]
+        [Route("getAll")]
+        [EnableCors("any")]
+        [AllowAnonymous]
+        public object GetAll()
+        {
+            List<Syllabus>syllabi=   _aAzureCosmosDBRepository.FindAll<Syllabus>().Result;
+
+            if (syllabi != null)
+            {
+                return syllabi;
+            }
+            else { return ""; }
         }
     }
 }

+ 0 - 1
HaBookCms.Contest/Startup.cs

@@ -24,7 +24,6 @@ using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.HttpOverrides;
 using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.SpaServices.Webpack;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.IdentityModel.Tokens;

+ 1 - 1
HaBookCms.Core/Models/Syllabus/SyllabusRoot.cs

@@ -4,7 +4,7 @@ using System.Text;
 
 namespace HaBookCms.Core.Models.Syllabus
 {
-    public class SyllabusRoot : SyllabusNode
+    public class Syllabus : SyllabusNode
     {
         public SyllabusNode root { get; set; }
     }