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>
63 lines
2.6 KiB
JavaScript
63 lines
2.6 KiB
JavaScript
import { randomUUID } from 'crypto';
|
|
import mongoose from 'mongoose';
|
|
|
|
// Fork-Entität "BrewAsset" (ADR 0004 §3): Bild-Snapshot pro Brew. Eigene Collection, kein Upstream-Modell.
|
|
const AssetSchema = new mongoose.Schema({
|
|
id : { type: String, required: true, unique: true },
|
|
brewId : { type: String, required: true },
|
|
sha256 : { type: String, required: true },
|
|
repoPath : { type: String, required: true },
|
|
commitSha : { type: String, required: true },
|
|
contentType : { type: String, required: true },
|
|
size : { type: Number, required: true },
|
|
data : { type: Buffer, required: true },
|
|
createdAt : { type: Date, default: Date.now }
|
|
}, { collection: 'brewassets', versionKey: false });
|
|
AssetSchema.index({ brewId: 1, sha256: 1 }, { unique: true });
|
|
|
|
export const getAssetModel = (connection = mongoose.connection)=>connection.models.ForkBrewAsset ?? connection.model('ForkBrewAsset', AssetSchema);
|
|
|
|
const toPlain = (doc, withData)=>{
|
|
if(!doc) return null;
|
|
const o = doc.toObject ? doc.toObject() : doc;
|
|
return {
|
|
id : o.id,
|
|
brewId : o.brewId,
|
|
sha256 : o.sha256,
|
|
repoPath : o.repoPath,
|
|
commitSha : o.commitSha,
|
|
contentType : o.contentType,
|
|
size : o.size,
|
|
createdAt : o.createdAt,
|
|
...(withData && o.data ? { data: Buffer.from(o.data.buffer ?? o.data) } : {})
|
|
};
|
|
};
|
|
|
|
export const createBrewAssetRepository = (connection = mongoose.connection)=>{
|
|
const Asset = getAssetModel(connection);
|
|
const NO_DATA = { data: 0 };
|
|
return {
|
|
Model : Asset,
|
|
// 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 Asset.updateOne({ brewId: String(brewId), sha256 },
|
|
{ $setOnInsert: { id: randomUUID(), repoPath, commitSha, contentType, size, data, createdAt: new Date() } },
|
|
{ upsert: true });
|
|
return this.findByBrewAndHash(brewId, sha256);
|
|
},
|
|
async findByBrewAndHash(brewId, sha256, { withData = false } = {}) {
|
|
return toPlain(await Asset.findOne({ brewId: String(brewId), sha256 }, withData ? undefined : NO_DATA), withData);
|
|
},
|
|
async listByBrew(brewId) {
|
|
return (await Asset.find({ brewId: String(brewId) }, NO_DATA).sort({ createdAt: 1, id: 1 })).map((d)=>toPlain(d, false));
|
|
},
|
|
async sumSizeByBrew(brewId) {
|
|
const [row] = await Asset.aggregate([{ $match: { brewId: String(brewId) } }, { $group: { _id: null, total: { $sum: '$size' } } }]);
|
|
return row?.total ?? 0;
|
|
},
|
|
async deleteByBrew(brewId) {
|
|
return (await Asset.deleteMany({ brewId: String(brewId) })).deletedCount;
|
|
}
|
|
};
|
|
};
|