feat(assets): Bild-Upload ins verbundene Git-Repo (#10) (#26)
Image / Image bauen und pushen (push) Successful in 4m29s
CI / App (npm ci + Build, ESLint, Stylelint, Jest) (push) Successful in 2m40s
CI / Storage (mongo) (push) Successful in 1m35s
CI / Storage (postgres) (push) Successful in 1m30s

Upload-Endpunkt POST /api/git/brews/:editId/assets (Magic Bytes, konfigurierbare Limits HB_ASSET_*, Dedupe, Commit ins Repo, Snapshot in brew_assets) und Auslieferung /git-assets/:shareId/:sha256.:ext gemäß ADR 0004. Postgres-Migration 0003, Mongo-Repo, Editor-Button, Drag-and-drop und Einfügen.

Closes #10

Co-authored-by: Dev (Paperclip) <dev-bot@noreply.git.morlana.online>
This commit was merged in pull request #26.
This commit is contained in:
dev-bot Bot authored and Gitea committed 2026-11-26 01:39:34 +00:00
1 parent bbf838c5d6
commit aeca3926a3
34 files changed
+1353 -11

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
// Repository für Bild-Snapshots (PostgreSQL, ADR 0004 §3). Gleicher Vertrag wie mongo/assets.js.
const UUID = /^[0-9a-f-]{36}$/i;
const META = ['id', 'brew_id', 'sha256', 'repo_path', 'commit_sha', 'content_type', 'size', 'created_at'];
const rowToAsset = (r)=>!r ? null : ({
id : r.id,
brewId : r.brew_id,
sha256 : r.sha256,
repoPath : r.repo_path,
commitSha : r.commit_sha,
contentType : r.content_type,
size : r.size,
createdAt : r.created_at,
...(r.data ? { data: Buffer.from(r.data) } : {})
});
export const createBrewAssetRepository = (db)=>({
// Legt das Asset an, falls (brewId, sha256) noch fehlt. Liefert in jedem Fall den gespeicherten Datensatz (ohne data).
async create({ brewId, sha256, repoPath, commitSha, contentType, size, data }) {
await db.insertInto('brew_assets')
.values({ brew_id: brewId, sha256, repo_path: repoPath, commit_sha: commitSha, content_type: contentType, size, data })
.onConflict((oc)=>oc.columns(['brew_id', 'sha256']).doNothing()).execute();
return this.findByBrewAndHash(brewId, sha256);
},
async findByBrewAndHash(brewId, sha256, { withData = false } = {}) {
if(!UUID.test(String(brewId))) return null;
const cols = withData ? [...META, 'data'] : META;
return rowToAsset(await db.selectFrom('brew_assets').select(cols).where('brew_id', '=', brewId).where('sha256', '=', sha256).executeTakeFirst());
},
async listByBrew(brewId) {
if(!UUID.test(String(brewId))) return [];
return (await db.selectFrom('brew_assets').select(META).where('brew_id', '=', brewId).orderBy('created_at').orderBy('id').execute()).map(rowToAsset);
},
async sumSizeByBrew(brewId) {
if(!UUID.test(String(brewId))) return 0;
const row = await db.selectFrom('brew_assets').select((eb)=>eb.fn.coalesce(eb.fn.sum('size'), eb.val(0)).as('total')).where('brew_id', '=', brewId).executeTakeFirst();
return Number(row?.total ?? 0);
},
async deleteByBrew(brewId) {
if(!UUID.test(String(brewId))) return 0;
return Number((await db.deleteFrom('brew_assets').where('brew_id', '=', brewId).executeTakeFirst()).numDeletedRows);
}
});
+3
View File
@@ -3,6 +3,7 @@ import { createBrewRepository } from './brews.js';
import { createNotificationRepository } from './notifications.js';
import { createUserRepository } from './users.js';
import { createGitConnectionRepository, createBrewGitLinkRepository } from './gitRepos.js';
import { createBrewAssetRepository } from './assets.js';
// PostgreSQL-Implementierung der Storage-Schicht (ADR 0002). Kysely + pg, versionierte Migrationen
// laufen beim Start (migrateToLatest). Konfiguration: HB_POSTGRES_URL, HB_POSTGRES_POOL_MAX.
@@ -17,6 +18,7 @@ export const createPostgresStorage = (config, overrides = {})=>{
repos.users = createUserRepository(db);
repos.gitConnections = createGitConnectionRepository(db);
repos.brewGitLinks = createBrewGitLinkRepository(db);
repos.brewAssets = createBrewAssetRepository(db);
};
return {
@@ -26,6 +28,7 @@ export const createPostgresStorage = (config, overrides = {})=>{
get users() { return repos.users; },
get gitConnections() { return repos.gitConnections; },
get brewGitLinks() { return repos.brewGitLinks; },
get brewAssets() { return repos.brewAssets; },
get pool() { return handle?.pool; }, // nur für Tests
async connect() {
const url = overrides.url ?? config?.get('hb_postgres_url');
@@ -0,0 +1,22 @@
import { sql } from 'kysely';
// ADR 0004 §3: Snapshot der hochgeladenen Bilder, damit die App sie ohne Git-Zugriff ausliefert.
export const up = async (db)=>{
await sql`
CREATE TABLE brew_assets (
id uuid PRIMARY KEY DEFAULT uuidv7(),
brew_id uuid NOT NULL REFERENCES brews(id) ON DELETE CASCADE,
sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
repo_path text NOT NULL,
commit_sha text NOT NULL,
content_type text NOT NULL,
size integer NOT NULL,
data bytea NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (brew_id, sha256)
)`.execute(db);
};
export const down = async (db)=>{
await sql`DROP TABLE IF EXISTS brew_assets`.execute(db);
};
+3 -1
View File
@@ -2,8 +2,10 @@
// Neue Migration: Datei `NNNN_beschreibung.js` mit up(db)/down(db) anlegen und hier eintragen.
import * as m0001 from './0001_initial.js';
import * as m0002 from './0002_git_backend.js';
import * as m0003 from './0003_brew_assets.js';
export default {
'0001_initial' : m0001,
'0002_git_backend' : m0002
'0002_git_backend' : m0002,
'0003_brew_assets' : m0003
};
+8
View File
@@ -3,6 +3,7 @@ import pg from 'pg';
import { createPostgresStorage } from './index.js';
import { runUserRepositoryContract } from '../contract/users.contract.js';
import { runGitRepositoryContract } from '../contract/git.contract.js';
import { runAssetRepositoryContract } from '../contract/assets.contract.js';
import { runBrewRepositoryContract, runNotificationRepositoryContract } from '../contract/brews.contract.js';
const url = process.env.HB_TEST_POSTGRES_URL;
@@ -55,6 +56,13 @@ suite('Postgres Git-Repositories (Contract)', ()=>{
});
});
suite('Postgres Asset-Repository (Contract)', ()=>{
runAssetRepositoryContract(async ()=>{
const { storage, cleanup } = await createStorage();
return { brews: storage.brews, brewAssets: storage.brewAssets, cleanup };
});
});
suite('Postgres Verbindung und Migrationen', ()=>{
test('Start ohne HB_POSTGRES_URL bricht mit klarer Meldung ab', async ()=>{
const storage = createPostgresStorage({ get: ()=>undefined });