Serve downloadable PDFs from a PocketBase documents collection
- New collection "documents" (title, slug, file, category, order, public); editors upload PDFs in the admin, no developer or redeploy needed - The app serves each file at /dokumente/<slug>.pdf, a URL that survives a new upload (PocketBase's own file name gets a random suffix) - New "documents" embed lists them as file cards, grouped by category; /downloads uses it - List/view rules are "public = true", so a draft PDF is not reachable through the PocketBase API either - Dev seed adds example documents with generated PDFs and fixes the const reassignment that broke seeding a fresh volume - Docs: editor guide, architecture, and the manual production setup steps Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
97321e2b49
commit
f1a06956f7
18 files changed
+641
-31
No files matched your search
@@ -9,7 +9,7 @@ using Elternbeirat.PocketBase;
|
||||
namespace Elternbeirat.Web.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Starts a throwaway PocketBase container once per test run, creates the four
|
||||
/// Starts a throwaway PocketBase container once per test run, creates the
|
||||
/// content collections and seeds them with a small, known data set. The tests run
|
||||
/// against this instance instead of the live one, so they are hermetic (no network
|
||||
/// to Unraid), reproducible (fixed data) and safe (isolated from production).
|
||||
@@ -194,7 +194,7 @@ public sealed class PocketBaseFixture : IAsyncLifetime
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Imports the four content collections from the shared schema file, the same
|
||||
/// Imports the content collections from the shared schema file, the same
|
||||
/// one the dev seed migration reads (<c>pb/pb_migrations/collections_schema.json</c>).
|
||||
/// Uses PocketBase's <c>/api/collections/import</c> so the whole schema is
|
||||
/// defined in one place instead of being typed out here.
|
||||
@@ -270,7 +270,7 @@ public sealed class PocketBaseFixture : IAsyncLifetime
|
||||
await CreateRecordAsync(http, "pages", new
|
||||
{
|
||||
title = "Downloads", body = "# Downloads", location = "header",
|
||||
order = 5, slug = "downloads", embed = Array.Empty<string>(), @public = true,
|
||||
order = 5, slug = "downloads", embed = new[] { "documents" }, @public = true,
|
||||
});
|
||||
// Page records for the posts and events lists. Routes /posts and /events are
|
||||
// served by PostList and EventList, which read these records for their heading
|
||||
@@ -388,6 +388,47 @@ public sealed class PocketBaseFixture : IAsyncLifetime
|
||||
question = "Wie viel kostet ein Schließfach?", answer = "20 Euro.",
|
||||
topic = lockerTopicId, order = 1, @public = true,
|
||||
});
|
||||
|
||||
// Documents: created out of order on purpose, so the tests prove the list is
|
||||
// sorted by "order". Two share a category, one has none, and one is a draft
|
||||
// whose file must never be served.
|
||||
await CreateDocumentAsync(http, "Einzugsermächtigung", "einzugsermaechtigung", "Förderverein", 2, true);
|
||||
await CreateDocumentAsync(http, "Aufnahmeantrag Förderverein", "aufnahmeantrag-foerderverein", "Förderverein", 1, true);
|
||||
await CreateDocumentAsync(http, "Elternbrief", "elternbrief", "", 3, true);
|
||||
await CreateDocumentAsync(http, "Internes Protokoll", "internal-minutes", "", 4, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The content of every seeded document's file: the smallest input PocketBase's
|
||||
/// MIME sniffing accepts as <c>application/pdf</c>, so the tests can compare the
|
||||
/// served bytes exactly.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<byte> DocumentContent { get; } = "%PDF-1.4\n%%EOF\n"u8.ToArray();
|
||||
|
||||
private static async Task CreateDocumentAsync(
|
||||
HttpClient http, string title, string slug, string category, int order, bool isPublic)
|
||||
{
|
||||
// A file field takes a multipart upload, not JSON. The multipart content owns
|
||||
// and disposes the parts added to it, which CA2000 cannot see.
|
||||
using var content = new MultipartFormDataContent();
|
||||
#pragma warning disable CA2000
|
||||
foreach (var (name, value) in new[]
|
||||
{
|
||||
("title", title),
|
||||
("slug", slug),
|
||||
("category", category),
|
||||
("order", order.ToString(CultureInfo.InvariantCulture)),
|
||||
("public", isPublic ? "true" : "false"),
|
||||
})
|
||||
content.Add(new StringContent(value), name);
|
||||
content.Add(new ByteArrayContent([.. DocumentContent])
|
||||
{
|
||||
Headers = { ContentType = new MediaTypeHeaderValue("application/pdf") },
|
||||
}, "file", $"{slug}.pdf");
|
||||
#pragma warning restore CA2000
|
||||
|
||||
var response = await http.PostAsync(new Uri("/api/collections/documents/records", UriKind.Relative), content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private static async Task<string> CreateRecordAsync(HttpClient http, string collection, object record)
|
||||
|
||||
Reference in new issue
Block a user