public async Task <(Stream stream, string fileName, string contentType)> DownloadFileAsync( Guid id, ClaimsPrincipal user, CancellationToken cancellationToken) { StoredFile file = await _dbContext.StoredFiles.SingleAsync(f => f.Id == id, cancellationToken); FileAccessUtils.CheckAccess(file, user); Stream stream = await _blobStorageService.DownloadBlobAsync(file.StoredBlobId, user, cancellationToken); return(stream, file.FileName, file.FileContentType); }
private BlobClient GetBlobClient(Guid blobId, ClaimsPrincipal user) { // The blob folder is the tenant or user id // Personal accounts -> user id, organizational accounts -> tenant id string name = $"{FileAccessUtils.GetBlobFolder(user)}/{blobId}"; BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(_options.FileContainerName); BlobClient blobClient = containerClient.GetBlobClient(name); return(blobClient); }
public async Task DeleteFileAsync(Guid id, ClaimsPrincipal user, CancellationToken cancellationToken) { StoredFile file = await _dbContext.StoredFiles.SingleAsync(f => f.Id == id, cancellationToken); FileAccessUtils.CheckAccess(file, user); _dbContext.StoredFiles.Remove(file); // These two operations cannot be canceled, so no cancellation token await _blobStorageService.DeleteBlobAsync(file.StoredBlobId, user); await _dbContext.SaveChangesAsync(); }