Repository navigation
Expand file tree
/
Copy pathImageService.cs
More file actions
53 lines (45 loc) · 1.95 KB
/
Copy pathImageService.cs
File metadata and controls
53 lines (45 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
namespace AllReady.Services
{
public class ImageService : IImageService
{
private const string ContainerName = "images";
private readonly IBlockBlob blockBlob;
public ImageService(IBlockBlob blockBlob)
{
this.blockBlob = blockBlob;
}
public async Task<string> UploadOrganizationImageAsync(int organizationId, IFormFile image)
{
var blobPath = organizationId.ToString();
return await UploadAsync(blobPath, image);
}
public async Task<string> UploadCampaignImageAsync(int organizationId, int campaignId, IFormFile image)
{
var blobPath = organizationId + @"/campaigns/" + campaignId;
return await UploadAsync(blobPath, image);
}
public async Task<string> UploadEventImageAsync(int organizationId, int eventId, IFormFile image)
{
var blobPath = organizationId + @"/events/" + eventId;
return await UploadAsync(blobPath, image);
}
public async Task DeleteImageAsync(string imageUrl)
{
await blockBlob.DeleteAsync(ContainerName, imageUrl);
}
private async Task<string> UploadAsync(string blobPath, IFormFile image)
{
var fileName = ContentDispositionHeaderValue.Parse(image.ContentDisposition).FileName.ToString().Trim('"').ToLower();
if (fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg") || fileName.EndsWith(".png") || fileName.EndsWith(".gif"))
{
var blobName = blobPath + "/" + fileName;
return await blockBlob.UploadFromStreamAsync(ContainerName, blobName, image);
}
throw new Exception("Invalid file extension: " + fileName + "You can only upload images with the extension: jpg, jpeg, gif, or png");
}
}
}