exportasyncfunctionPOST(req:Request){constjson=req.headers.get("accept")?.includes("application/json");try{constuser=awaitrequireUser();constf=awaitreq.formData();constcontent=String(f.get("content")||"").trim();constvisibility=String(f.get("visibility")||"PUBLIC");constsourceId=Number(f.get("sourceId"));consttags=String(f.get("tags")||"").split(/\s*,\s*/).filter(Boolean).map(t=>t.replace(/^#/,""));if(!content||!['PRIVATE','PROTECTED','PUBLIC'].includes(visibility)||!sourceId)thrownewError("Invalid post");constsource=db.prepare("SELECT s.id FROM sources s JOIN source_members sm ON sm.source_id=s.id WHERE s.id=? AND sm.user_id=? AND s.is_enabled=1").get(sourceId,user.id);if(!source)thrownewError("Source not available");constmax=Number(process.env.UPLOAD_MAX_BYTES||10485760);constfiles=f.getAll('attachments').filter((x):xisFile=>xinstanceofFile&&x.size>0);constattachments:any[]=[];awaitmkdir(join(process.cwd(),'public','uploads'),{recursive:true});for(constfileoffiles){if(file.size>max)thrownewError(`${file.name} exceeds upload limit`);constid=randomUUID()+extname(file.name);awaitwriteFile(join(process.cwd(),'public','uploads',id),Buffer.from(awaitfile.arrayBuffer()));attachments.push({name:file.name,url:`/uploads/${id}`,type:file.type,size:file.size});}constout=db.prepare("INSERT INTO posts(source_id,author_id,content,visibility,tags_json,attachments_json,origin,sync_status) VALUES(?,?,?,?,?,?,'hub','queued')").run(sourceId,user.id,content,visibility,JSON.stringify(tags),JSON.stringify(attachments));db.prepare("INSERT INTO sync_jobs(source_id,kind,payload_json,trigger) VALUES(?, 'push', ?, 'manual')").run(sourceId,JSON.stringify({postId:out.lastInsertRowid}));if(json)returnNextResponse.json({id:Number(out.lastInsertRowid)},{status:201});returnNextResponse.redirect(externalUrl(req,`/posts/${out.lastInsertRowid}`));}catch(e){constmessage=einstanceofError?e.message:'post';if(json)returnNextResponse.json({error:message},{status:400});returnNextResponse.redirect(externalUrl(req,'/dashboard?error='+encodeURIComponent(message)));}}