fix(assets): Review PR #26 – Upload unter Brew-Lock, inject per Ref, metadataEditor zurückgenommen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c57bef85c9
commit
1ff86709b8
4 files changed
+39
-23
No files matched your search
@@ -53,28 +53,32 @@ export const createAssetService = ({ cfg, gitSvc, getStorage, logger = console }
|
||||
throw new AssetError('A02', `Der Dateityp stimmt nicht mit dem Inhalt überein (erkannt: ${type.toUpperCase()}).`);
|
||||
|
||||
const sha256 = crypto.createHash('sha256').update(body).digest('hex');
|
||||
const existing = await storage().brewAssets.findByBrewAndHash(brew.id, sha256);
|
||||
if(existing) return { created: false, asset: { ...existing, filename }, brew };
|
||||
|
||||
const used = await storage().brewAssets.sumSizeByBrew(brew.id);
|
||||
if(used + body.length > cfg.maxBytesPerBrew)
|
||||
throw new AssetError('A03', `Das Speicherlimit für Bilder dieses Brews (${formatBytes(cfg.maxBytesPerBrew)}) wäre überschritten. Belegt: ${formatBytes(used)}.`);
|
||||
|
||||
const repoPath = buildRepoPath({ assetsDir: conn.assetsDir, filename, sha256, type });
|
||||
const author = await gitSvc.authorOf(account);
|
||||
const { commit } = await gitSvc.locks.withKey(brew.id, ()=>gitSvc.commitContent(conn, {
|
||||
branch : link.branch,
|
||||
filePath : repoPath,
|
||||
content : body,
|
||||
author,
|
||||
message : `Add asset ${repoPath} via Homebrewery`,
|
||||
maxBytes : cfg.maxBytes,
|
||||
ignoreConflict : true // inhaltsadressierter Pfad: ein vorhandener Blob ist derselbe Inhalt
|
||||
}));
|
||||
|
||||
// Snapshot erst nach erfolgreichem Push (ADR 0004 §2)
|
||||
const asset = await storage().brewAssets.create({ brewId: brew.id, sha256, repoPath, commitSha: commit, contentType: mimeFor(type), size: body.length, data: body });
|
||||
return { created: true, asset: { ...asset, filename }, brew };
|
||||
// Dedupe, Limitprüfung, Commit und Snapshot unter einem Lock pro Brew, sonst Race beim Brew-Limit
|
||||
return gitSvc.locks.withKey(brew.id, async ()=>{
|
||||
const existing = await storage().brewAssets.findByBrewAndHash(brew.id, sha256);
|
||||
if(existing) return { created: false, asset: { ...existing, filename }, brew };
|
||||
|
||||
const used = await storage().brewAssets.sumSizeByBrew(brew.id);
|
||||
if(used + body.length > cfg.maxBytesPerBrew)
|
||||
throw new AssetError('A03', `Das Speicherlimit für Bilder dieses Brews (${formatBytes(cfg.maxBytesPerBrew)}) wäre überschritten. Belegt: ${formatBytes(used)}.`);
|
||||
|
||||
const repoPath = buildRepoPath({ assetsDir: conn.assetsDir, filename, sha256, type });
|
||||
const { commit } = await gitSvc.commitContent(conn, {
|
||||
branch : link.branch,
|
||||
filePath : repoPath,
|
||||
content : body,
|
||||
author,
|
||||
message : `Add asset ${repoPath} via Homebrewery`,
|
||||
maxBytes : cfg.maxBytes,
|
||||
ignoreConflict : true // inhaltsadressierter Pfad: ein vorhandener Blob ist derselbe Inhalt
|
||||
});
|
||||
|
||||
// Snapshot erst nach erfolgreichem Push (ADR 0004 §2)
|
||||
const asset = await storage().brewAssets.create({ brewId: brew.id, sha256, repoPath, commitSha: commit, contentType: mimeFor(type), size: body.length, data: body });
|
||||
return { created: true, asset: { ...asset, filename }, brew };
|
||||
});
|
||||
},
|
||||
|
||||
toResponse : result
|
||||
|
||||
Reference in new issue
Block a user