From 92c8160d4796c6a4cefb5770f4f4ff1e082e3f41 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 28 Mar 2021 19:34:06 +0300 Subject: Uploading files to cloudinary now uses the filename, not a guid --- src/Services/DevHive.Services/Services/CloudinaryService.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/Services') diff --git a/src/Services/DevHive.Services/Services/CloudinaryService.cs b/src/Services/DevHive.Services/Services/CloudinaryService.cs index 57955a2..6e2e975 100644 --- a/src/Services/DevHive.Services/Services/CloudinaryService.cs +++ b/src/Services/DevHive.Services/Services/CloudinaryService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text.RegularExpressions; using System.Threading.Tasks; using CloudinaryDotNet; using CloudinaryDotNet.Actions; @@ -11,6 +12,10 @@ namespace DevHive.Services.Services { public class CloudinaryService : ICloudService { + // 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 Regex _imageRegex = new Regex(".*(?=\\.)"); + private readonly Cloudinary _cloudinary; public CloudinaryService(string cloudName, string apiKey, string apiSecret) @@ -23,7 +28,7 @@ namespace DevHive.Services.Services List fileUrls = new(); foreach (var formFile in formFiles) { - string formFileId = Guid.NewGuid().ToString(); + string fileName = _imageRegex.Match(formFile.FileName).ToString(); using (var ms = new MemoryStream()) { @@ -32,8 +37,8 @@ namespace DevHive.Services.Services RawUploadParams rawUploadParams = new() { - File = new FileDescription(formFileId, new MemoryStream(formBytes)), - PublicId = formFileId, + File = new FileDescription(fileName, new MemoryStream(formBytes)), + PublicId = fileName, UseFilename = true }; -- cgit v1.2.3