-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
122 lines (111 loc) · 4.53 KB
/
Copy pathindex.tsx
File metadata and controls
122 lines (111 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 文件管理器 - 主入口
import { Script, Intent, Navigation, Path } from "scripting";
import { getFileInfo, readTextFile } from "./manager/utils";
import { FilePreviewView } from "./view/FilePreview";
import { ArchiveBrowserPage, ImageViewer, VideoViewerPage, LivePhotoPreviewPage } from "./view/MediaViewer";
import { HomeView } from "./view/preview";
import { openEditorDirectly } from "./view/EditorDirectly";
import { EditorPage } from "./view/EditorPage";
import { resolveOpenerForFile } from "./view/DefaultOpenerPicker";
/** 清理 _intent_transfer/ 中超过 maxAgeMs 的旧文件 */
async function cleanupTransferDir(maxAgeMs = 60 * 1000) {
const dir = Path.join(FileManager.appGroupDocumentsDirectory, "_intent_transfer");
try {
if (!(await FileManager.exists(dir))) return;
const entries = await FileManager.readDirectory(dir);
const now = Date.now();
for (const name of entries) {
try {
const fullPath = Path.join(dir, name);
const stat = await FileManager.stat(fullPath);
// modificationDate 可能是秒或毫秒,统一转为毫秒比较
const raw = stat.modificationDate;
if (!raw) continue; // modificationDate 为 0 表示 stat 异常,跳过不删
const modMs = raw > 1e12 ? raw : raw * 1000;
if (now - modMs > maxAgeMs) {
await FileManager.remove(fullPath);
}
} catch {}
}
} catch {}
}
async function run() {
// URL Scheme 跳转回来的是刚 copy 的文件,跳过清理避免误删
const queryFileURL = Script.queryParameters?.fileURL as string | undefined;
if (!queryFileURL) {
await cleanupTransferDir();
}
// 检查是否有通过 Intent 传入的文件,或通过 URL Scheme 跳转回来的大文件
const intentFiles = Intent.fileURLsParameter;
const filePath = intentFiles?.[0] ?? queryFileURL;
if (filePath) {
try {
const fileInfo = await getFileInfo(filePath);
const cat = fileInfo.category;
// ── 来自 URL Scheme 的大文件(跳转回来的):和 intent.tsx 一样用 resolveOpenerForFile ──
if (queryFileURL) {
const prefix = await resolveOpenerForFile(filePath, cat);
if (!prefix) {
// 清理临时文件
try { await FileManager.remove(filePath); } catch {}
Script.exit();
return;
}
// 不在这里删文件 — Navigation.present 只是挂载 view,
// EditorPage 等 viewer 的 useEffect 还没读文件内容。
// 由下次启动时 cleanupTransferDir() 统一清理旧文件。
if (prefix === "editor:" || prefix === "preview:") {
await Navigation.present({
element: (
<EditorPage
path={filePath}
fileName={fileInfo.name}
fileSize={fileInfo.size}
mode="present"
/>
),
modalPresentationStyle: prefix === "editor:" ? "overFullScreen" : undefined,
});
} else if (prefix === "archive:") {
await Navigation.present({ element: <ArchiveBrowserPage filePath={filePath} /> });
} else if (prefix === "video:") {
await Navigation.present({ element: <VideoViewerPage filePath={filePath} /> });
} else if (prefix === "image:") {
await Navigation.present({ element: <ImageViewer filePath={filePath} /> });
} else if (prefix === "livephoto:") {
await Navigation.present({ element: <LivePhotoPreviewPage livePath={filePath} /> });
} else {
await Navigation.present({
element: <FilePreviewView fileInfo={fileInfo} content={null} />,
modalPresentationStyle: "fullScreen",
});
}
Script.exit();
return;
}
// ── 来自 Intent 的文件:保持原有逻辑 ──
let content: string | null = null;
if (cat === "text" || cat === "code" || cat === "data") {
content = await readTextFile(filePath);
}
if (content !== null) {
await openEditorDirectly(fileInfo, content);
Script.exit();
return;
}
await Navigation.present({
element: cat === "archive" ? <ArchiveBrowserPage filePath={filePath} /> : <FilePreviewView fileInfo={fileInfo} content={content} />,
modalPresentationStyle: "overFullScreen",
});
Script.exit();
return;
} catch (e) {
console.log("预览文件失败:", e);
}
}
await Navigation.present({
element: <HomeView />,
modalPresentationStyle: "overFullScreen",
});
}
run();