'use strict'; // 3D avatar models (VRM). Ready-made anime characters whose licence allows // any use (VRoid samples, CC0 community models) are downloaded on first use // from a pinned commit of github.com/ruslanmv/3D-Avatar-Chatbot (Apache-2.0) // and checked against their SHA-256; the user can also upload their own VRM // (e.g. from VRoid Hub, respecting its licence). Kept in ~/.cc-web/avatar. const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const { pipeline } = require('stream/promises'); const { Transform, Readable } = require('stream'); const BASE = 'https://raw.githubusercontent.com/ruslanmv/3D-Avatar-Chatbot/2aa957f4dba43dc363a3d387b60df55b95bd2a91/vendor/avatars'; const MAX_UPLOAD = 80 * 1024 * 1024; const ID_RE = /^[a-z0-9-]{1,40}$/; const CATALOG = [ { id: 'vivi', name: 'Vivi', file: 'Vivi.vrm', sha256: 'eaf902e041a7a810f1423599ae75682f61184ab6ef0206da9a8ed9caa8ec3a9d', size: 18089136, license: 'CC0', note: 'Kahküllü kısa saç, tatlı' }, { id: 'avatar-a', name: 'AvatarSample A', file: 'AvatarSample_A.vrm', sha256: 'b86b0b8a66d48911431d6f920a5211a974226f83aa672eca3f3dfade58ac346e', size: 15000000, license: 'VRoid örneği (herkese, ticari dahil serbest)', note: 'Kahverengi saç, sade' }, { id: 'shino', name: 'Sendagaya Shino', file: 'Sendagaya_Shino.vrm', sha256: '1e177c1a7b14f783a9c48395831db8616260d3bddd4154cb2784b779adca49b5', size: 15074328, license: 'CC0', note: 'Uzun siyah saç' }, { id: 'victoria', name: 'Victoria Rubin', file: 'Victoria_Rubin.vrm', sha256: 'b1372131bdbf233f46320146d565f342a7e4f6f4b8f2aefb301f1a283ec07e1e', size: 18000000, license: 'CC0', note: 'Sarı-pembe saç' }, { id: 'vita', name: 'Vita', file: 'Vita.vrm', sha256: 'f2bf78f28a24e2f75f5ca0b6c3b646654c394e4b03592dfaa0d0633ef0972b4d', size: 18000000, license: 'CC0', note: 'Beyaz-mavi saç, iki renkli göz' }, { id: 'avatar-b', name: 'AvatarSample B', file: 'AvatarSample_B.vrm', sha256: '4a271bd3b5a3d19e054fd113ee154635b72e7141f4a8ccbcdba3c7f9cea6ee8d', size: 15000000, license: 'VRoid örneği (herkese, ticari dahil serbest)', note: 'Mor saç, kedi kulağı tokalı' }, ]; function httpError(status, message) { const err = new Error(message); err.status = status; return err; } class AvatarModels { constructor({ home }) { this.dir = path.join(home, '.cc-web', 'avatar', 'models'); this.downloads = new Map(); } file(id) { return path.join(this.dir, `${id}.vrm`); } list() { const out = CATALOG.map((m) => ({ id: m.id, name: m.name, license: m.license, note: m.note, size: m.size, builtin: true, ready: fs.existsSync(this.file(m.id)), downloading: this.downloads.has(m.id) })); let files = []; try { files = fs.readdirSync(this.dir); } catch { files = []; } for (const f of files) { const m = /^custom-([a-z0-9-]{1,30})\.vrm$/.exec(f); if (!m) continue; const st = fs.statSync(path.join(this.dir, f)); out.push({ id: `custom-${m[1]}`, name: `Yüklenen: ${m[1]}`, license: 'senin modelin', note: '', size: st.size, builtin: false, ready: true }); } return out; } // Downloads a catalog model once (concurrent requests share the download). ensure(id) { if (fs.existsSync(this.file(id))) return Promise.resolve(true); const entry = CATALOG.find((m) => m.id === id); if (!entry) return Promise.reject(httpError(404, 'Model bulunamadı.')); if (this.downloads.has(id)) return this.downloads.get(id); const job = (async () => { fs.mkdirSync(this.dir, { recursive: true }); const res = await fetch(`${BASE}/${entry.file}`, { signal: AbortSignal.timeout(10 * 60 * 1000) }); if (!res.ok || !res.body) throw httpError(502, `Model indirilemedi (HTTP ${res.status}).`); const tmp = `${this.file(id)}.part`; const hash = crypto.createHash('sha256'); const tap = new Transform({ transform(chunk, _e, cb) { hash.update(chunk); cb(null, chunk); }, }); await pipeline(Readable.fromWeb(res.body), tap, fs.createWriteStream(tmp)); if (hash.digest('hex') !== entry.sha256) { fs.rmSync(tmp, { force: true }); throw httpError(502, 'İndirilen model doğrulanamadı (SHA-256 uyuşmadı).'); } fs.renameSync(tmp, this.file(id)); return true; })().finally(() => this.downloads.delete(id)); this.downloads.set(id, job); return job; } async pathFor(id) { if (!ID_RE.test(String(id))) throw httpError(400, 'Geçersiz model.'); if (!id.startsWith('custom-')) await this.ensure(id); const file = this.file(id); if (!fs.existsSync(file)) throw httpError(404, 'Model bulunamadı.'); return file; } async upload(req, rawName) { const declared = Number(req.headers['content-length']); if (declared > MAX_UPLOAD) throw httpError(413, 'Model çok büyük (en fazla 80 MB).'); const slug = String(rawName || 'model').toLowerCase().replace(/\.vrm$/, '').normalize('NFKD').replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 30) || 'model'; fs.mkdirSync(this.dir, { recursive: true }); const id = `custom-${slug}`; const tmp = `${this.file(id)}.part`; let size = 0; let head = Buffer.alloc(0); const meter = new Transform({ transform(chunk, _e, cb) { size += chunk.length; if (head.length < 4) head = Buffer.concat([head, chunk.subarray(0, 4)]); if (size > MAX_UPLOAD) cb(httpError(413, 'Model çok büyük (en fazla 80 MB).')); else cb(null, chunk); }, }); try { await pipeline(req, meter, fs.createWriteStream(tmp)); } catch (err) { fs.rmSync(tmp, { force: true }); throw err.status ? err : httpError(400, 'Yükleme yarıda kesildi.'); } // VRM is a binary glTF: it starts with "glTF". if (head.toString('ascii', 0, 4) !== 'glTF') { fs.rmSync(tmp, { force: true }); throw httpError(400, 'Bu bir VRM/GLB dosyası değil.'); } fs.renameSync(tmp, this.file(id)); return { id, size }; } remove(id) { if (!ID_RE.test(String(id))) throw httpError(400, 'Geçersiz model.'); fs.rmSync(this.file(id), { force: true }); return { ok: true }; } } module.exports = { AvatarModels, CATALOG };