本地化改造:免登录 + 本机 AI + 离线可用
在上游 gitee.com/676015863/unicut 基础上做的自托管改造,使其可脱离官方账号体系 与 CDN 独立运行。详见 CHANGELOG.md。 主要能力 - 免登录:GUEST_USER 直接使用核心功能,checkAuth 不再拦截 - 本机 AI:对话 mimo-v2.6-flash(同源 /newapi/v1)、视觉 deepseek-v4-flash-vision-exp、 生图 /omniai、生视频 /civideo;密钥仅存服务端(nginx 占位符 + entrypoint 注入) - AI 一键出片:图片+标题 → AI 文案 → 人声/语速/情绪 → 确认生成直接排时间轴, 并支持转场、图片运镜、字幕位置/大小/配色/入场动画 - 离线可用:工程数据 IndexedDB + 项目文件 JSON 双写;官方素材走同源 /cdn/ 反代; 无 OPFS 时素材回落 IndexedDB files 表 修复 - 账号类接口本地短路,不再反复弹「Header parameter [authorization] cannot be empty」 - isFSApiSupported 改取值判断,修 OPFS 判定错误导致卡在文件夹引导页 - 模板素材统一 /cdn/ 改写 + nginx 兜底转发,修缩略图全裂 - 剔除素材已失效的模板(上游 CDN 已删,点开必卡 loading 50%) - 补齐 SDK 内置 4 张滤镜贴图,修画布卡 loading 0% - 封面上传失败不再中断整次保存 品牌与页面 - 「云剪」改为 UniCut - 首页删除收费标准栏目;顶部菜单移除 GitHub/Gitee;底部栏整体下线 安全 - .env 与 public/local-ai.json(含密钥)已在 .gitignore,不入库 - public/local-ai.json.example 仅含占位符
This commit is contained in:
1 parent
e0f2d47c51
commit
59241af720
362 files changed
+6054
-1254
No files matched your search
@@ -5,9 +5,41 @@ import type { MovieData } from 'video-core-sdk';
|
||||
|
||||
const ROOT_HANDLE_KEY = 'root';
|
||||
|
||||
/** 检查 File System Access API 是否可用 */
|
||||
/**
|
||||
* 检查 File System Access API 是否可用。
|
||||
*
|
||||
* 必须用 window.showDirectoryPicker(取值判断),不能写成 'showDirectoryPicker' in window:
|
||||
* in 只要属性名存在于原型链上就为真 —— 某些环境下该方法虽然被禁用/未实现,
|
||||
* 属性名仍在原型上,in 判定为 true,于是 FolderGuard 会一直停在「选择本地存储文件夹」
|
||||
* 引导页,编辑器根本进不去(表现为打开就白屏/只有弹窗)。
|
||||
*/
|
||||
export function isFSApiSupported(): boolean {
|
||||
return typeof window !== 'undefined' && 'showDirectoryPicker' in window;
|
||||
if (typeof window === 'undefined') return false;
|
||||
try {
|
||||
return typeof (window as any).showDirectoryPicker === 'function';
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地保存是否可用 —— 判据是 IndexedDB,不是 File System Access API。
|
||||
*
|
||||
* 为什么不能用 isFSApiSupported():showDirectoryPicker 只在 https / localhost 这类「安全上下文」
|
||||
* 里存在,用 http://192.168.x.x 打开站点时它为 false,整条本地链路会被判为不可用,
|
||||
* 保存/作品列表随即退回官方远端接口;而远端要登录 token,免登录下必然返回
|
||||
* "Header parameter [authorization] cannot be empty",自动保存每 10 秒一次,用户就会被反复弹窗打断。
|
||||
*
|
||||
* 工程数据本来就先写 IndexedDB(saveProject),文件落盘只是可选增强(没选文件夹时自动跳过),
|
||||
* 所以只要 IndexedDB 可用,本地保存就成立。
|
||||
*/
|
||||
export function canUseLocalSave(): boolean {
|
||||
if (typeof window === 'undefined') return false;
|
||||
try {
|
||||
return !!window.indexedDB;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取持久化的文件夹句柄 */
|
||||
@@ -133,6 +165,48 @@ async function copyFileToLocal(
|
||||
return { relativePath: `${dirName ? dirName + '/' : ''}${actualFileName}`, fileName: actualFileName };
|
||||
}
|
||||
|
||||
/**
|
||||
* 把文件字节落到本地:有目录句柄就写真实文件(便于备份带走),
|
||||
* 没有句柄(http://IP 这类非安全上下文,OPFS 不可用)就写进 IndexedDB 的 files 表。
|
||||
*
|
||||
* 返回相对路径;两边都写不进去才返回 null。
|
||||
*/
|
||||
export async function saveLocalFileBlob(relativePath: string, blob: Blob | File): Promise<string | null> {
|
||||
const root = await getRootHandle();
|
||||
if (root) {
|
||||
const result = await copyFileToLocal(root, blob, relativePath, relativePath.split('/').pop() || 'file');
|
||||
return result.relativePath;
|
||||
}
|
||||
try {
|
||||
await localDB.files.put({ path: relativePath, blob, createdAt: Date.now() });
|
||||
return relativePath;
|
||||
} catch (err) {
|
||||
console.error('写入本地文件失败(IndexedDB 兜底也失败):', relativePath, err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 按相对路径读文件字节:优先目录句柄,取不到再从 IndexedDB 的 files 表读 */
|
||||
export async function readLocalFileBlob(relativePath: string): Promise<Blob | null> {
|
||||
const root = await getRootHandle();
|
||||
if (root) {
|
||||
try {
|
||||
const parts = relativePath.split('/').filter(Boolean);
|
||||
const fileName = parts.pop()!;
|
||||
let dir = root;
|
||||
for (const seg of parts) {
|
||||
dir = await dir.getDirectoryHandle(seg);
|
||||
}
|
||||
const fileHandle = await dir.getFileHandle(fileName);
|
||||
return await fileHandle.getFile();
|
||||
} catch {
|
||||
/* 目录里没有,继续走 IndexedDB 兜底 */
|
||||
}
|
||||
}
|
||||
const rec = await localDB.files.get(relativePath);
|
||||
return rec?.blob || null;
|
||||
}
|
||||
|
||||
/** 将文件写入本地存储的指定相对路径(供工程包导入使用),root 可传入已获取的句柄避免重复查询 */
|
||||
export async function saveFileToLocal(
|
||||
file: File | Blob,
|
||||
@@ -156,9 +230,6 @@ export function createLocalUploadBase64(relativeDir: string) {
|
||||
name: string;
|
||||
file_type?: 'image' | 'video' | 'audio' | 'json' | 'txt' | 'font';
|
||||
}): Promise<[{ storage_path: string }, null]> => {
|
||||
const root = await getRootHandle();
|
||||
if (!root) throw new Error('未选择本地存储文件夹');
|
||||
|
||||
const mimeMap: Record<string, string> = {
|
||||
json: 'application/json',
|
||||
txt: 'text/plain',
|
||||
@@ -170,8 +241,10 @@ export function createLocalUploadBase64(relativeDir: string) {
|
||||
const mimeType = mimeMap[params.file_type || ''] || 'image/png';
|
||||
const blob = base64ToBlob(params.content, mimeType);
|
||||
|
||||
const result = await copyFileToLocal(root, blob, `${relativeDir}/${params.name}`, params.name);
|
||||
return [{ storage_path: result.relativePath }, null];
|
||||
// 有目录句柄写文件,没有就写 IndexedDB(http://IP 打开时 OPFS 不可用)
|
||||
const storagePath = await saveLocalFileBlob(`${relativeDir}/${params.name}`, blob);
|
||||
if (!storagePath) throw new Error('本地存储空间不可用,请检查浏览器存储设置');
|
||||
return [{ storage_path: storagePath }, null];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -190,12 +263,7 @@ export async function addMaterial(params: {
|
||||
from?: 'user' | 'system';
|
||||
tag?: string;
|
||||
}): Promise<MaterialRecord | null> {
|
||||
const root = await getRootHandle();
|
||||
if (!root) {
|
||||
Toast.error('未选择本地存储文件夹,请刷新页面重试');
|
||||
return null;
|
||||
}
|
||||
|
||||
// 没有目录句柄(http://IP 打开时 OPFS 不可用)也照常入库:文件字节走 IndexedDB 兜底
|
||||
const { appId, name, type, file, thumbPath, framesPath, wavePath, attrs, from, tag } = params;
|
||||
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
||||
const now = Date.now();
|
||||
@@ -206,8 +274,13 @@ export async function addMaterial(params: {
|
||||
const relativeDir = `materials/${appId}/${type}`;
|
||||
const relativePath = `${relativeDir}/${safeFileName}`;
|
||||
|
||||
// 复制主文件
|
||||
const fileResult = await copyFileToLocal(root, file, relativePath, safeFileName);
|
||||
// 复制主文件(有目录句柄写文件,没有写 IndexedDB)
|
||||
const savedPath = await saveLocalFileBlob(relativePath, file);
|
||||
if (!savedPath) {
|
||||
Toast.error('本地存储空间不可用,请检查浏览器存储设置');
|
||||
return null;
|
||||
}
|
||||
const fileResult = { relativePath: savedPath, fileName: safeFileName };
|
||||
|
||||
// 从路径中提取文件名(路径格式:materials/appId/type/xxx.png)
|
||||
const extractNameFromPath = (p: string) => p.split('/').pop();
|
||||
@@ -452,6 +525,17 @@ export async function deleteMaterial(id: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// 同步清掉 IndexedDB 里的文件字节(无目录句柄时的落点),避免越积越多
|
||||
const blobPaths = [record.relativePath, record.thumbRelativePath, record.framesRelativePath, record.waveRelativePath]
|
||||
.filter(Boolean) as string[];
|
||||
for (const p of blobPaths) {
|
||||
try {
|
||||
await localDB.files.delete(p);
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
}
|
||||
|
||||
await localDB.materials.delete(id);
|
||||
}
|
||||
|
||||
@@ -487,26 +571,35 @@ export async function getMaterialFile(relativePath: string): Promise<File | null
|
||||
}
|
||||
|
||||
const root = await getRootHandle();
|
||||
if (!root) return null;
|
||||
|
||||
try {
|
||||
const parts = relativePath.split('/').filter(Boolean);
|
||||
const fileName = parts.pop()!;
|
||||
const dirPath = parts.join('/');
|
||||
if (root) {
|
||||
try {
|
||||
const parts = relativePath.split('/').filter(Boolean);
|
||||
const fileName = parts.pop()!;
|
||||
const dirPath = parts.join('/');
|
||||
|
||||
let dir = root;
|
||||
if (dirPath) {
|
||||
for (const seg of dirPath.split('/')) {
|
||||
dir = await dir.getDirectoryHandle(seg);
|
||||
let dir = root;
|
||||
if (dirPath) {
|
||||
for (const seg of dirPath.split('/')) {
|
||||
dir = await dir.getDirectoryHandle(seg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fileHandle = await dir.getFileHandle(fileName);
|
||||
return await fileHandle.getFile();
|
||||
const fileHandle = await dir.getFileHandle(fileName);
|
||||
return await fileHandle.getFile();
|
||||
} catch (err) {
|
||||
/* 目录里没有,走下面的 IndexedDB 兜底 */
|
||||
}
|
||||
}
|
||||
|
||||
// 没有目录句柄(http://IP)或文件不在目录里:从 IndexedDB 的 files 表读
|
||||
try {
|
||||
const rec = await localDB.files.get(relativePath);
|
||||
if (rec?.blob) return new File([rec.blob], relativePath.split('/').pop() || 'file');
|
||||
} catch (err) {
|
||||
console.warn('读取本地素材文件失败:', relativePath, err);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 读取素材文件为 URL */
|
||||
@@ -517,33 +610,44 @@ export async function getMaterialFileUrl(relativePath: string): Promise<string |
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
// 目录句柄优先;没有句柄或文件不在目录里时,回退到 IndexedDB 的 files 表
|
||||
const root = await getRootHandle();
|
||||
|
||||
if (!root) return null;
|
||||
if (root) {
|
||||
try {
|
||||
const parts = relativePath.split('/').filter(Boolean);
|
||||
const fileName = parts.pop()!;
|
||||
const dirPath = parts.join('/');
|
||||
|
||||
let dir = root;
|
||||
if (dirPath) {
|
||||
for (const seg of dirPath.split('/')) {
|
||||
dir = await dir.getDirectoryHandle(seg);
|
||||
}
|
||||
}
|
||||
|
||||
const fileHandle = await dir.getFileHandle(fileName);
|
||||
const file = await fileHandle.getFile();
|
||||
return URL.createObjectURL(file);
|
||||
} catch (err) {
|
||||
/* 目录里没有,走下面的 IndexedDB 兜底 */
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const parts = relativePath.split('/').filter(Boolean);
|
||||
const fileName = parts.pop()!;
|
||||
const dirPath = parts.join('/');
|
||||
|
||||
let dir = root;
|
||||
if (dirPath) {
|
||||
for (const seg of dirPath.split('/')) {
|
||||
dir = await dir.getDirectoryHandle(seg);
|
||||
}
|
||||
}
|
||||
|
||||
const fileHandle = await dir.getFileHandle(fileName);
|
||||
const file = await fileHandle.getFile();
|
||||
return URL.createObjectURL(file);
|
||||
const rec = await localDB.files.get(relativePath);
|
||||
if (rec?.blob) return URL.createObjectURL(rec.blob);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
console.error('读取素材文件失败:', relativePath);
|
||||
Toast.error('读取素材文件失败:' + relativePath);
|
||||
return null;
|
||||
console.error('读取本地文件(IndexedDB)失败:', relativePath, err);
|
||||
}
|
||||
|
||||
console.error('读取素材文件失败:', relativePath);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 工程 JSON 落盘的串行队列:自动保存每 10 秒触发一次,避免并发写同一个文件互相打断
|
||||
const projectFileQueue = new Map<string, Promise<void>>();
|
||||
|
||||
/** 保存项目数据 */
|
||||
export async function saveProject(appId: string, data: any): Promise<void> {
|
||||
const existing = await localDB.projects.get(appId);
|
||||
@@ -557,12 +661,53 @@ export async function saveProject(appId: string, data: any): Promise<void> {
|
||||
createdAt: existing?.createdAt || Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
// 同步把工程数据写一份到项目文件夹(projects/<id>.json)。
|
||||
// IndexedDB 只存在于当前浏览器:换设备、清站点数据、换浏览器都会丢工程;
|
||||
// 落成文件后可以直接备份/同步/带走。写失败不影响已入库的数据。
|
||||
const prev = projectFileQueue.get(appId) || Promise.resolve();
|
||||
const next = prev
|
||||
.catch(() => {})
|
||||
.then(() => writeProjectFile(appId, data).catch(e => console.warn('工程 JSON 落盘失败:', appId, e)));
|
||||
projectFileQueue.set(appId, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
/** 获取项目数据 */
|
||||
/** 把工程数据写入 projects/<appId>.json */
|
||||
async function writeProjectFile(appId: string, data: any): Promise<void> {
|
||||
const root = await getRootHandle();
|
||||
if (!root) return; // 未选择本地文件夹(仅内存模式)时跳过
|
||||
const body = JSON.stringify(
|
||||
{ id: appId, version: 1, updatedAt: Date.now(), title: data?.title || '', width: data?.width, height: data?.height, data },
|
||||
null,
|
||||
2,
|
||||
);
|
||||
const dir = await ensureDir(root, 'projects');
|
||||
const fileHandle = await dir.getFileHandle(`${appId}.json`, { create: true });
|
||||
const writable = await fileHandle.createWritable();
|
||||
await writable.write(new Blob([body], { type: 'application/json' }));
|
||||
await writable.close();
|
||||
}
|
||||
|
||||
/** 获取项目数据:优先 IndexedDB,缺失时从文件夹里的 projects/<id>.json 读回 */
|
||||
export async function getProject(appId: string): Promise<any | null> {
|
||||
const record = await localDB.projects.get(appId);
|
||||
return record?.data ?? null;
|
||||
if (record?.data) return record.data;
|
||||
return readProjectFile(appId);
|
||||
}
|
||||
|
||||
/** 从 projects/<appId>.json 读回工程数据(跨设备/清站点数据后恢复用) */
|
||||
async function readProjectFile(appId: string): Promise<any | null> {
|
||||
const root = await getRootHandle();
|
||||
if (!root) return null;
|
||||
try {
|
||||
const dir = await ensureDir(root, 'projects');
|
||||
const fileHandle = await dir.getFileHandle(`${appId}.json`);
|
||||
const file = await fileHandle.getFile();
|
||||
const parsed = JSON.parse(await file.text());
|
||||
return parsed?.data ?? null;
|
||||
} catch (e) {
|
||||
return null; // 文件也不存在就当作没有该工程
|
||||
}
|
||||
}
|
||||
|
||||
/** 删除项目数据 */
|
||||
|
||||
Reference in new issue
Block a user