From f5ffedf5816d192c2df7149717fce6d161e2c8d6 Mon Sep 17 00:00:00 2001 From: chsami Date: Wed, 31 Jul 2024 14:59:44 +0200 Subject: [PATCH] Ability to download jar from AzureStorageService --- MicrobotApi/Controllers/FileController.cs | 66 +++++++++++++++++---- MicrobotApi/MicrobotApi.csproj | 1 + MicrobotApi/Services/AzureStorageService.cs | 51 ++++++++++++---- 3 files changed, 93 insertions(+), 25 deletions(-) diff --git a/MicrobotApi/Controllers/FileController.cs b/MicrobotApi/Controllers/FileController.cs index 48775b2..c4d60fc 100644 --- a/MicrobotApi/Controllers/FileController.cs +++ b/MicrobotApi/Controllers/FileController.cs @@ -1,8 +1,11 @@ -using MicrobotApi.Database; +using Azure; +using Azure.Storage.Blobs.Models; +using MicrobotApi.Database; +using MicrobotApi.Models; using MicrobotApi.Services; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; namespace MicrobotApi.Controllers; @@ -12,24 +15,63 @@ public class FileController : Controller { private readonly AzureStorageService _azureStorageService; private readonly MicrobotContext _microbotContext; + private readonly IMemoryCache _memoryCache; - public FileController(AzureStorageService azureStorageService, MicrobotContext microbotContext) + public FileController(AzureStorageService azureStorageService, MicrobotContext microbotContext, IMemoryCache memoryCache) { _azureStorageService = azureStorageService; _microbotContext = microbotContext; + _memoryCache = memoryCache; } - - [Authorize] - [HttpGet("download/{blobName}/{key}/{hwid}")] - public async Task Download(string blobName, string key, string hwid) - { - var exists = await _microbotContext.Keys.AnyAsync(x => x.Key == key && x.HWID == hwid); - if (!exists) + /*[HttpGet("download/{fileName}/{key}/{hwid}")] + public async Task Download(Guid fileName, string key, string hwid) + { + DateTime? dateTime = _memoryCache.Get(key); + if (!dateTime.HasValue) + { return Unauthorized(); + } + + var exists = await _microbotContext.Keys.AnyAsync(x => x.Key == key && (x.HWID == "" || x.HWID == hwid)); - var file = await _azureStorageService.DownloadFile(blobName); + if (!exists) + { + return Unauthorized(); + } + + var script = await _microbotContext.Scripts + .FirstAsync(x => x.Id == fileName); + + var file = await _azureStorageService.DownloadFile(script.Name + "/" + script.Id + ".jar"); + + return File(file.Value.Content, "application/octet-stream", script.Id.ToString()); - return File(file.Value.Content, "application/octet-stream", blobName); + }*/ + + [HttpGet("{path}")] + public async Task List(string path) + { + var downloadUrl = await _azureStorageService.GetDownloadUrl(path); + + return Ok(downloadUrl); + + } + + [HttpGet("list/{environment}/{fileName}")] + public async Task List(string environment, string fileName) + { + var fileNames = await _azureStorageService.GetFileNames(environment, fileName); + + return Ok(fileNames); + + } + + [HttpGet("download{environment}/{fileName}")] + public async Task Download(string environment, string fileName) + { + var file = await _azureStorageService.DownloadFile(environment + "/" + fileName); + + return File(file.Value.Content, "application/octet-stream", fileName); } } \ No newline at end of file diff --git a/MicrobotApi/MicrobotApi.csproj b/MicrobotApi/MicrobotApi.csproj index 2bdfdfe..cc7a22a 100644 --- a/MicrobotApi/MicrobotApi.csproj +++ b/MicrobotApi/MicrobotApi.csproj @@ -8,6 +8,7 @@ + diff --git a/MicrobotApi/Services/AzureStorageService.cs b/MicrobotApi/Services/AzureStorageService.cs index 47a121a..1341860 100644 --- a/MicrobotApi/Services/AzureStorageService.cs +++ b/MicrobotApi/Services/AzureStorageService.cs @@ -1,4 +1,6 @@ -using Azure; +using System.Web; +using Azure; +using Azure.Storage; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; using Azure.Storage.Blobs.Specialized; @@ -9,14 +11,16 @@ namespace MicrobotApi.Services; public class AzureStorageService { private readonly BlobServiceClient _blobServiceClient; + private readonly IConfiguration _configuration; private const string BlobContainer = "microbot"; - public AzureStorageService(BlobServiceClient blobServiceClient) + public AzureStorageService(BlobServiceClient blobServiceClient, IConfiguration configuration) { _blobServiceClient = blobServiceClient; + _configuration = configuration; } - public static Uri GetSasUri(BlobBaseClient blobClient, string storedPolicyName = null) + public static Uri GetSasUri(BlobBaseClient blobClient) { if (!blobClient.CanGenerateSasUri) return null; @@ -28,15 +32,9 @@ public static Uri GetSasUri(BlobBaseClient blobClient, string storedPolicyName = Resource = "b" }; - if (storedPolicyName == null) - { - sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(2); - sasBuilder.SetPermissions(BlobSasPermissions.Read); - } - else - { - sasBuilder.Identifier = storedPolicyName; - } + sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5); + sasBuilder.SetPermissions(BlobSasPermissions.Read); + var sasUri = blobClient.GenerateSasUri(sasBuilder); return sasUri; @@ -46,9 +44,36 @@ public Task> DownloadFile(string storagePath) { var containerClient = _blobServiceClient.GetBlobContainerClient(BlobContainer); var blobClient = containerClient.GetBlobClient(storagePath); - + var blobData = blobClient.DownloadAsync(); return blobData; } + + public async Task GetDownloadUrl(string storagePath) + { + var containerClient = _blobServiceClient.GetBlobContainerClient(BlobContainer); + + BlobClient blobClient = containerClient.GetBlobClient(HttpUtility.UrlDecode(storagePath)); + Uri sasUri = blobClient.GenerateSasUri(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddHours(1)); + return sasUri; + } + + public async Task> GetFileNames(string storagePath, string fileName) + { + var containerClient = _blobServiceClient.GetBlobContainerClient(BlobContainer); + + var fileNames = new List(); + + // List blobs in the container + await foreach (BlobItem blobItem in containerClient.GetBlobsAsync(prefix: storagePath)) + { + if (blobItem.Name.Contains(fileName)) + { + fileNames.Add(blobItem.Name); + } + } + + return fileNames; + } } \ No newline at end of file