diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-11 12:57:32 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-11 12:57:32 +0300 |
| commit | 6e72e69cfbef65c05005c793e0928b4d81224234 (patch) | |
| tree | 9da0ab333458dc4c37f4e9dd34b10f0bb123c52d | |
| parent | 1691baf054b6695da35c9d29032d9b3934c1f979 (diff) | |
| download | it-kariera-exam-template-6e72e69cfbef65c05005c793e0928b4d81224234.tar it-kariera-exam-template-6e72e69cfbef65c05005c793e0928b4d81224234.tar.gz it-kariera-exam-template-6e72e69cfbef65c05005c793e0928b4d81224234.zip | |
Added Cloudinary service and configured it
| -rw-r--r-- | ExamTemplate/Services/CloudinaryService.cs | 49 | ||||
| -rw-r--r-- | ExamTemplate/Services/Services.csproj | 1 | ||||
| -rw-r--r-- | ExamTemplate/Web/Startup.cs | 5 | ||||
| -rw-r--r-- | ExamTemplate/Web/appsettings.json | 5 |
4 files changed, 60 insertions, 0 deletions
diff --git a/ExamTemplate/Services/CloudinaryService.cs b/ExamTemplate/Services/CloudinaryService.cs new file mode 100644 index 0000000..03b7265 --- /dev/null +++ b/ExamTemplate/Services/CloudinaryService.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using System.IO; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using CloudinaryDotNet; +using CloudinaryDotNet.Actions; +using Microsoft.AspNetCore.Http; + +namespace ExamTemplate.Services +{ + public class CloudinaryService + { + // Regex for getting the filename without (final) filename extension + // So, from image.png, it will match image, and from doc.my.txt will match doc.my + private static readonly Regex s_imageRegex = new(".*(?=\\.)"); + + private readonly Cloudinary _cloudinary; + + public CloudinaryService(string cloudName, string apiKey, string apiSecret) + { + this._cloudinary = new Cloudinary(new Account(cloudName, apiKey, apiSecret)); + } + + public async Task<List<string>> UploadFilesToCloud(List<IFormFile> formFiles) + { + List<string> fileUrls = new(); + foreach (var formFile in formFiles) + { + string fileName = s_imageRegex.Match(formFile.FileName).ToString(); + + using var ms = new MemoryStream(); + formFile.CopyTo(ms); + byte[] formBytes = ms.ToArray(); + + RawUploadParams rawUploadParams = new() + { + File = new FileDescription(fileName, new MemoryStream(formBytes)), + PublicId = fileName, + UseFilename = true + }; + + RawUploadResult rawUploadResult = await this._cloudinary.UploadAsync(rawUploadParams); + fileUrls.Add(rawUploadResult.Url.AbsoluteUri); + } + + return fileUrls; + } + } +} diff --git a/ExamTemplate/Services/Services.csproj b/ExamTemplate/Services/Services.csproj index 9383013..eb60f3d 100644 --- a/ExamTemplate/Services/Services.csproj +++ b/ExamTemplate/Services/Services.csproj @@ -7,6 +7,7 @@ <ItemGroup> <PackageReference Include="Automapper" Version="10.1.1" /> + <PackageReference Include="CloudinaryDotNet" Version="1.15.1" /> <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" /> </ItemGroup>
diff --git a/ExamTemplate/Web/Startup.cs b/ExamTemplate/Web/Startup.cs index ba8737f..bfaab91 100644 --- a/ExamTemplate/Web/Startup.cs +++ b/ExamTemplate/Web/Startup.cs @@ -33,6 +33,11 @@ namespace ExamTemplate.Web * Dependency Injection configuration
*/
+ services.AddTransient<CloudinaryService>(options =>
+ new CloudinaryService(
+ cloudName: this.Configuration.GetSection("Cloud").GetSection("cloudName").Value,
+ apiKey: this.Configuration.GetSection("Cloud").GetSection("apiKey").Value,
+ apiSecret: this.Configuration.GetSection("Cloud").GetSection("apiSecret").Value));
services.AddTransient<UserService>();
/*
diff --git a/ExamTemplate/Web/appsettings.json b/ExamTemplate/Web/appsettings.json index f49d9ab..f1b58be 100644 --- a/ExamTemplate/Web/appsettings.json +++ b/ExamTemplate/Web/appsettings.json @@ -2,6 +2,11 @@ "ConnectionStrings": {
"LocalDBConnection": "Server=localhost;Port=5432;Database=TemplateContext;User Id=;Password=;"
},
+ "Cloud": {
+ "cloudName": "ExamTemplate",
+ "apiKey": "",
+ "apiSecret": ""
+ },
"Logging": {
"LogLevel": {
"Default": "Information",
|
