初始化 M0:Astro SSR + node:sqlite 脚手架与只读页面
This commit is contained in:
1 parent
97440b4120
commit
8430e5304d
39 files changed
+6398
-1
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
|
||||
const DB_PATH = process.env.DB_PATH ?? 'data/app.db';
|
||||
const MIGRATIONS_DIR = 'migrations';
|
||||
|
||||
let instance: DatabaseSync | undefined;
|
||||
|
||||
/** 单例连接。首次调用时建目录、开 WAL、跑未应用的迁移。 */
|
||||
export function getDb(): DatabaseSync {
|
||||
if (!instance) {
|
||||
mkdirSync(dirname(DB_PATH), { recursive: true });
|
||||
instance = new DatabaseSync(DB_PATH);
|
||||
instance.exec('PRAGMA journal_mode = WAL;');
|
||||
instance.exec('PRAGMA foreign_keys = ON;');
|
||||
applyMigrations(instance);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
function applyMigrations(db: DatabaseSync): void {
|
||||
db.exec('CREATE TABLE IF NOT EXISTS _migrations (name TEXT PRIMARY KEY, applied_at TEXT NOT NULL)');
|
||||
|
||||
if (!existsSync(MIGRATIONS_DIR)) return;
|
||||
|
||||
const applied = new Set(
|
||||
(db.prepare('SELECT name FROM _migrations').all() as { name: string }[]).map((r) => r.name),
|
||||
);
|
||||
|
||||
const files = readdirSync(MIGRATIONS_DIR)
|
||||
.filter((f) => f.endsWith('.sql'))
|
||||
.sort();
|
||||
|
||||
for (const file of files) {
|
||||
if (applied.has(file)) continue;
|
||||
db.exec(readFileSync(join(MIGRATIONS_DIR, file), 'utf8'));
|
||||
db.prepare('INSERT INTO _migrations (name, applied_at) VALUES (?, ?)').run(
|
||||
file,
|
||||
new Date().toISOString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
import { getDb } from './db';
|
||||
import { chipSchema, deviceSchema, type Chip, type Device, type DeviceCategory } from './schema';
|
||||
|
||||
type Row = Record<string, unknown>;
|
||||
|
||||
function parseJson<T>(value: unknown, fallback: T): T {
|
||||
if (typeof value !== 'string' || value.length === 0) return fallback;
|
||||
try {
|
||||
return JSON.parse(value) as T;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
function toNullable(value: string | undefined | null): string | null {
|
||||
return value ?? null;
|
||||
}
|
||||
|
||||
function rowToDevice(row: Row): Device {
|
||||
return deviceSchema.parse({
|
||||
slug: row.slug,
|
||||
category: row.category,
|
||||
brand: row.brand,
|
||||
model: row.model,
|
||||
aliases: parseJson(row.aliases_json, []),
|
||||
announceDate: row.announce_date ?? undefined,
|
||||
releaseDate: row.release_date ?? undefined,
|
||||
lifecycle: row.lifecycle,
|
||||
cover: row.cover_key ?? undefined,
|
||||
gallery: parseJson(row.gallery_json, []),
|
||||
tags: parseJson(row.tags_json, []),
|
||||
summary: row.summary ?? undefined,
|
||||
chipRefs: parseJson(row.chip_refs_json, {}),
|
||||
meta: parseJson(row.meta_json, []),
|
||||
specs: parseJson(row.specs_json, {}),
|
||||
});
|
||||
}
|
||||
|
||||
function rowToChip(row: Row): Chip {
|
||||
return chipSchema.parse({
|
||||
slug: row.slug,
|
||||
type: row.type,
|
||||
brand: row.brand,
|
||||
model: row.model,
|
||||
codename: row.codename ?? undefined,
|
||||
aliases: parseJson(row.aliases_json, []),
|
||||
process: row.process ?? undefined,
|
||||
arch: row.arch ?? undefined,
|
||||
meta: parseJson(row.meta_json, []),
|
||||
detail: parseJson(row.detail_json, {}),
|
||||
});
|
||||
}
|
||||
|
||||
/* ------------------------------- devices ------------------------------- */
|
||||
|
||||
export function listDevices(options: { category?: DeviceCategory } = {}): Device[] {
|
||||
const db = getDb();
|
||||
const rows = options.category
|
||||
? db
|
||||
.prepare(
|
||||
'SELECT * FROM devices WHERE category = ? ORDER BY release_date DESC, brand, model',
|
||||
)
|
||||
.all(options.category)
|
||||
: db.prepare('SELECT * FROM devices ORDER BY release_date DESC, brand, model').all();
|
||||
return (rows as Row[]).map(rowToDevice);
|
||||
}
|
||||
|
||||
export function getDevice(slug: string): Device | undefined {
|
||||
const row = getDb().prepare('SELECT * FROM devices WHERE slug = ?').get(slug) as Row | undefined;
|
||||
return row ? rowToDevice(row) : undefined;
|
||||
}
|
||||
|
||||
export function countDevices(category?: DeviceCategory): number {
|
||||
const db = getDb();
|
||||
const row = category
|
||||
? (db.prepare('SELECT COUNT(*) AS n FROM devices WHERE category = ?').get(category) as { n: number })
|
||||
: (db.prepare('SELECT COUNT(*) AS n FROM devices').get() as { n: number });
|
||||
return row.n;
|
||||
}
|
||||
|
||||
/** 反查:搭载某颗芯片的设备(按 chipRefs 中任一引用匹配) */
|
||||
export function devicesUsingChip(chipSlug: string): Device[] {
|
||||
const rows = getDb()
|
||||
.prepare(
|
||||
`SELECT * FROM devices
|
||||
WHERE json_extract(chip_refs_json, '$.soc') = ?
|
||||
OR json_extract(chip_refs_json, '$.cpu') = ?
|
||||
OR json_extract(chip_refs_json, '$.gpu') = ?
|
||||
OR json_extract(chip_refs_json, '$.npu') = ?
|
||||
OR json_extract(chip_refs_json, '$.modem') = ?
|
||||
ORDER BY release_date DESC`,
|
||||
)
|
||||
.all(chipSlug, chipSlug, chipSlug, chipSlug, chipSlug);
|
||||
return (rows as Row[]).map(rowToDevice);
|
||||
}
|
||||
|
||||
export function saveDevice(input: Device): Device {
|
||||
const device = deviceSchema.parse(input);
|
||||
const now = new Date().toISOString();
|
||||
getDb()
|
||||
.prepare(
|
||||
`INSERT INTO devices (
|
||||
slug, category, sub_category, brand, model, aliases_json,
|
||||
announce_date, release_date, lifecycle, cover_key, gallery_json,
|
||||
tags_json, summary, specs_json, chip_refs_json, meta_json,
|
||||
created_at, updated_at
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(slug) DO UPDATE SET
|
||||
category = excluded.category,
|
||||
sub_category = excluded.sub_category,
|
||||
brand = excluded.brand,
|
||||
model = excluded.model,
|
||||
aliases_json = excluded.aliases_json,
|
||||
announce_date = excluded.announce_date,
|
||||
release_date = excluded.release_date,
|
||||
lifecycle = excluded.lifecycle,
|
||||
cover_key = excluded.cover_key,
|
||||
gallery_json = excluded.gallery_json,
|
||||
tags_json = excluded.tags_json,
|
||||
summary = excluded.summary,
|
||||
specs_json = excluded.specs_json,
|
||||
chip_refs_json = excluded.chip_refs_json,
|
||||
meta_json = excluded.meta_json,
|
||||
updated_at = excluded.updated_at`,
|
||||
)
|
||||
.run(
|
||||
device.slug,
|
||||
device.category,
|
||||
null,
|
||||
device.brand,
|
||||
device.model,
|
||||
JSON.stringify(device.aliases),
|
||||
toNullable(device.announceDate),
|
||||
toNullable(device.releaseDate),
|
||||
device.lifecycle,
|
||||
toNullable(device.cover),
|
||||
JSON.stringify(device.gallery),
|
||||
JSON.stringify(device.tags),
|
||||
toNullable(device.summary),
|
||||
JSON.stringify(device.specs),
|
||||
JSON.stringify(device.chipRefs),
|
||||
JSON.stringify(device.meta),
|
||||
now,
|
||||
now,
|
||||
);
|
||||
return getDevice(device.slug)!;
|
||||
}
|
||||
|
||||
/* -------------------------------- chips -------------------------------- */
|
||||
|
||||
export function listChips(options: { type?: Chip['type'] } = {}): Chip[] {
|
||||
const db = getDb();
|
||||
const rows = options.type
|
||||
? db.prepare('SELECT * FROM chips WHERE type = ? ORDER BY brand, model').all(options.type)
|
||||
: db.prepare('SELECT * FROM chips ORDER BY brand, model').all();
|
||||
return (rows as Row[]).map(rowToChip);
|
||||
}
|
||||
|
||||
export function getChip(slug: string): Chip | undefined {
|
||||
const row = getDb().prepare('SELECT * FROM chips WHERE slug = ?').get(slug) as Row | undefined;
|
||||
return row ? rowToChip(row) : undefined;
|
||||
}
|
||||
|
||||
export function countChips(type?: Chip['type']): number {
|
||||
const db = getDb();
|
||||
const row = type
|
||||
? (db.prepare('SELECT COUNT(*) AS n FROM chips WHERE type = ?').get(type) as { n: number })
|
||||
: (db.prepare('SELECT COUNT(*) AS n FROM chips').get() as { n: number });
|
||||
return row.n;
|
||||
}
|
||||
|
||||
export function saveChip(input: Chip): Chip {
|
||||
const chip = chipSchema.parse(input);
|
||||
const now = new Date().toISOString();
|
||||
getDb()
|
||||
.prepare(
|
||||
`INSERT INTO chips (
|
||||
slug, type, brand, model, codename, aliases_json,
|
||||
process, arch, detail_json, meta_json, created_at, updated_at
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(slug) DO UPDATE SET
|
||||
type = excluded.type,
|
||||
brand = excluded.brand,
|
||||
model = excluded.model,
|
||||
codename = excluded.codename,
|
||||
aliases_json = excluded.aliases_json,
|
||||
process = excluded.process,
|
||||
arch = excluded.arch,
|
||||
detail_json = excluded.detail_json,
|
||||
meta_json = excluded.meta_json,
|
||||
updated_at = excluded.updated_at`,
|
||||
)
|
||||
.run(
|
||||
chip.slug,
|
||||
chip.type,
|
||||
chip.brand,
|
||||
chip.model,
|
||||
toNullable(chip.codename),
|
||||
JSON.stringify(chip.aliases),
|
||||
toNullable(chip.process),
|
||||
toNullable(chip.arch),
|
||||
JSON.stringify(chip.detail),
|
||||
JSON.stringify(chip.meta),
|
||||
now,
|
||||
now,
|
||||
);
|
||||
return getChip(chip.slug)!;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import { z } from 'zod';
|
||||
import { phoneSpecsSchema } from './phone';
|
||||
import { socDetailSchema } from './soc';
|
||||
import { fieldMetaSchema, dateStringSchema } from './meta';
|
||||
|
||||
export const deviceCategorySchema = z.enum([
|
||||
'phone',
|
||||
'tablet',
|
||||
'laptop',
|
||||
'desktop',
|
||||
'handheld',
|
||||
'peripheral',
|
||||
]);
|
||||
export type DeviceCategory = z.infer<typeof deviceCategorySchema>;
|
||||
|
||||
export const lifecycleSchema = z.enum(['announced', 'released', 'discontinued']);
|
||||
export const chipTypeSchema = z.enum(['soc', 'cpu', 'gpu', 'npu', 'modem']);
|
||||
|
||||
export const slugSchema = z
|
||||
.string()
|
||||
.min(1)
|
||||
.regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, 'slug 只能用小写字母、数字和连字符');
|
||||
|
||||
export const chipRefsSchema = z
|
||||
.object({
|
||||
soc: z.string().optional(),
|
||||
cpu: z.string().optional(),
|
||||
gpu: z.string().optional(),
|
||||
npu: z.string().optional(),
|
||||
modem: z.string().optional(),
|
||||
})
|
||||
.default({});
|
||||
|
||||
const deviceBaseSchema = z.object({
|
||||
slug: slugSchema,
|
||||
brand: z.string().min(1),
|
||||
model: z.string().min(1),
|
||||
aliases: z.array(z.string()).default([]),
|
||||
announceDate: dateStringSchema.optional(),
|
||||
releaseDate: dateStringSchema.optional(),
|
||||
lifecycle: lifecycleSchema.default('released'),
|
||||
/** 存储 key,不是 URL */
|
||||
cover: z.string().optional(),
|
||||
gallery: z.array(z.string()).default([]),
|
||||
tags: z.array(z.string()).default([]),
|
||||
summary: z.string().optional(),
|
||||
chipRefs: chipRefsSchema,
|
||||
meta: z.array(fieldMetaSchema).default([]),
|
||||
});
|
||||
|
||||
/** M0 只实现 phone,后续 tablet/laptop/... 逐个加进 discriminatedUnion */
|
||||
export const phoneDeviceSchema = deviceBaseSchema.extend({
|
||||
category: z.literal('phone'),
|
||||
specs: phoneSpecsSchema,
|
||||
});
|
||||
export type PhoneDevice = z.infer<typeof phoneDeviceSchema>;
|
||||
|
||||
export const deviceSchema = z.discriminatedUnion('category', [phoneDeviceSchema]);
|
||||
export type Device = z.infer<typeof deviceSchema>;
|
||||
|
||||
const chipBaseSchema = z.object({
|
||||
slug: slugSchema,
|
||||
brand: z.string().min(1),
|
||||
model: z.string().min(1),
|
||||
codename: z.string().optional(),
|
||||
aliases: z.array(z.string()).default([]),
|
||||
process: z.string().optional(), // "TSMC N4P"
|
||||
arch: z.string().optional(), // "ARMv9.2"
|
||||
meta: z.array(fieldMetaSchema).default([]),
|
||||
});
|
||||
|
||||
export const socChipSchema = chipBaseSchema.extend({
|
||||
type: z.literal('soc'),
|
||||
detail: socDetailSchema,
|
||||
});
|
||||
export type SocChip = z.infer<typeof socChipSchema>;
|
||||
|
||||
export const chipSchema = z.discriminatedUnion('type', [socChipSchema]);
|
||||
export type Chip = z.infer<typeof chipSchema>;
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from './meta';
|
||||
export * from './phone';
|
||||
export * from './soc';
|
||||
export * from './device';
|
||||
@@ -0,0 +1,23 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/** 字段置信度:AI 分级录入的唯一依据 */
|
||||
export const confidenceSchema = z.enum(['high', 'medium', 'low']);
|
||||
export type Confidence = z.infer<typeof confidenceSchema>;
|
||||
|
||||
/**
|
||||
* 字段级元数据。path 是扁平路径(如 specs.rearCamera[0].sensorModel),
|
||||
* 与 AI 接口返回的字段路径一一对应。
|
||||
*/
|
||||
export const fieldMetaSchema = z.object({
|
||||
path: z.string(),
|
||||
confidence: confidenceSchema,
|
||||
sourceUrl: z.url().optional(),
|
||||
verified: z.boolean().default(false),
|
||||
capturedAt: z.string(),
|
||||
});
|
||||
export type FieldMeta = z.infer<typeof fieldMetaSchema>;
|
||||
|
||||
/** ISO 日期字符串(只到日或 datetime 均可) */
|
||||
export const dateStringSchema = z.string().refine((v) => !Number.isNaN(new Date(v).getTime()), {
|
||||
message: '不是有效日期',
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const extraSchema = z.record(z.string(), z.string()).default({});
|
||||
|
||||
export const cameraPositionSchema = z.enum([
|
||||
'wide',
|
||||
'ultrawide',
|
||||
'tele',
|
||||
'periscope',
|
||||
'macro',
|
||||
'depth',
|
||||
'mono',
|
||||
'front',
|
||||
]);
|
||||
|
||||
/** 单颗摄像头。多摄必须逐颗建模,才能检索与对比 */
|
||||
export const cameraModuleSchema = z.object({
|
||||
position: cameraPositionSchema,
|
||||
sensorModel: z.string().optional(), // "Sony IMX890"
|
||||
resolution: z.string().optional(), // "50MP"
|
||||
aperture: z.string().optional(), // "f/1.8"
|
||||
focalLength: z.string().optional(), // "24mm"
|
||||
sensorSize: z.string().optional(), // "1/1.56\""
|
||||
pixelSize: z.string().optional(), // "1.0µm"
|
||||
lensElements: z.string().optional(), // "7P"
|
||||
ois: z.boolean().optional(),
|
||||
eis: z.boolean().optional(),
|
||||
zoom: z.string().optional(), // "3x 光学 / 30x 数码"
|
||||
autofocusType: z.string().optional(), // "双像素 PDAF"
|
||||
video: z
|
||||
.object({
|
||||
max: z.string().optional(), // "8K@24fps"
|
||||
modes: z.array(z.string()).default([]),
|
||||
stabilization: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
flash: z.string().optional(),
|
||||
extra: extraSchema,
|
||||
});
|
||||
export type CameraModule = z.infer<typeof cameraModuleSchema>;
|
||||
|
||||
export const phoneSpecsSchema = z.object({
|
||||
body: z
|
||||
.object({
|
||||
dimensions: z.string().optional(), // "161.4 x 74.6 x 8.2 mm"
|
||||
weight: z.string().optional(), // "193 g"
|
||||
build: z
|
||||
.object({
|
||||
front: z.string().optional(),
|
||||
back: z.string().optional(),
|
||||
frame: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
sim: z.string().optional(),
|
||||
ipRating: z.string().optional(), // "IP68"
|
||||
colors: z.array(z.string()).default([]),
|
||||
buttons: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
display: z
|
||||
.object({
|
||||
size: z.string().optional(), // "6.79 英寸"
|
||||
resolution: z.string().optional(), // "2712 x 1220"
|
||||
aspectRatio: z.string().optional(),
|
||||
panel: z.string().optional(), // "LTPO AMOLED"
|
||||
refreshRate: z.string().optional(), // "120Hz"
|
||||
refreshRange: z.string().optional(), // "1-120Hz"
|
||||
touchSampling: z.string().optional(),
|
||||
brightnessTypical: z.string().optional(),
|
||||
brightnessPeak: z.string().optional(),
|
||||
hdr: z.string().optional(),
|
||||
protection: z.string().optional(), // "康宁大猩猩 Victus 2"
|
||||
pwmDimming: z.string().optional(),
|
||||
alwaysOn: z.boolean().optional(),
|
||||
})
|
||||
.optional(),
|
||||
platform: z
|
||||
.object({
|
||||
os: z.string().optional(),
|
||||
skin: z.string().optional(),
|
||||
chipset: z.string().optional(), // 冗余展示用,权威值在 chipRefs.soc
|
||||
})
|
||||
.optional(),
|
||||
memory: z
|
||||
.object({
|
||||
ram: z.string().optional(),
|
||||
ramType: z.string().optional(), // "LPDDR5X"
|
||||
storage: z.string().optional(),
|
||||
storageType: z.string().optional(), // "UFS 4.0"
|
||||
cardSlot: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
rearCamera: z.array(cameraModuleSchema).default([]),
|
||||
frontCamera: z.array(cameraModuleSchema).default([]),
|
||||
/** 振动马达:X轴/Z轴线性、转子;型号如 "AAC 0809" */
|
||||
vibration: z
|
||||
.object({
|
||||
type: z.string().optional(),
|
||||
model: z.string().optional(),
|
||||
size: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
speakers: z
|
||||
.object({
|
||||
count: z.number().optional(),
|
||||
stereo: z.boolean().optional(),
|
||||
units: z
|
||||
.array(
|
||||
z.object({
|
||||
model: z.string().optional(),
|
||||
size: z.string().optional(),
|
||||
power: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
dolbyAtmos: z.boolean().optional(),
|
||||
hiRes: z.boolean().optional(),
|
||||
loudnessDba: z.string().optional(),
|
||||
headphoneJack: z.boolean().optional(),
|
||||
})
|
||||
.optional(),
|
||||
connectivity: z
|
||||
.object({
|
||||
cellular: z.string().optional(),
|
||||
wifi: z.string().optional(),
|
||||
bluetooth: z.string().optional(),
|
||||
nfc: z.boolean().optional(),
|
||||
gps: z.string().optional(),
|
||||
usb: z.string().optional(),
|
||||
ir: z.boolean().optional(),
|
||||
uwb: z.boolean().optional(),
|
||||
})
|
||||
.optional(),
|
||||
battery: z
|
||||
.object({
|
||||
capacity: z.string().optional(), // "5400mAh"
|
||||
type: z.string().optional(),
|
||||
wired: z.string().optional(),
|
||||
wireless: z.string().optional(),
|
||||
reverse: z.string().optional(),
|
||||
chargingProtocol: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
sensors: z
|
||||
.object({
|
||||
fingerprint: z.string().optional(),
|
||||
fingerprintType: z.string().optional(), // 超声波 / 光学
|
||||
fingerprintPosition: z.string().optional(),
|
||||
gyro: z.boolean().optional(),
|
||||
barometer: z.boolean().optional(),
|
||||
colorSensor: z.boolean().optional(),
|
||||
flickerSensor: z.boolean().optional(),
|
||||
})
|
||||
.optional(),
|
||||
software: z
|
||||
.object({
|
||||
launchVersion: z.string().optional(),
|
||||
currentVersion: z.string().optional(),
|
||||
updatePolicy: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
extra: extraSchema,
|
||||
});
|
||||
export type PhoneSpecs = z.infer<typeof phoneSpecsSchema>;
|
||||
@@ -0,0 +1,70 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* SoC 详情。与设备 specs 分开维护,供多机型引用。
|
||||
*/
|
||||
export const socDetailSchema = z.object({
|
||||
cpuClusters: z
|
||||
.array(
|
||||
z.object({
|
||||
name: z.string().optional(), // "性能簇" / "能效簇"
|
||||
cores: z
|
||||
.array(
|
||||
z.object({
|
||||
microArch: z.string().optional(), // "Cortex-X4"
|
||||
count: z.number().optional(),
|
||||
clockMax: z.string().optional(), // "3.3GHz"
|
||||
l1: z.string().optional(),
|
||||
l2: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
l3: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
gpuBlock: z
|
||||
.object({
|
||||
name: z.string().optional(), // "Adreno 750"
|
||||
arch: z.string().optional(),
|
||||
cores: z.number().optional(),
|
||||
clockMax: z.string().optional(),
|
||||
apis: z.array(z.string()).default([]),
|
||||
})
|
||||
.optional(),
|
||||
npuBlock: z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
tops: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
ispBlock: z.string().optional(),
|
||||
dspBlock: z.string().optional(),
|
||||
modemBlock: z.string().optional(),
|
||||
memorySupport: z
|
||||
.object({
|
||||
type: z.string().optional(), // "LPDDR5X"
|
||||
maxClock: z.string().optional(),
|
||||
bandwidth: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
storageSupport: z.string().optional(), // "UFS 4.0"
|
||||
displaySupport: z.string().optional(), // 最高分辨率/刷新率
|
||||
videoCodec: z
|
||||
.object({
|
||||
encode: z.array(z.string()).default([]),
|
||||
decode: z.array(z.string()).default([]),
|
||||
})
|
||||
.optional(),
|
||||
benchmarks: z
|
||||
.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
score: z.string(),
|
||||
unit: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
extra: z.record(z.string(), z.string()).default({}),
|
||||
});
|
||||
export type SocDetail = z.infer<typeof socDetailSchema>;
|
||||
@@ -0,0 +1,65 @@
|
||||
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
|
||||
export interface StorageDriver {
|
||||
put(key: string, buf: Uint8Array, contentType: string): Promise<void>;
|
||||
delete(key: string): Promise<void>;
|
||||
publicUrl(key: string): string;
|
||||
}
|
||||
|
||||
const LOCAL_ROOT = process.env.UPLOADS_DIR ?? 'data/uploads';
|
||||
|
||||
/**
|
||||
* 本地磁盘驱动。key 形如 `devices/xiaomi-14/cover.webp`,
|
||||
* 落盘到 data/uploads 下,URL 由 /uploads/[...key] 端点提供。
|
||||
*/
|
||||
class LocalDriver implements StorageDriver {
|
||||
async put(key: string, buf: Uint8Array, _contentType: string): Promise<void> {
|
||||
const target = join(LOCAL_ROOT, key);
|
||||
await mkdir(join(target, '..'), { recursive: true });
|
||||
await writeFile(target, buf);
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<void> {
|
||||
await rm(join(LOCAL_ROOT, key), { force: true });
|
||||
}
|
||||
|
||||
publicUrl(key: string): string {
|
||||
return `/uploads/${key}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* S3 兼容驱动占位。M2 接入(@aws-sdk/client-s3 或 SigV4 手写),
|
||||
* 配置项:S3_ENDPOINT / S3_BUCKET / S3_REGION / S3_KEY / S3_SECRET / S3_PUBLIC_BASE_URL
|
||||
*/
|
||||
class S3Driver implements StorageDriver {
|
||||
private readonly base: string;
|
||||
constructor() {
|
||||
this.base = process.env.S3_PUBLIC_BASE_URL ?? '';
|
||||
}
|
||||
async put(): Promise<void> {
|
||||
throw new Error('S3 驱动尚未实现(M2 接入)');
|
||||
}
|
||||
async delete(): Promise<void> {
|
||||
throw new Error('S3 驱动尚未实现(M2 接入)');
|
||||
}
|
||||
publicUrl(key: string): string {
|
||||
if (!this.base) throw new Error('缺少 S3_PUBLIC_BASE_URL');
|
||||
return `${this.base.replace(/\/$/, '')}/${key}`;
|
||||
}
|
||||
}
|
||||
|
||||
let driver: StorageDriver | undefined;
|
||||
|
||||
export function getStorage(): StorageDriver {
|
||||
if (!driver) {
|
||||
const kind = process.env.STORAGE_DRIVER ?? 'local';
|
||||
driver = kind === 's3' ? new S3Driver() : new LocalDriver();
|
||||
}
|
||||
return driver;
|
||||
}
|
||||
|
||||
export function storageRoot(): string {
|
||||
return LOCAL_ROOT;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from 'clsx';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
Reference in new issue
Block a user