From 01b821c3c60d4c39bb0e36c8ed6477b7f79fbb50 Mon Sep 17 00:00:00 2001 From: feijie Date: Thu, 18 Apr 2019 10:35:22 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E9=80=82=E7=94=A8?= =?UTF-8?q?=E4=BA=8Emysql=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ImageApi/Dockerfile | 8 +++---- ImageApi/Dockerfile1.original | 24 +++++++++++++++++++ ImageApi/ImageApi.csproj | 2 +- ImageApi/Program.cs | 2 ++ .../PublishProfiles/FolderProfile.pubxml | 3 +++ .../PublishProfiles/FolderProfile1.pubxml | 22 +++++++++++++++++ ImageApi/Startup.cs | 23 +++++++----------- ImageApi/appsettings.json | 4 ++++ ImageCore/ImageOption.cs | 4 ++-- ...ageCore.Persistence.EntityFramework.csproj | 2 ++ ....cs => 20190417083731_initial.Designer.cs} | 11 ++++----- ...30_Inital.cs => 20190417083731_initial.cs} | 5 ++-- .../Migrations/ImageDbContextModelSnapshot.cs | 7 ++---- 13 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 ImageApi/Dockerfile1.original create mode 100644 ImageApi/Properties/PublishProfiles/FolderProfile1.pubxml rename Persistence/ImageCore.Persistence.EntityFramework/Migrations/{20190301113830_Inital.Designer.cs => 20190417083731_initial.Designer.cs} (83%) rename Persistence/ImageCore.Persistence.EntityFramework/Migrations/{20190301113830_Inital.cs => 20190417083731_initial.cs} (92%) diff --git a/ImageApi/Dockerfile b/ImageApi/Dockerfile index c52689d..86e47b7 100644 --- a/ImageApi/Dockerfile +++ b/ImageApi/Dockerfile @@ -1,15 +1,13 @@ -#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed. -#For more information, please see https://aka.ms/containercompat - -FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base +FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base WORKDIR /app EXPOSE 80 -FROM microsoft/dotnet:2.2-sdk-nanoserver-1803 AS build +FROM microsoft/dotnet:2.2-sdk AS build WORKDIR /src COPY ["ImageApi/ImageApi.csproj", "ImageApi/"] COPY ["ImageApi.Core/ImageApi.Core.csproj", "ImageApi.Core/"] COPY ["ImageCore/ImageCore.csproj", "ImageCore/"] +COPY ["Persistence/ImageCore.Persistence.EntityFramework/ImageCore.Persistence.EntityFramework.csproj", "Persistence/ImageCore.Persistence.EntityFramework/"] RUN dotnet restore "ImageApi/ImageApi.csproj" COPY . . WORKDIR "/src/ImageApi" diff --git a/ImageApi/Dockerfile1.original b/ImageApi/Dockerfile1.original new file mode 100644 index 0000000..c52689d --- /dev/null +++ b/ImageApi/Dockerfile1.original @@ -0,0 +1,24 @@ +#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed. +#For more information, please see https://aka.ms/containercompat + +FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base +WORKDIR /app +EXPOSE 80 + +FROM microsoft/dotnet:2.2-sdk-nanoserver-1803 AS build +WORKDIR /src +COPY ["ImageApi/ImageApi.csproj", "ImageApi/"] +COPY ["ImageApi.Core/ImageApi.Core.csproj", "ImageApi.Core/"] +COPY ["ImageCore/ImageCore.csproj", "ImageCore/"] +RUN dotnet restore "ImageApi/ImageApi.csproj" +COPY . . +WORKDIR "/src/ImageApi" +RUN dotnet build "ImageApi.csproj" -c Release -o /app + +FROM build AS publish +RUN dotnet publish "ImageApi.csproj" -c Release -o /app + +FROM base AS final +WORKDIR /app +COPY --from=publish /app . +ENTRYPOINT ["dotnet", "ImageApi.dll"] \ No newline at end of file diff --git a/ImageApi/ImageApi.csproj b/ImageApi/ImageApi.csproj index bf2c7f6..84202f9 100644 --- a/ImageApi/ImageApi.csproj +++ b/ImageApi/ImageApi.csproj @@ -3,7 +3,7 @@ netcoreapp2.2 InProcess - Windows + Linux diff --git a/ImageApi/Program.cs b/ImageApi/Program.cs index a743a1f..65862ca 100644 --- a/ImageApi/Program.cs +++ b/ImageApi/Program.cs @@ -19,6 +19,8 @@ public static void Main(string[] args) public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseUrls("http://*:8089") .UseStartup(); } } diff --git a/ImageApi/Properties/PublishProfiles/FolderProfile.pubxml b/ImageApi/Properties/PublishProfiles/FolderProfile.pubxml index 2500033..031a5e7 100644 --- a/ImageApi/Properties/PublishProfiles/FolderProfile.pubxml +++ b/ImageApi/Properties/PublishProfiles/FolderProfile.pubxml @@ -15,5 +15,8 @@ 2528c869-0822-49b2-ba00-6beebc316244 D:\wwwroot\ImageService False + netcoreapp2.2 + false + <_IsPortable>true \ No newline at end of file diff --git a/ImageApi/Properties/PublishProfiles/FolderProfile1.pubxml b/ImageApi/Properties/PublishProfiles/FolderProfile1.pubxml new file mode 100644 index 0000000..444f409 --- /dev/null +++ b/ImageApi/Properties/PublishProfiles/FolderProfile1.pubxml @@ -0,0 +1,22 @@ + + + + + FileSystem + FileSystem + Release + Any CPU + + True + False + netcoreapp2.2 + 2528c869-0822-49b2-ba00-6beebc316244 + false + <_IsPortable>true + D:\wwwroot\ImageService_Demo + False + + \ No newline at end of file diff --git a/ImageApi/Startup.cs b/ImageApi/Startup.cs index fe2eacd..2866a27 100644 --- a/ImageApi/Startup.cs +++ b/ImageApi/Startup.cs @@ -22,13 +22,13 @@ public class Startup { private const string DefaultCorsPolicyName = "public"; - private readonly IConfigurationRoot _appConfiguration; + private readonly IConfiguration configuration; private readonly IHostingEnvironment _hostingEnvironment; - public Startup(IHostingEnvironment env) + public Startup(IHostingEnvironment env, IConfiguration configuration) { _hostingEnvironment = env; - _appConfiguration = env.GetAppConfiguration(); + this.configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. @@ -39,13 +39,16 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton( new PhysicalFileProvider(Directory.GetCurrentDirectory())); services.AddSingleton(); + services.AddEntityFrameworkNpgsql(); services.AddImageService(option => { - option.Filters = new[] {".jpg", ".png", ".bmp"}; + option.Filters = new[] {".jpg", ".jpeg", ".gif", ".png", ".bmp"}; }) .UseEntityFrameworkStore(builder => { - builder.UseSqlServer("Server=localhost; Database=ImageDb; Trusted_Connection=True;"); + //builder.UseSqlServer("Server=localhost; Database=ImageDb; Trusted_Connection=True;"); + //builder.UseNpgsql(_appConfiguration.GetConnectionString("Default")); + builder.UseMySql(configuration.GetConnectionString("Mysql")); }); services.AddCors(options => { @@ -53,7 +56,7 @@ public void ConfigureServices(IServiceCollection services) { builder .WithOrigins( - _appConfiguration["CorsOrigins"] + configuration["CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .ToArray() ) @@ -84,12 +87,4 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) .UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "图片资源API"); }); } } - - public static class HostingEnvironmentExtensions - { - public static IConfigurationRoot GetAppConfiguration(this IHostingEnvironment env) - { - return AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName); - } - } } diff --git a/ImageApi/appsettings.json b/ImageApi/appsettings.json index 9c898f8..79ab510 100644 --- a/ImageApi/appsettings.json +++ b/ImageApi/appsettings.json @@ -1,4 +1,8 @@ { + "ConnectionStrings": { + //"Default": "Server=172.16.4.6;Port=5432;Database=ImageDb;User Id=postgres;Password=123456", + "Mysql": "Server=212.64.91.153;Port=3306;Database=ImageDb;User Id=root;Password=feijie999" + }, "Logging": { "LogLevel": { "Default": "Warning" diff --git a/ImageCore/ImageOption.cs b/ImageCore/ImageOption.cs index 3265512..1d4d3aa 100644 --- a/ImageCore/ImageOption.cs +++ b/ImageCore/ImageOption.cs @@ -12,9 +12,9 @@ public class ImageOption public string[] Filters { get; set; } = {".jpg", ".png", ".bmp"}; /// - /// 可接收文件最大的大小单位kb,默认为2MB + /// 可接收文件最大的大小单位kb,默认为5MB /// - public long MaxContentLength { get; set; } = 1024*1024 * 2; + public long MaxContentLength { get; set; } = 1024*1024 * 5; /// /// 文件存储模式,默认为分布式存储 diff --git a/Persistence/ImageCore.Persistence.EntityFramework/ImageCore.Persistence.EntityFramework.csproj b/Persistence/ImageCore.Persistence.EntityFramework/ImageCore.Persistence.EntityFramework.csproj index 12b9d5b..63b61f7 100644 --- a/Persistence/ImageCore.Persistence.EntityFramework/ImageCore.Persistence.EntityFramework.csproj +++ b/Persistence/ImageCore.Persistence.EntityFramework/ImageCore.Persistence.EntityFramework.csproj @@ -8,6 +8,8 @@ + + diff --git a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190301113830_Inital.Designer.cs b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.Designer.cs similarity index 83% rename from Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190301113830_Inital.Designer.cs rename to Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.Designer.cs index f011f2b..7db3b00 100644 --- a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190301113830_Inital.Designer.cs +++ b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.Designer.cs @@ -3,23 +3,21 @@ using ImageCore.Persistence.EntityFramework; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ImageCore.Persistence.EntityFramework.Migrations { [DbContext(typeof(ImageDbContext))] - [Migration("20190301113830_Inital")] - partial class Inital + [Migration("20190417083731_initial")] + partial class initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "2.2.2-servicing-10034") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("ImageCore.Persistence.EntityFramework.Image", b => { @@ -52,8 +50,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.HasIndex("Key") - .IsUnique() - .HasFilter("[Key] IS NOT NULL"); + .IsUnique(); b.ToTable("Images"); }); diff --git a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190301113830_Inital.cs b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.cs similarity index 92% rename from Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190301113830_Inital.cs rename to Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.cs index 109f587..48f532d 100644 --- a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190301113830_Inital.cs +++ b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.cs @@ -3,7 +3,7 @@ namespace ImageCore.Persistence.EntityFramework.Migrations { - public partial class Inital : Migration + public partial class initial : Migration { protected override void Up(MigrationBuilder migrationBuilder) { @@ -32,8 +32,7 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "IX_Images_Key", table: "Images", column: "Key", - unique: true, - filter: "[Key] IS NOT NULL"); + unique: true); } protected override void Down(MigrationBuilder migrationBuilder) diff --git a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs index 19693ae..13694e2 100644 --- a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs +++ b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs @@ -3,7 +3,6 @@ using ImageCore.Persistence.EntityFramework; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ImageCore.Persistence.EntityFramework.Migrations @@ -16,8 +15,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "2.2.2-servicing-10034") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("ImageCore.Persistence.EntityFramework.Image", b => { @@ -50,8 +48,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); b.HasIndex("Key") - .IsUnique() - .HasFilter("[Key] IS NOT NULL"); + .IsUnique(); b.ToTable("Images"); }); From bfa5a043f8038e14445945dc193fad826ca89f1d Mon Sep 17 00:00:00 2001 From: feijie Date: Fri, 26 Apr 2019 11:27:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djpeg=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ImageApi/Controllers/ImageController.cs | 2 +- ImageApi/ImageFileInfoExtensions.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ImageApi/Controllers/ImageController.cs b/ImageApi/Controllers/ImageController.cs index 3e89194..fa55bd5 100644 --- a/ImageApi/Controllers/ImageController.cs +++ b/ImageApi/Controllers/ImageController.cs @@ -51,7 +51,7 @@ public ImageController(IFileStore fileStore, IImageService imageService, ImageOp /// [Route("/img/{size}/t{imageType}t{yearMonth}-{id}.{format}")] [HttpGet] - [ResponseCache(Duration = 600, Location = ResponseCacheLocation.Any)] + [ResponseCache(Duration = 600, Location = ResponseCacheLocation.Client)] public async Task Index([FromServices] IImageParameterFixer parameterFixer, [FromRoute] ImageParameter parameter) { diff --git a/ImageApi/ImageFileInfoExtensions.cs b/ImageApi/ImageFileInfoExtensions.cs index 9136e2c..9dc0e7d 100644 --- a/ImageApi/ImageFileInfoExtensions.cs +++ b/ImageApi/ImageFileInfoExtensions.cs @@ -13,6 +13,7 @@ public static ImageDto ToImage(this ImageFileInfo fileInfo) switch (fileInfo.Extension) { case ".jpg": + case ".jpeg": imageFormat = ImageFormat.Jpeg; break; case ".gif": From f4bb8417405ee64d0cdd6a048bcc72e04a582f4b Mon Sep 17 00:00:00 2001 From: feijie Date: Wed, 5 Jun 2019 10:09:59 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8Linux=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E5=A4=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ImageApi.Core/ImageParameter.cs | 4 ++-- ImageApi/ImageFileInfoExtensions.cs | 2 +- ImageApi/Startup.cs | 4 ++-- ImageApi/SwaggerFileUploadFilter.cs | 5 +++-- ImageApi/appsettings.json | 3 +-- ...itial.Designer.cs => 20190426035517_initial.Designer.cs} | 6 ++++-- ...{20190417083731_initial.cs => 20190426035517_initial.cs} | 0 .../Migrations/ImageDbContextModelSnapshot.cs | 4 +++- 8 files changed, 16 insertions(+), 12 deletions(-) rename Persistence/ImageCore.Persistence.EntityFramework/Migrations/{20190417083731_initial.Designer.cs => 20190426035517_initial.Designer.cs} (89%) rename Persistence/ImageCore.Persistence.EntityFramework/Migrations/{20190417083731_initial.cs => 20190426035517_initial.cs} (100%) diff --git a/ImageApi.Core/ImageParameter.cs b/ImageApi.Core/ImageParameter.cs index 8568514..a6c5cbb 100644 --- a/ImageApi.Core/ImageParameter.cs +++ b/ImageApi.Core/ImageParameter.cs @@ -30,7 +30,7 @@ public ImageParameter GetFixed(IImageParameterFixer fixer) public string GetRelativePath() { - var folder = $"App_Data\\storage\\images\\{this.Year}\\{this.Month}\\{this.Id}\\"; + var folder = $"App_Data/storage/images/{this.Year}/{this.Month}/{this.Id}/"; if (this.ImageFormat == ImageFormat.Gif) { return folder + "Full.gif"; @@ -40,7 +40,7 @@ public string GetRelativePath() public string GetVirtualPath() { - return "~\\"+GetRelativePath(); + return "~/"+GetRelativePath(); } } } diff --git a/ImageApi/ImageFileInfoExtensions.cs b/ImageApi/ImageFileInfoExtensions.cs index 9dc0e7d..b5c4a14 100644 --- a/ImageApi/ImageFileInfoExtensions.cs +++ b/ImageApi/ImageFileInfoExtensions.cs @@ -10,7 +10,7 @@ public static class ImageFileInfoExtensions public static ImageDto ToImage(this ImageFileInfo fileInfo) { ImageFormat imageFormat; - switch (fileInfo.Extension) + switch (fileInfo.Extension.ToLower()) { case ".jpg": case ".jpeg": diff --git a/ImageApi/Startup.cs b/ImageApi/Startup.cs index 2866a27..f5aa59b 100644 --- a/ImageApi/Startup.cs +++ b/ImageApi/Startup.cs @@ -47,8 +47,8 @@ public void ConfigureServices(IServiceCollection services) .UseEntityFrameworkStore(builder => { //builder.UseSqlServer("Server=localhost; Database=ImageDb; Trusted_Connection=True;"); - //builder.UseNpgsql(_appConfiguration.GetConnectionString("Default")); - builder.UseMySql(configuration.GetConnectionString("Mysql")); + builder.UseNpgsql(configuration.GetConnectionString("Default")); + //builder.UseMySql(configuration.GetConnectionString("Mysql")); }); services.AddCors(options => { diff --git a/ImageApi/SwaggerFileUploadFilter.cs b/ImageApi/SwaggerFileUploadFilter.cs index c287c39..90006cb 100644 --- a/ImageApi/SwaggerFileUploadFilter.cs +++ b/ImageApi/SwaggerFileUploadFilter.cs @@ -20,9 +20,10 @@ public void Apply(Operation operation, OperationFilterContext context) } var fileParameters = context.ApiDescription.ActionDescriptor.Parameters - .Where(n => n.ParameterType == typeof(IFormFile)||n.BindingInfo.BindingSource == BindingSource.FormFile).ToList(); + .Where(n => n.ParameterType == typeof(IFormFile) || + (n.BindingInfo != null && n.BindingInfo.BindingSource == BindingSource.FormFile)).ToList(); - if (fileParameters.Count < 0) + if (fileParameters.Count == 0) { return; } diff --git a/ImageApi/appsettings.json b/ImageApi/appsettings.json index 79ab510..934feb0 100644 --- a/ImageApi/appsettings.json +++ b/ImageApi/appsettings.json @@ -1,7 +1,6 @@ { "ConnectionStrings": { - //"Default": "Server=172.16.4.6;Port=5432;Database=ImageDb;User Id=postgres;Password=123456", - "Mysql": "Server=212.64.91.153;Port=3306;Database=ImageDb;User Id=root;Password=feijie999" + "Default": "Server=172.16.4.6;Port=5432;Database=ImageDb;User Id=postgres;Password=123456" }, "Logging": { "LogLevel": { diff --git a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.Designer.cs b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190426035517_initial.Designer.cs similarity index 89% rename from Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.Designer.cs rename to Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190426035517_initial.Designer.cs index 7db3b00..ae6af73 100644 --- a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.Designer.cs +++ b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190426035517_initial.Designer.cs @@ -5,19 +5,21 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace ImageCore.Persistence.EntityFramework.Migrations { [DbContext(typeof(ImageDbContext))] - [Migration("20190417083731_initial")] + [Migration("20190426035517_initial")] partial class initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn) .HasAnnotation("ProductVersion", "2.2.2-servicing-10034") - .HasAnnotation("Relational:MaxIdentifierLength", 64); + .HasAnnotation("Relational:MaxIdentifierLength", 63); modelBuilder.Entity("ImageCore.Persistence.EntityFramework.Image", b => { diff --git a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.cs b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190426035517_initial.cs similarity index 100% rename from Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190417083731_initial.cs rename to Persistence/ImageCore.Persistence.EntityFramework/Migrations/20190426035517_initial.cs diff --git a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs index 13694e2..3dfff7c 100644 --- a/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs +++ b/Persistence/ImageCore.Persistence.EntityFramework/Migrations/ImageDbContextModelSnapshot.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace ImageCore.Persistence.EntityFramework.Migrations { @@ -14,8 +15,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn) .HasAnnotation("ProductVersion", "2.2.2-servicing-10034") - .HasAnnotation("Relational:MaxIdentifierLength", 64); + .HasAnnotation("Relational:MaxIdentifierLength", 63); modelBuilder.Entity("ImageCore.Persistence.EntityFramework.Image", b => { From 3bfaf899c2c55fc934c8596c78d93521bdce8dce Mon Sep 17 00:00:00 2001 From: feijie Date: Mon, 28 Oct 2019 15:43:29 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=9C=A8=E9=A1=B9=E7=9B=AE=E4=B8=AD?= =?UTF-8?q?=E5=8A=A0=E5=85=A5IdentityServer=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ImageApi/Controllers/ImageController.cs | 3 +++ ImageApi/ImageApi.csproj | 3 +++ .../PublishProfiles/FolderProfile2.pubxml | 19 ++++++++++++++ ImageApi/Startup.cs | 25 +++++++++++++++++++ ImageApi/appsettings.json | 6 ++++- 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 ImageApi/Properties/PublishProfiles/FolderProfile2.pubxml diff --git a/ImageApi/Controllers/ImageController.cs b/ImageApi/Controllers/ImageController.cs index fa55bd5..72a42f7 100644 --- a/ImageApi/Controllers/ImageController.cs +++ b/ImageApi/Controllers/ImageController.cs @@ -7,6 +7,7 @@ using ImageCore; using ImageCore.Enums; using ImageCore.Extensions; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -93,6 +94,8 @@ public async Task Index([FromServices] IImageParameterFixer param /// 例如 /img/s80x80/t20t201902-94E0437664E3FA99C094E0437664E3FA99C0.png /// [HttpPost] + [Authorize] + [DisableRequestSizeLimit] public async Task Upload([FromFile] ImageFileInfo file, bool isTemp = false, BusinessType businessType = BusinessType.Default) { diff --git a/ImageApi/ImageApi.csproj b/ImageApi/ImageApi.csproj index 84202f9..f3bc101 100644 --- a/ImageApi/ImageApi.csproj +++ b/ImageApi/ImageApi.csproj @@ -12,9 +12,12 @@ + + + diff --git a/ImageApi/Properties/PublishProfiles/FolderProfile2.pubxml b/ImageApi/Properties/PublishProfiles/FolderProfile2.pubxml new file mode 100644 index 0000000..8b70de7 --- /dev/null +++ b/ImageApi/Properties/PublishProfiles/FolderProfile2.pubxml @@ -0,0 +1,19 @@ + + + + + FileSystem + FileSystem + Release + Any CPU + + True + False + 2528c869-0822-49b2-ba00-6beebc316244 + D:\wwwroot\ImageServer_Mysql + False + + \ No newline at end of file diff --git a/ImageApi/Startup.cs b/ImageApi/Startup.cs index f5aa59b..7c20f5d 100644 --- a/ImageApi/Startup.cs +++ b/ImageApi/Startup.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -40,6 +41,20 @@ public void ConfigureServices(IServiceCollection services) new PhysicalFileProvider(Directory.GetCurrentDirectory())); services.AddSingleton(); services.AddEntityFrameworkNpgsql(); + services.AddAuthentication("Bearer") + .AddIdentityServerAuthentication(options => + { + options.Authority = configuration["AuthServer:Authority"]; + options.RequireHttpsMetadata = false; + options.ApiName = configuration["AuthServer:ApiName"]; + //options.InboundJwtClaimTypeMap["sub"] = AbpClaimTypes.UserId; + //options.InboundJwtClaimTypeMap["role"] = AbpClaimTypes.Role; + //options.InboundJwtClaimTypeMap["email"] = AbpClaimTypes.Email; + //options.InboundJwtClaimTypeMap["email_verified"] = AbpClaimTypes.EmailVerified; + //options.InboundJwtClaimTypeMap["phone_number"] = AbpClaimTypes.PhoneNumber; + //options.InboundJwtClaimTypeMap["phone_number_verified"] = AbpClaimTypes.PhoneNumberVerified; + //options.InboundJwtClaimTypeMap["name"] = AbpClaimTypes.UserName; + }); services.AddImageService(option => { option.Filters = new[] {".jpg", ".jpeg", ".gif", ".png", ".bmp"}; @@ -71,6 +86,15 @@ public void ConfigureServices(IServiceCollection services) c.SwaggerDoc("v1", new Info {Title = "图片资源API", Version = "v1"}); c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "ImageApi.xml")); c.OperationFilter(); + c.AddSecurityDefinition("bearerAuth", new ApiKeyScheme() + { + Description = "JWT Authorization header using the bearer scheme. Example: \"Authorization: Bearer {token}\"", + Name = "Authorization", + In = "header", + Type = "apiKey" + }); + var security = new Dictionary> { { "bearerAuth", new string[] { } }, }; + c.AddSecurityRequirement(security); }); } @@ -82,6 +106,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseDeveloperExceptionPage(); } app.UseCors(DefaultCorsPolicyName); + app.UseAuthentication(); app.UseMvcWithDefaultRoute(); app.UseSwagger() .UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "图片资源API"); }); diff --git a/ImageApi/appsettings.json b/ImageApi/appsettings.json index 934feb0..3d03031 100644 --- a/ImageApi/appsettings.json +++ b/ImageApi/appsettings.json @@ -1,6 +1,10 @@ { "ConnectionStrings": { - "Default": "Server=172.16.4.6;Port=5432;Database=ImageDb;User Id=postgres;Password=123456" + "Default": "host=6.6.6.6;port=5432;database=image;userid=postgres;pwd=123123;" + }, + "AuthServer": { + "Authority": "http://localhost:44305/", + "ApiName": "Image" }, "Logging": { "LogLevel": {