reboot alterflixhub
This commit is contained in:
commit
1e84023a44
188 files changed
+31629
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import { defineEventHandler, getQuery } from "h3";
|
||||
|
||||
import { requireAdmin } from "@/lib/admin-auth.server";
|
||||
import { readEvents, summarize } from "@/lib/analytics.server";
|
||||
|
||||
/** สรุปสถิติการค้นหา — ใช้ในแท็บ "สถิติการค้นหา" ของหลังบ้าน */
|
||||
export default defineEventHandler(async (event) => {
|
||||
requireAdmin(event);
|
||||
const query = getQuery(event);
|
||||
const requested = Number(query.days);
|
||||
const days = [1, 7, 30, 90].includes(requested) ? requested : 7;
|
||||
return summarize(await readEvents(), days);
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { createError, defineEventHandler, readBody } from "h3";
|
||||
|
||||
import { requireAdmin } from "@/lib/admin-auth.server";
|
||||
import { readContent, sanitizeCinemas, writeContent } from "@/lib/content.server";
|
||||
|
||||
/**
|
||||
* วาง JSON ของโรงหนัง (ออบเจกต์เดียวหรือ array) เพื่อเพิ่ม/ทับรายการเดิม
|
||||
* ใช้กับปุ่ม "วาง JSON" ในหน้า admin — ทับเฉพาะ slug ที่ซ้ำกัน
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
requireAdmin(event);
|
||||
const body = (await readBody(event)) as { raw?: string };
|
||||
const raw = (body?.raw ?? "").trim();
|
||||
if (!raw) {
|
||||
throw createError({ statusCode: 400, statusMessage: "ยังไม่มีข้อมูลให้วาง" });
|
||||
}
|
||||
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: `JSON ไม่ถูกต้อง: ${error instanceof Error ? error.message : ""}`,
|
||||
});
|
||||
}
|
||||
|
||||
const incoming = sanitizeCinemas(Array.isArray(parsed) ? parsed : [parsed]);
|
||||
const current = await readContent();
|
||||
|
||||
const bySlug = new Map(current.cinemas.map((cinema) => [cinema.slug, cinema]));
|
||||
for (const cinema of incoming) bySlug.set(cinema.slug, cinema);
|
||||
|
||||
const next = {
|
||||
cinemas: Array.from(bySlug.values()),
|
||||
site: current.site,
|
||||
about: current.about,
|
||||
};
|
||||
await writeContent(next);
|
||||
return { imported: incoming.length, store: next };
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
import { defineEventHandler } from "h3";
|
||||
|
||||
import { requireAdmin } from "@/lib/admin-auth.server";
|
||||
import { readContent } from "@/lib/content.server";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
requireAdmin(event);
|
||||
return readContent();
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { createError, defineEventHandler, readBody } from "h3";
|
||||
|
||||
import { requireAdmin } from "@/lib/admin-auth.server";
|
||||
import {
|
||||
readContent,
|
||||
sanitizeAbout,
|
||||
sanitizeCinemas,
|
||||
sanitizeSite,
|
||||
writeContent,
|
||||
} from "@/lib/content.server";
|
||||
|
||||
/** บันทึกทั้งชุด: ส่งมาเฉพาะส่วนที่แก้ได้ (cinemas, site และ/หรือ about) */
|
||||
export default defineEventHandler(async (event) => {
|
||||
requireAdmin(event);
|
||||
const body = (await readBody(event)) as {
|
||||
cinemas?: unknown;
|
||||
site?: unknown;
|
||||
about?: unknown;
|
||||
};
|
||||
const current = await readContent();
|
||||
|
||||
try {
|
||||
const next = {
|
||||
cinemas:
|
||||
body?.cinemas === undefined
|
||||
? current.cinemas
|
||||
: sanitizeCinemas(body.cinemas),
|
||||
site: body?.site === undefined ? current.site : sanitizeSite(body.site),
|
||||
about:
|
||||
body?.about === undefined ? current.about : sanitizeAbout(body.about),
|
||||
};
|
||||
await writeContent(next);
|
||||
return next;
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage:
|
||||
error instanceof Error ? error.message : "ข้อมูลไม่ถูกต้อง",
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { createError, defineEventHandler, readBody } from "h3";
|
||||
|
||||
import { createAdminSession, isValidCredentials } from "@/lib/admin-auth.server";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = (await readBody(event)) as {
|
||||
username?: string;
|
||||
password?: string;
|
||||
};
|
||||
const username = (body?.username ?? "").trim();
|
||||
const password = body?.password ?? "";
|
||||
|
||||
if (!isValidCredentials(username, password)) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง",
|
||||
});
|
||||
}
|
||||
|
||||
createAdminSession(event);
|
||||
return { ok: true };
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
import { defineEventHandler } from "h3";
|
||||
|
||||
import { clearAdminSession, isAdmin } from "@/lib/admin-auth.server";
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
clearAdminSession(event);
|
||||
return { ok: true, authenticated: false, wasAuthenticated: isAdmin(event) };
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
import { defineEventHandler } from "h3";
|
||||
|
||||
import { isAdmin } from "@/lib/admin-auth.server";
|
||||
|
||||
export default defineEventHandler((event) => ({
|
||||
authenticated: isAdmin(event),
|
||||
}));
|
||||
@@ -0,0 +1,80 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { defineEventHandler, getHeader, getRequestIP, readBody } from "h3";
|
||||
|
||||
import { appendEvent } from "@/lib/analytics.server";
|
||||
import type { AnalyticsEvent, AnalyticsEventType } from "@/lib/analytics-types";
|
||||
|
||||
const ALLOWED_TYPES: AnalyticsEventType[] = [
|
||||
"search",
|
||||
"cinema_select",
|
||||
"cinema_view",
|
||||
"near_me",
|
||||
];
|
||||
|
||||
const MAX_EVENTS_PER_REQUEST = 20;
|
||||
const MAX_TEXT = 120;
|
||||
|
||||
function text(value: unknown): string | undefined {
|
||||
if (typeof value !== "string") return undefined;
|
||||
const trimmed = value.trim().slice(0, MAX_TEXT);
|
||||
return trimmed === "" ? undefined : trimmed;
|
||||
}
|
||||
|
||||
function count(value: unknown): number | undefined {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? Math.floor(parsed) : undefined;
|
||||
}
|
||||
|
||||
/** เกลือหมุนรายวัน — hash ของผู้เข้าชมจึงย้อนระบุตัวตนไม่ได้ */
|
||||
function visitorHash(ip: string, userAgent: string): string {
|
||||
const salt = `${new Date().toISOString().slice(0, 10)}${
|
||||
process.env.ANALYTICS_SALT ?? "alterflixhub-analytics"
|
||||
}`;
|
||||
return createHash("sha256").update(`${salt}|${ip}|${userAgent}`).digest("hex").slice(0, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* ตัวรับ event ฝั่งเว็บ (เทียบเท่า /api/send ของ Umami)
|
||||
* ไม่เก็บคุกกี้ ไม่เก็บ IP ดิบ — เก็บเฉพาะ hash ที่หมุนเกลือทุกวัน
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = (await readBody(event)) as
|
||||
| { events?: unknown }
|
||||
| Record<string, unknown>;
|
||||
const rawEvents = Array.isArray(body)
|
||||
? body
|
||||
: Array.isArray((body as { events?: unknown })?.events)
|
||||
? ((body as { events: unknown[] }).events)
|
||||
: [body];
|
||||
|
||||
const ip = getRequestIP(event, { xForwardedFor: true }) ?? "0.0.0.0";
|
||||
const userAgent = getHeader(event, "user-agent") ?? "";
|
||||
const visitor = visitorHash(ip, userAgent);
|
||||
const at = new Date().toISOString();
|
||||
|
||||
const accepted: AnalyticsEvent[] = [];
|
||||
for (const raw of rawEvents.slice(0, MAX_EVENTS_PER_REQUEST)) {
|
||||
if (!raw || typeof raw !== "object") continue;
|
||||
const entry = raw as Record<string, unknown>;
|
||||
const type = entry.type;
|
||||
if (typeof type !== "string" || !ALLOWED_TYPES.includes(type as AnalyticsEventType)) {
|
||||
continue;
|
||||
}
|
||||
accepted.push({
|
||||
type: type as AnalyticsEventType,
|
||||
at,
|
||||
visitor,
|
||||
locale: text(entry.locale) ?? "th",
|
||||
query: text(entry.query),
|
||||
category: text(entry.category),
|
||||
result_count: count(entry.result_count),
|
||||
slug: text(entry.slug),
|
||||
source: text(entry.source),
|
||||
position: count(entry.position),
|
||||
});
|
||||
}
|
||||
|
||||
for (const item of accepted) await appendEvent(item);
|
||||
return { ok: true, stored: accepted.length };
|
||||
});
|
||||
Reference in new issue
Block a user