aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Backend/Api/Api/Api.csproj1
-rw-r--r--Backend/Api/Api/Controllers/PostController.cs9
-rw-r--r--Backend/Api/Api/Interfaces/IFileService.cs1
-rw-r--r--Backend/Api/Api/Services/FileService.cs18
4 files changed, 29 insertions, 0 deletions
diff --git a/Backend/Api/Api/Api.csproj b/Backend/Api/Api/Api.csproj
index 24c41b7..0e541f9 100644
--- a/Backend/Api/Api/Api.csproj
+++ b/Backend/Api/Api/Api.csproj
@@ -10,6 +10,7 @@
<PackageReference Include="Geocoding.Core" Version="4.0.1" />
<PackageReference Include="Geocoding.Google" Version="4.0.1" />
<PackageReference Include="Geocoding.MapQuest" Version="4.0.1" />
+ <PackageReference Include="Magick.NET-Q16-AnyCPU" Version="12.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs
index dc48c73..3fe1b3f 100644
--- a/Backend/Api/Api/Controllers/PostController.cs
+++ b/Backend/Api/Api/Controllers/PostController.cs
@@ -66,6 +66,15 @@ namespace Api.Controllers
return BadRequest("Slika ne postoji");
return File(System.IO.File.ReadAllBytes(f.path), "image/*", Path.GetFileName(f.path));
}
+ [HttpGet("image/compress/{id}")]
+ //[Authorize(Roles = "User")]
+ public async Task<ActionResult> getCompressedImage(string id)
+ {
+ Byte[] f = await _fileService.getCompressedImage(id);
+ if (f == null)
+ return BadRequest("Slika ne postoji");
+ return File(f, "image/*", "tempcompress");
+ }
[HttpPost("posts/{id}/addrating")]
[Authorize(Roles = "User")]
diff --git a/Backend/Api/Api/Interfaces/IFileService.cs b/Backend/Api/Api/Interfaces/IFileService.cs
index e736305..33ff5d9 100644
--- a/Backend/Api/Api/Interfaces/IFileService.cs
+++ b/Backend/Api/Api/Interfaces/IFileService.cs
@@ -4,5 +4,6 @@
{
Task<Models.File> add(Models.File file);
Task<Models.File> getById(string id);
+ Task<Byte[]> getCompressedImage(string id);
}
} \ No newline at end of file
diff --git a/Backend/Api/Api/Services/FileService.cs b/Backend/Api/Api/Services/FileService.cs
index 1937c10..b951efc 100644
--- a/Backend/Api/Api/Services/FileService.cs
+++ b/Backend/Api/Api/Services/FileService.cs
@@ -1,5 +1,6 @@
using Api.Interfaces;
using Api.Models;
+using ImageMagick;
using MongoDB.Driver;
using File = Api.Models.File;
@@ -24,5 +25,22 @@ namespace Api.Services
{
return await _files.Find(file => file._id == id).FirstOrDefaultAsync();
}
+ public async Task<Byte[]> getCompressedImage(string id)
+ {
+ Byte[] res = null;
+ Models.File f = await getById(id);
+ if (f == null || !System.IO.File.Exists(f.path))
+ return res;
+ using (MagickImage image = new MagickImage(f.path))
+ {
+ image.Format = image.Format;
+ image.Quality = 30;
+ res= image.ToByteArray();
+ }
+
+
+ return res;
+
+ }
}
}