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
@@ -11,6 +11,8 @@ export const useAssetUpload = ({ editId, containerRef, inject, active = true })=
|
||||
const [linked, setLinked] = useState(false);
|
||||
const [state, setState] = useState({ busy: false, percent: null, name: '', errors: [], done: 0 });
|
||||
const busyRef = useRef(false);
|
||||
const injectRef = useRef(inject);
|
||||
injectRef.current = inject;
|
||||
|
||||
useEffect(()=>{
|
||||
let live = true;
|
||||
@@ -36,10 +38,10 @@ export const useAssetUpload = ({ editId, containerRef, inject, active = true })=
|
||||
const { markdown, errors } = await uploadFiles(editId, files, cfg, {
|
||||
onProgress : ({ percent, name })=>setState((s)=>({ ...s, percent, name }))
|
||||
});
|
||||
if(markdown.length) inject(`${markdown.join('\n')}\n`);
|
||||
if(markdown.length) injectRef.current(`${markdown.join('\n')}\n`);
|
||||
busyRef.current = false;
|
||||
setState({ busy: false, percent: null, name: '', errors, done: markdown.length });
|
||||
}, [available, editId, cfg, inject]);
|
||||
}, [available, editId, cfg]);
|
||||
|
||||
// Drag-and-drop und Einfügen im Text-Editor (Capture-Phase, vor CodeMirror). Nur Bilddateien, Text bleibt unberührt.
|
||||
useEffect(()=>{
|
||||
|
||||
@@ -264,7 +264,7 @@ const MetadataEditor = ({
|
||||
defaultValue={metadata.lang || ''}
|
||||
keepValueOnClose={true}
|
||||
placeholder='en'
|
||||
valuePatterns='^[a-z]{2,3}(-[A-Z][a-z]{3})?(-[A-Z]{2})?$'
|
||||
valuePatterns="^[a-z]{2,3}(-[A-Z][a-z]{3})?(-[A-Z]{2})?$"
|
||||
onSelect={(value)=>handleLanguage(value)}
|
||||
onEntry={(e)=>{
|
||||
e.target.setCustomValidity(''); //Clear the validation popup while typing
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -217,6 +217,16 @@ describe('Prüfungen und Fehler', ()=>{
|
||||
expect(remote.log(dir, 'main').filter((l)=>l.includes('Add asset'))).toHaveLength(2);
|
||||
});
|
||||
|
||||
test('zwei parallele Uploads am Brew-Limit: einer 201, einer 413 A03 (kein Race)', async ()=>{
|
||||
build({ HB_ASSET_MAX_BYTES_PER_BREW: String(pngVariant(1).length + 1) });
|
||||
const { brew, dir } = await linkedBrew();
|
||||
const [a, b] = await Promise.all([upload(brew.editId, pngVariant(1)), upload(brew.editId, pngVariant(2))]);
|
||||
expect([a.status, b.status].sort()).toEqual([201, 413]);
|
||||
expect([a, b].find((r)=>r.status === 413).body.HBErrorCode).toBe('A03');
|
||||
expect(await storage.brewAssets.listByBrew(brew.id)).toHaveLength(1);
|
||||
expect(remote.log(dir, 'main').filter((l)=>l.includes('Add asset'))).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('leerer Body: 400 A06', async ()=>{
|
||||
const { brew } = await linkedBrew();
|
||||
const res = await upload(brew.editId, Buffer.alloc(0));
|
||||
|
||||
Reference in new issue
Block a user