diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..959ade5cb --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +AWS_BUCKET_NAME= +AWS_BUCKET_PUBLIC_KEY= +AWS_BUCKET_SECRET_KEY= diff --git a/.gitignore b/.gitignore index eb271ba04..fb6bdbc7e 100644 --- a/.gitignore +++ b/.gitignore @@ -352,3 +352,5 @@ MigrationBackup/ .DS_Store src/Blogifier/Blog.db + +.env diff --git a/src/Blogifier.Core/Providers/BucketProvider.cs b/src/Blogifier.Core/Providers/BucketProvider.cs index e543a17d8..b72249412 100644 --- a/src/Blogifier.Core/Providers/BucketProvider.cs +++ b/src/Blogifier.Core/Providers/BucketProvider.cs @@ -1,3 +1,4 @@ +using System.Text; using System.Reflection; using Blogifier.Core.Extensions; using Blogifier.Shared; @@ -36,8 +37,9 @@ public BucketProvider(IConfiguration configuration) { _configuration = configuration; _storageRoot = $"{ContentRoot}{_slash}wwwroot{_slash}data{_slash}"; - publicKey = _configuration["AWSCredentials:S3Bucket:accessKey"]; - secretKey = _configuration["AWSCredentials:S3Bucket:secretKey"]; + var tuple = GetAWSCredentials(); + publicKey = tuple.Item1; + secretKey = tuple.Item2; s3Client = new AmazonS3Client(publicKey, secretKey, bucketRegion); } @@ -46,6 +48,30 @@ public bool FileExists(string path) throw new NotImplementedException(); } + private (string, string) GetAWSCredentials() { + var filePath = "../../.env"; + var publicKeyS = ""; + var secretKeyS = ""; + + + if (!File.Exists(filePath)) { + return (null, null); + } + + foreach (var line in File.ReadAllLines(filePath)) + { + var parts = line.Split('=',StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length != 2) + continue; + + if(parts[0] == "AWS_BUCKET_PUBLIC_KEY") publicKeyS = parts[1]; + else if (parts[0] == "AWS_BUCKET_SECRET_KEY") secretKeyS = parts[1]; + } + + return (publicKeyS, secretKeyS); + } + public async Task> GetThemes() { var themes = new List(); @@ -132,8 +158,9 @@ public async Task DownloadFile(string keyName) public async Task UploadFormFile(IFormFile file, string path = "") { s3Client = new AmazonS3Client(publicKey, secretKey, bucketRegion); - + var res = await UploadFileAsync(file.OpenReadStream(), path); + if (res == "Ok") return true; else diff --git a/src/Blogifier.Shared/Domain/Configuration.cs b/src/Blogifier.Shared/Domain/Configuration.cs index 4cd990666..82f8d3343 100644 --- a/src/Blogifier.Shared/Domain/Configuration.cs +++ b/src/Blogifier.Shared/Domain/Configuration.cs @@ -5,6 +5,9 @@ namespace Blogifier.Shared.Domain { public class Configuration { - + [Key] + public string Name { get; set;} + + public bool Active { get; set; } } } diff --git a/src/Blogifier/appsettings.Development.json b/src/Blogifier/appsettings.Development.json index 40babb8b6..8983e0fc1 100644 --- a/src/Blogifier/appsettings.Development.json +++ b/src/Blogifier/appsettings.Development.json @@ -5,12 +5,5 @@ "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } - }, - "AWSCredentials": { - "S3Bucket": { - "name": "qvanuncios-bucket", - "accessKey": "AKIAYCXVMZ23PAEXBPWR", - "secretKey": "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" - } } } diff --git a/src/Blogifier/appsettings.json b/src/Blogifier/appsettings.json index 7c365e2f1..8c8a7bc69 100644 --- a/src/Blogifier/appsettings.json +++ b/src/Blogifier/appsettings.json @@ -11,12 +11,5 @@ "DbProvider": "SQLite", "ConnString": "Data Source=Blog.db", "Salt": "SECRET-CHANGE-ME!" - }, - "AWSCredentials": { - "S3Bucket": { - "name": "qvanuncios-bucket", - "accessKey": "AKIAYCXVMZ23PAEXBPWR", - "secretKey": "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" - } - } + } }