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 @@ namespace Elternbeirat.PocketBase;
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// There is one method per collection (pages, posts, events, faqs). Each returns
|
||||
/// There is one method per collection (pages, posts, events, faqs, documents). Each returns
|
||||
/// only records with <c>public = true</c> and leaves filtering and sorting to
|
||||
/// PocketBase via query parameters.
|
||||
/// </para>
|
||||
@@ -108,6 +108,71 @@ public sealed class PocketBaseClient(HttpClient httpClient)
|
||||
public Task<IReadOnlyList<FaqTopic>> GetFaqTopicsAsync(CancellationToken token = default)
|
||||
=> GetRecordsAsync<FaqTopic>("faq_topics", "order", token, expand: "faqs_via_topic");
|
||||
|
||||
/// <summary>
|
||||
/// Gets all public documents, ordered by <see cref="Document.Order"/>, then by
|
||||
/// <see cref="Document.Title"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The title as second key makes the order total, so documents an editor left at
|
||||
/// the same (or no) order still list the same way between requests.
|
||||
/// </remarks>
|
||||
/// <inheritdoc cref="GetPagesAsync" path="/param"/>
|
||||
/// <returns>
|
||||
/// The public documents, or an empty list if there are none.
|
||||
/// </returns>
|
||||
/// <inheritdoc cref="GetPagesAsync" path="/exception"/>
|
||||
public Task<IReadOnlyList<Document>> GetDocumentsAsync(CancellationToken token = default)
|
||||
=> GetRecordsAsync<Document>("documents", "order,title", token);
|
||||
|
||||
/// <summary>
|
||||
/// Opens the uploaded PDF of a document for reading.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Reads PocketBase's file endpoint
|
||||
/// (<c>/api/files/documents/<id>/<file></c>). The file is streamed,
|
||||
/// not buffered: the returned stream reads straight from the response, so a
|
||||
/// large PDF never sits in the app's memory as a whole.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The client timeout only covers the wait for PocketBase's response headers;
|
||||
/// reading the body afterwards is not cut off, so a slow visitor connection
|
||||
/// does not abort the download midway.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="document">
|
||||
/// The document whose file to open, as returned by <see cref="GetDocumentsAsync"/>.
|
||||
/// </param>
|
||||
/// <param name="token">
|
||||
/// A token to cancel the request.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The file content. The caller owns the stream and must dispose it.
|
||||
/// </returns>
|
||||
/// <exception cref="PocketBaseUnavailableException">
|
||||
/// PocketBase could not be reached, timed out, or does not have the file (any
|
||||
/// error status).
|
||||
/// </exception>
|
||||
/// <exception cref="OperationCanceledException">
|
||||
/// <paramref name="token"/> was canceled by the caller.
|
||||
/// </exception>
|
||||
public async Task<Stream> OpenDocumentFileAsync(Document document, CancellationToken token = default)
|
||||
{
|
||||
var url = $"/api/files/documents/{Uri.EscapeDataString(document.Id)}/{Uri.EscapeDataString(document.File)}";
|
||||
try
|
||||
{
|
||||
return await httpClient.GetStreamAsync(new Uri(url, UriKind.Relative), token);
|
||||
}
|
||||
// Same failure shapes as a record read (see GetRecordsAsync); an error status
|
||||
// surfaces as HttpRequestException too.
|
||||
catch (Exception exception) when (
|
||||
exception is HttpRequestException
|
||||
or TaskCanceledException { InnerException: TimeoutException })
|
||||
{
|
||||
throw new PocketBaseUnavailableException($"Could not read the file of document '{document.Slug}'.", exception);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether PocketBase answers its health endpoint.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user