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 | |
| 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')
4 files changed, 63 insertions, 3 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; + } + } +} diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs index 36f6351..8676f6c 100644 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; namespace DevHive.Services.Models.Post.Post { @@ -8,6 +10,6 @@ namespace DevHive.Services.Models.Post.Post public string Message { get; set; } - // public List<IFormFile> Files { get; set; } + public List<IFormFile> Files { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs index 3e673c1..f0a4fe5 100644 --- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using DevHive.Services.Models.Post.Comment; +using Microsoft.Extensions.FileProviders; namespace DevHive.Services.Models.Post.Post { @@ -20,6 +21,6 @@ namespace DevHive.Services.Models.Post.Post public List<ReadCommentServiceModel> Comments { get; set; } = new(); - //public List<string> Files { get; set; } + public List<IFileInfo> Files { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs index 8924b07..24b0b74 100644 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; namespace DevHive.Services.Models.Post.Post { @@ -10,6 +12,6 @@ namespace DevHive.Services.Models.Post.Post public string NewMessage { get; set; } - // public List<IFormFile> Files { get; set; } + public List<IFormFile> Files { get; set; } } } |
