using System.IO; using System.Threading.Tasks; using AnkiSentenceCardBuilder.Controllers; using AnkiSentenceCardBuilder.Models; using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.Functions.Worker; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Logging; namespace AnkiSentenceCardBuilder { public class Function1 { private readonly ILogger _logger; public Function1(ILogger logger) { _logger = logger; } [Function(nameof(Function1))] public async Task Run([BlobTrigger("anki2-file/{name}.anki2", Connection = "BlobConnectionString")] BlockBlobClient blobClient, string name) { //Copy the blob to a local temp folder string tempDbPath = await CopyBlobToTempFolder(blobClient, folderName: "Anki2Sentence", fileName: $"{name}.anki2"); //Setup db controller Anki2Controller anki2Controller = new Anki2Controller(tempDbPath); //Get the decks var decks = anki2Controller.GetTable(); //using var blobStreamReader = new StreamReader(stream); //var content = await blobStreamReader.ReadToEndAsync(); _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} Temp: {tempDbPath}"); } //Returns full path where the blob was copied to public async Task CopyBlobToTempFolder(BlockBlobClient blobClient, string folderName, string fileName) { //Create the directory path string directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), folderName); //Add any missing directories Directory.CreateDirectory(directoryPath); //Add file name for the full path string fullPath = Path.Combine(directoryPath, fileName); //Copy blob to the temp path await blobClient.DownloadToAsync(fullPath); //Return the path return fullPath; } } }