diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-29 20:39:07 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-29 20:39:07 +0200 |
| commit | b38d6693476917972345397298b534af2b8b8f78 (patch) | |
| tree | 5e62376cf693a7233cec4ab341f658be5d9c4ec2 /src/DevHive.Services/Models/Cloud/CloudinaryService.cs | |
| parent | 2417df3ff2939b67695b80905f301ef53c905260 (diff) | |
| download | DevHive-b38d6693476917972345397298b534af2b8b8f78.tar DevHive-b38d6693476917972345397298b534af2b8b8f78.tar.gz DevHive-b38d6693476917972345397298b534af2b8b8f78.zip | |
File Upload implemented; Post Layers adapted to File Uploading
Diffstat (limited to 'src/DevHive.Services/Models/Cloud/CloudinaryService.cs')
| -rw-r--r-- | src/DevHive.Services/Models/Cloud/CloudinaryService.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/DevHive.Services/Models/Cloud/CloudinaryService.cs b/src/DevHive.Services/Models/Cloud/CloudinaryService.cs new file mode 100644 index 0000000..a9bc9bd --- /dev/null +++ b/src/DevHive.Services/Models/Cloud/CloudinaryService.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using CloudinaryDotNet; +using CloudinaryDotNet.Actions; +using DevHive.Services.Interfaces; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Services +{ + public class CloudinaryService : ICloudService + { + 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 formFileId = Guid.NewGuid().ToString(); + + if (formFile.Length > 0) + { + using (var ms = new MemoryStream()) + { + formFile.CopyTo(ms); + byte[] formBytes = ms.ToArray(); + + ImageUploadParams imageUploadParams = new() + { + File = new FileDescription(formFileId, new MemoryStream(formBytes)), + PublicId = formFileId + }; + + ImageUploadResult uploadResult = await this._cloudinary.UploadAsync(imageUploadParams); + fileUrls.Add(uploadResult.Url.AbsoluteUri); + } + } + } + + return fileUrls; + } + + public async Task<bool> RemoveFilesFromCloud(List<string> fileUrls) + { + return true; + } + } +} |
