feat: marketplace, économie à crédits, perks temps réel & pubs réelles
Transforme XIP en réseau social satirique complet : monnaie fictive, marketplace, cosmétiques visibles de tous, messages riches sandboxés, pubs pilotées par les données, et tous les compteurs mock rendus réels. Backend (Bun + Hono + Prisma + Redis) - Économie par IP : modèles Wallet/Purchase/Entitlement, lib/wallet.ts avec spend() atomique (point unique du paywall) + recharge gratuite. - isLocalhost() → mode gratuit (README « si localhost: pas de paywall »). - Marketplace : lib/catalog.ts (achat transactionnel, stock limité, limites par IP) + routes/shop.ts ; 10 produits seedés (idempotent). - Perks : lib/perks.ts (cache Redis busté à l'achat) ; authorPerks injecté dans les payloads messages + endpoint batch /api/perks ; frame WS « perks » global pour MAJ live des messages déjà affichés. - Messages riches : Message.richMode/richContent, gating par entitlement. - Pubs réelles : modèle Ad seedé avec les 4 pubs (ex-hardcodées), rotation par API, comptage d'impressions réel + réconciliation. - WebSocket : IP capturée par connexion → broadcastToIp / broadcast ; frames wallet/perks/ads/alert. - Pièces jointes : lib/storage.ts (UUID, jamais exécuté) + routes/uploads.ts (limite 1 Mo sauf déblocage/localhost, Content-Disposition: attachment). - Alerte audio : routes/alert.ts (cooldown serveur Redis NX, clamp durée). - Compteur « argent extorqué » réel : impressions×CPM + crédits dépensés. Frontend (Vue 3 + Vite) - /shop : ShopPage + ProductCard fidèles aux maquettes ; composables useWallet/useShop/usePerks/useAds/useAttachments/useAlert. - UI de réponse (bannière + sous-threads), solde + lien Shop dans le header. - Perks rendus : Style Doré (or), Pets autour de l'IP, NoAds masque les pubs. - RichContent.vue : iframe sandbox verrouillée (htmlcss sans script ; js allow-scripts seul, jamais allow-same-origin) + CSP. - AdBand/InlineCasinoAd pilotés par l'API ; barre de saisie avec 📎, compteur de caractères, composer riche et bouton alerte. Infra - Migration economy_ads_attachments_rich ; seed idempotent (produits+pubs). - vite.config : usePolling (HMR fiable sur /mnt/c via WSL). - backend/.gitignore : uploads/. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
93
backend/src/routes/uploads.ts
Normal file
93
backend/src/routes/uploads.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { Hono } from "hono";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { prisma } from "../lib/prisma";
|
||||
import { getClientIp, isLocalhost } from "../lib/ip";
|
||||
import { storeFile, absolutePathFor } from "../lib/storage";
|
||||
|
||||
const uploads = new Hono();
|
||||
|
||||
const FREE_LIMIT = 1_000_000; // 1 Mo for the free tier (README)
|
||||
const ABSOLUTE_MAX = 50_000_000; // hard cap even for paid, to protect the dev box
|
||||
|
||||
async function ownsNoFileLimit(ip: string): Promise<boolean> {
|
||||
if (isLocalhost(ip)) return true;
|
||||
const rows = await prisma.entitlement.findMany({
|
||||
where: { ip, kind: "no-file-limit", active: true },
|
||||
});
|
||||
return rows.length > 0;
|
||||
}
|
||||
|
||||
// POST /api/uploads (multipart) — store a file, return its metadata.
|
||||
uploads.post("/", async (c) => {
|
||||
const ip = getClientIp(c);
|
||||
|
||||
let body: Record<string, unknown>;
|
||||
try {
|
||||
body = await c.req.parseBody();
|
||||
} catch {
|
||||
return c.json({ error: "Upload invalide" }, 400);
|
||||
}
|
||||
const file = body["file"];
|
||||
if (!(file instanceof File)) {
|
||||
return c.json({ error: "Aucun fichier" }, 400);
|
||||
}
|
||||
|
||||
if (file.size > ABSOLUTE_MAX) {
|
||||
return c.json({ error: "Fichier trop volumineux (50 Mo max absolu)" }, 413);
|
||||
}
|
||||
if (file.size > FREE_LIMIT && !(await ownsNoFileLimit(ip))) {
|
||||
return c.json(
|
||||
{ error: "Fichier > 1 Mo : débloque « Fichiers illimités » dans le Shop 💸" },
|
||||
413
|
||||
);
|
||||
}
|
||||
|
||||
const id = randomUUID();
|
||||
let stored;
|
||||
try {
|
||||
stored = await storeFile(id, file);
|
||||
} catch {
|
||||
return c.json({ error: "Échec d'écriture" }, 500);
|
||||
}
|
||||
|
||||
const attachment = await prisma.attachment.create({
|
||||
data: {
|
||||
id,
|
||||
ip,
|
||||
filename: file.name || "fichier",
|
||||
mimeType: file.type || "application/octet-stream",
|
||||
size: file.size,
|
||||
storagePath: stored.storagePath,
|
||||
},
|
||||
select: { id: true, filename: true, mimeType: true, size: true },
|
||||
});
|
||||
|
||||
return c.json(attachment, 201);
|
||||
});
|
||||
|
||||
// GET /uploads/:id — serve the stored bytes. Images inline; everything else is
|
||||
// forced to download (never rendered same-origin, never executed).
|
||||
uploads.get("/:id", async (c) => {
|
||||
const id = c.req.param("id");
|
||||
const att = await prisma.attachment.findUnique({ where: { id } });
|
||||
if (!att) return c.json({ error: "Introuvable" }, 404);
|
||||
|
||||
let file;
|
||||
try {
|
||||
file = Bun.file(absolutePathFor(att.storagePath));
|
||||
} catch {
|
||||
return c.json({ error: "Introuvable" }, 404);
|
||||
}
|
||||
if (!(await file.exists())) return c.json({ error: "Introuvable" }, 404);
|
||||
|
||||
const isImage = att.mimeType.startsWith("image/");
|
||||
const headers: Record<string, string> = {
|
||||
// Images may render inline; anything else downloads. Never serve as HTML.
|
||||
"Content-Type": isImage ? att.mimeType : "application/octet-stream",
|
||||
"Content-Disposition": `${isImage ? "inline" : "attachment"}; filename="${att.filename.replace(/"/g, "")}"`,
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
};
|
||||
return new Response(file, { headers });
|
||||
});
|
||||
|
||||
export default uploads;
|
||||
Reference in New Issue
Block a user