Liten kommentar

This commit is contained in:
2025-06-29 09:13:57 +02:00
parent 88161dc2ab
commit a0b1ee2f85

View File

@ -2,10 +2,10 @@ using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using OfflineDemoApi.Data; using OfflineDemoApi.Data;
using OfflineDemoApi.Models; using OfflineDemoApi.Models;
using System;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Tommy har varit h<>r
// Add services to the container. // Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi(); builder.Services.AddOpenApi();
@ -24,20 +24,20 @@ builder.Services.AddSingleton(blobContainerClient);
// Configure Kestrel limits // Configure Kestrel limits
builder.WebHost.ConfigureKestrel(options => builder.WebHost.ConfigureKestrel(options =>
{ {
options.Limits.MaxRequestBodySize = 524288000*2; // Example: 500 MB options.Limits.MaxRequestBodySize = 524288000 * 2; // Example: 500 MB
options.Limits.MaxRequestBufferSize = 524288000 * 2; // Example: 100 MB options.Limits.MaxRequestBufferSize = 524288000 * 2; // Example: 100 MB
}); });
builder.Services.AddSingleton<ISqlDataAccess, SqlDataAccess>(); builder.Services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowAnyOrigin", policy => options.AddPolicy("AllowAnyOrigin", policy =>
{ {
policy.AllowAnyOrigin() policy.AllowAnyOrigin()
.AllowAnyHeader() .AllowAnyHeader()
.AllowAnyMethod(); .AllowAnyMethod();
}); });
}); });
var app = builder.Build(); var app = builder.Build();
@ -47,99 +47,100 @@ app.UseCors("AllowAnyOrigin");
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.MapOpenApi(); app.MapOpenApi();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.MapGet("/", () => { app.MapGet("/", () =>
return "Hello World"; {
return "Hello World";
}); });
app.MapGet("/api/shorts", async (ISqlDataAccess sql) => app.MapGet("/api/shorts", async (ISqlDataAccess sql) =>
{ {
return await sql.LoadData<ShortsModel, dynamic>("spShorts_GetAll", new { }, "sql"); return await sql.LoadData<ShortsModel, dynamic>("spShorts_GetAll", new { }, "sql");
}); });
app.MapGet("/api/file", async (IConfiguration config, string url) => app.MapGet("/api/file", async (IConfiguration config, string url) =>
{ {
var blobUri = new Uri(url); var blobUri = new Uri(url);
var storageAccountKey = config.GetValue<string>("AzureStorage:StorageAccountKey"); var storageAccountKey = config.GetValue<string>("AzureStorage:StorageAccountKey");
var storageAccountName = config.GetValue<string>("AzureStorage:StorageAccountName"); var storageAccountName = config.GetValue<string>("AzureStorage:StorageAccountName");
var credential = new Azure.Storage.StorageSharedKeyCredential(storageAccountName, storageAccountKey); var credential = new Azure.Storage.StorageSharedKeyCredential(storageAccountName, storageAccountKey);
var blobClient = new BlobClient(blobUri, credential); var blobClient = new BlobClient(blobUri, credential);
var downloadResponse = await blobClient.DownloadStreamingAsync(); var downloadResponse = await blobClient.DownloadStreamingAsync();
return Results.File( return Results.File(
downloadResponse.Value.Content, downloadResponse.Value.Content,
downloadResponse.Value.Details.ContentType, downloadResponse.Value.Details.ContentType,
fileDownloadName: blobUri.Segments.Last() fileDownloadName: blobUri.Segments.Last()
); );
}); });
app.MapPost("/api/upload", async (HttpRequest request, app.MapPost("/api/upload", async (HttpRequest request,
BlobContainerClient containerClient, BlobContainerClient containerClient,
ISqlDataAccess sql) => ISqlDataAccess sql) =>
{ {
if (!request.HasFormContentType || request.Form.Files.Count == 0) if (!request.HasFormContentType || request.Form.Files.Count == 0)
{ {
return Results.BadRequest("Invalid form submission. Files are required."); return Results.BadRequest("Invalid form submission. Files are required.");
} }
var form = await request.ReadFormAsync(); var form = await request.ReadFormAsync();
// Extract form data // Extract form data
var title = form["Title"].ToString(); var title = form["Title"].ToString();
var description = form["Description"].ToString(); var description = form["Description"].ToString();
var hashtags = form["Hashtags"].ToString(); var hashtags = form["Hashtags"].ToString();
var mp4File = form.Files["Mp4File"]; var mp4File = form.Files["Mp4File"];
var imageFile = form.Files["ImageFile"]; var imageFile = form.Files["ImageFile"];
if (mp4File == null || imageFile == null) if (mp4File == null || imageFile == null)
{ {
return Results.BadRequest("Both MP4 and image files are required."); return Results.BadRequest("Both MP4 and image files are required.");
} }
// Validate file size // Validate file size
const long maxFileSize = 800L * 1024 * 1024; // 800MB const long maxFileSize = 800L * 1024 * 1024; // 800MB
if (mp4File.Length > maxFileSize) if (mp4File.Length > maxFileSize)
{ {
return Results.BadRequest("MP4 file exceeds the 800MB limit."); return Results.BadRequest("MP4 file exceeds the 800MB limit.");
} }
var createNewParameters = new { title, description, hashtags }; var createNewParameters = new { title, description, hashtags };
int id = await sql.SaveDataScalar<int, dynamic>("spShorts_CreateNew", createNewParameters, "sql"); int id = await sql.SaveDataScalar<int, dynamic>("spShorts_CreateNew", createNewParameters, "sql");
// Rename files // Rename files
string? mp4FileName = $"{id}.mp4"; string? mp4FileName = $"{id}.mp4";
// TODO - Fix this extension lookup // TODO - Fix this extension lookup
string? imageFileName = $"{id}.{imageFile.FileName.Split('.')[1]}"; string? imageFileName = $"{id}.{imageFile.FileName.Split('.')[1]}";
// Upload MP4 file // Upload MP4 file
var mp4BlobClient = containerClient.GetBlobClient($"shorts/{mp4FileName}"); var mp4BlobClient = containerClient.GetBlobClient($"shorts/{mp4FileName}");
await using (var mp4Stream = mp4File.OpenReadStream()) await using (var mp4Stream = mp4File.OpenReadStream())
{ {
await mp4BlobClient.UploadAsync(mp4Stream, true); await mp4BlobClient.UploadAsync(mp4Stream, true);
} }
// Upload Image file // Upload Image file
var imageBlobClient = containerClient.GetBlobClient($"images/{imageFileName}"); var imageBlobClient = containerClient.GetBlobClient($"images/{imageFileName}");
await using (var imageStream = imageFile.OpenReadStream()) await using (var imageStream = imageFile.OpenReadStream())
{ {
await imageBlobClient.UploadAsync(imageStream, true); await imageBlobClient.UploadAsync(imageStream, true);
} }
// Update SQL with the uploaded files // Update SQL with the uploaded files
string? mp4FileUrl = mp4BlobClient.Uri.ToString(); string? mp4FileUrl = mp4BlobClient.Uri.ToString();
string? imageFileUrl = imageBlobClient.Uri.ToString(); string? imageFileUrl = imageBlobClient.Uri.ToString();
var addUploadedFilesParameters = new { id, mp4FileUrl, imageFileUrl }; var addUploadedFilesParameters = new { id, mp4FileUrl, imageFileUrl };
await sql.SaveData("spShorts_AddUploadedFiles", addUploadedFilesParameters, "sql"); await sql.SaveData("spShorts_AddUploadedFiles", addUploadedFilesParameters, "sql");
return Results.Ok(new { Message = "Files uploaded to Azure Blob Storage successfully." }); return Results.Ok(new { Message = "Files uploaded to Azure Blob Storage successfully." });
}); });
app.Run(); app.Run();