Skip to content

Commit

Permalink
Ability to download jar from AzureStorageService
Browse files Browse the repository at this point in the history
  • Loading branch information
chsami committed Jul 31, 2024
1 parent fdf734f commit f5ffedf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
66 changes: 54 additions & 12 deletions MicrobotApi/Controllers/FileController.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<IActionResult> 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<IActionResult> Download(Guid fileName, string key, string hwid)
{
DateTime? dateTime = _memoryCache.Get<DateTime>(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<IActionResult> List(string path)
{
var downloadUrl = await _azureStorageService.GetDownloadUrl(path);

return Ok(downloadUrl);

}

[HttpGet("list/{environment}/{fileName}")]
public async Task<IActionResult> List(string environment, string fileName)
{
var fileNames = await _azureStorageService.GetFileNames(environment, fileName);

return Ok(fileNames);

}

[HttpGet("download{environment}/{fileName}")]
public async Task<IActionResult> Download(string environment, string fileName)
{
var file = await _azureStorageService.DownloadFile(environment + "/" + fileName);

return File(file.Value.Content, "application/octet-stream", fileName);
}
}
1 change: 1 addition & 0 deletions MicrobotApi/MicrobotApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Discord.Net" Version="3.15.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
Expand Down
51 changes: 38 additions & 13 deletions MicrobotApi/Services/AzureStorageService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -46,9 +44,36 @@ public Task<Response<BlobDownloadInfo>> DownloadFile(string storagePath)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(BlobContainer);
var blobClient = containerClient.GetBlobClient(storagePath);

var blobData = blobClient.DownloadAsync();

return blobData;
}

public async Task<Uri> 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<List<string>> GetFileNames(string storagePath, string fileName)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(BlobContainer);

var fileNames = new List<string>();

// 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;
}
}

0 comments on commit f5ffedf

Please sign in to comment.