@xcanwin/manyoyo 6.2.6 → 6.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/web/frontend/app.css
CHANGED
|
@@ -1224,6 +1224,23 @@ body.workspace-switcher-open .workspace-switcher-panel {
|
|
|
1224
1224
|
line-height: 1.6;
|
|
1225
1225
|
}
|
|
1226
1226
|
|
|
1227
|
+
.files-markdown-preview {
|
|
1228
|
+
padding: 4px 6px;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
.files-image-preview {
|
|
1232
|
+
display: flex;
|
|
1233
|
+
justify-content: center;
|
|
1234
|
+
padding: 12px;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
.files-image-preview img {
|
|
1238
|
+
max-width: 100%;
|
|
1239
|
+
height: auto;
|
|
1240
|
+
border-radius: 8px;
|
|
1241
|
+
border: 1px solid rgba(181, 146, 99, 0.2);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1227
1244
|
.files-editor-host {
|
|
1228
1245
|
min-height: 100%;
|
|
1229
1246
|
}
|
|
@@ -104,6 +104,7 @@
|
|
|
104
104
|
<div class="files-preview-meta" data-role="preview-meta">请选择左侧文件或目录</div>
|
|
105
105
|
</div>
|
|
106
106
|
<div class="files-preview-actions">
|
|
107
|
+
<button type="button" class="secondary" data-action="toggle-markdown-view" hidden>查看源码</button>
|
|
107
108
|
<button type="button" class="secondary" data-action="save" disabled>保存</button>
|
|
108
109
|
</div>
|
|
109
110
|
</header>
|
|
@@ -122,6 +123,7 @@
|
|
|
122
123
|
const visitBtn = root.querySelector('[data-action="visit"]');
|
|
123
124
|
const mkdirBtn = root.querySelector('[data-action="mkdir"]');
|
|
124
125
|
const saveBtn = root.querySelector('[data-action="save"]');
|
|
126
|
+
const toggleMarkdownViewBtn = root.querySelector('[data-action="toggle-markdown-view"]');
|
|
125
127
|
|
|
126
128
|
const state = {
|
|
127
129
|
visible: false,
|
|
@@ -144,7 +146,8 @@
|
|
|
144
146
|
editor: null,
|
|
145
147
|
editorHost: null,
|
|
146
148
|
previewReadOnly: true,
|
|
147
|
-
previewDirty: false
|
|
149
|
+
previewDirty: false,
|
|
150
|
+
markdownViewMode: 'rendered'
|
|
148
151
|
};
|
|
149
152
|
|
|
150
153
|
function setStatus(text) {
|
|
@@ -161,6 +164,12 @@
|
|
|
161
164
|
state.editorHost = null;
|
|
162
165
|
}
|
|
163
166
|
|
|
167
|
+
function getPreviewLanguage() {
|
|
168
|
+
return state.selectedFile
|
|
169
|
+
? (state.selectedFile.language || inferLanguageFromPath(state.selectedFile.path))
|
|
170
|
+
: '';
|
|
171
|
+
}
|
|
172
|
+
|
|
164
173
|
function isEditablePreview() {
|
|
165
174
|
return Boolean(
|
|
166
175
|
state.selectedFile
|
|
@@ -168,6 +177,7 @@
|
|
|
168
177
|
&& state.previewReadOnly === false
|
|
169
178
|
&& state.historyOnly !== true
|
|
170
179
|
&& state.savingFile !== true
|
|
180
|
+
&& (getPreviewLanguage() !== 'markdown' || state.markdownViewMode === 'source')
|
|
171
181
|
);
|
|
172
182
|
}
|
|
173
183
|
|
|
@@ -179,6 +189,28 @@
|
|
|
179
189
|
saveBtn.textContent = state.savingFile ? '保存中...' : '保存';
|
|
180
190
|
}
|
|
181
191
|
|
|
192
|
+
function syncMarkdownToggleButton() {
|
|
193
|
+
if (!toggleMarkdownViewBtn) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const isMarkdown = Boolean(state.selectedFile)
|
|
197
|
+
&& state.selectedFile.kind === 'text'
|
|
198
|
+
&& getPreviewLanguage() === 'markdown';
|
|
199
|
+
toggleMarkdownViewBtn.hidden = !isMarkdown;
|
|
200
|
+
toggleMarkdownViewBtn.textContent = state.markdownViewMode === 'source' ? '查看渲染' : '查看源码';
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function resolveMarkdownImageUrl(basePath, relativeHref) {
|
|
204
|
+
try {
|
|
205
|
+
const lastSlash = String(basePath || '').lastIndexOf('/');
|
|
206
|
+
const baseDir = lastSlash >= 0 ? basePath.slice(0, lastSlash + 1) : '/';
|
|
207
|
+
const resolvedPath = new URL(relativeHref, 'http://manyoyo-internal' + baseDir).pathname;
|
|
208
|
+
return '/api/sessions/' + encodeURIComponent(state.sessionName) + '/fs/raw?path=' + encodeURIComponent(resolvedPath);
|
|
209
|
+
} catch (e) {
|
|
210
|
+
return '';
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
182
214
|
function renderPreviewEmpty(title, description) {
|
|
183
215
|
state.selectedFile = null;
|
|
184
216
|
state.previewReadOnly = true;
|
|
@@ -193,6 +225,7 @@
|
|
|
193
225
|
previewBodyNode.innerHTML = `<div class="files-empty">${escapeHtml(description)}</div>`;
|
|
194
226
|
}
|
|
195
227
|
destroyEditor();
|
|
228
|
+
syncMarkdownToggleButton();
|
|
196
229
|
syncSaveButton();
|
|
197
230
|
}
|
|
198
231
|
|
|
@@ -226,12 +259,34 @@
|
|
|
226
259
|
previewMetaNode.textContent = `${payload.kind === 'text' ? '文本文件' : '文件'} · ${formatBytes(payload.size)} · ${modeLabel}${payload.truncated ? ' · 已截断预览' : ''}`;
|
|
227
260
|
}
|
|
228
261
|
if (!previewBodyNode) {
|
|
262
|
+
syncMarkdownToggleButton();
|
|
229
263
|
syncSaveButton();
|
|
230
264
|
return;
|
|
231
265
|
}
|
|
232
266
|
|
|
233
267
|
if (payload.kind === 'text') {
|
|
234
268
|
const language = payload.language || inferLanguageFromPath(payload.path);
|
|
269
|
+
const showMarkdownRendered = language === 'markdown'
|
|
270
|
+
&& state.markdownViewMode !== 'source'
|
|
271
|
+
&& window.ManyoyoMarkdown
|
|
272
|
+
&& typeof window.ManyoyoMarkdown.render === 'function';
|
|
273
|
+
|
|
274
|
+
if (showMarkdownRendered) {
|
|
275
|
+
destroyEditor();
|
|
276
|
+
const markdownNode = document.createElement('div');
|
|
277
|
+
markdownNode.className = 'md-content files-markdown-preview';
|
|
278
|
+
markdownNode.innerHTML = window.ManyoyoMarkdown.render(String(payload.content || ''), {
|
|
279
|
+
imageUrlResolver: function (relativeHref) {
|
|
280
|
+
return resolveMarkdownImageUrl(payload.path, relativeHref);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
previewBodyNode.innerHTML = '';
|
|
284
|
+
previewBodyNode.appendChild(markdownNode);
|
|
285
|
+
syncMarkdownToggleButton();
|
|
286
|
+
syncSaveButton();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
235
290
|
if (window.ManyoyoCodeEditor && typeof window.ManyoyoCodeEditor.create === 'function') {
|
|
236
291
|
if (!state.editor || !state.editorHost || !previewBodyNode.contains(state.editorHost)) {
|
|
237
292
|
destroyEditor();
|
|
@@ -252,18 +307,31 @@
|
|
|
252
307
|
state.editor.setLanguage(language);
|
|
253
308
|
state.editor.setReadOnly(state.previewReadOnly);
|
|
254
309
|
}
|
|
310
|
+
syncMarkdownToggleButton();
|
|
255
311
|
syncSaveButton();
|
|
256
312
|
return;
|
|
257
313
|
}
|
|
258
314
|
|
|
259
315
|
destroyEditor();
|
|
260
316
|
previewBodyNode.innerHTML = `<pre class="files-pre">${escapeHtml(String(payload.content || ''))}</pre>`;
|
|
317
|
+
syncMarkdownToggleButton();
|
|
318
|
+
syncSaveButton();
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (payload.kind === 'image') {
|
|
323
|
+
destroyEditor();
|
|
324
|
+
const rawUrl = '/api/sessions/' + encodeURIComponent(state.sessionName) + '/fs/raw?path='
|
|
325
|
+
+ encodeURIComponent(payload.path);
|
|
326
|
+
previewBodyNode.innerHTML = `<div class="files-image-preview"><img src="${escapeHtml(rawUrl)}" alt="${escapeHtml(payload.path || '')}" /></div>`;
|
|
327
|
+
syncMarkdownToggleButton();
|
|
261
328
|
syncSaveButton();
|
|
262
329
|
return;
|
|
263
330
|
}
|
|
264
331
|
|
|
265
332
|
destroyEditor();
|
|
266
333
|
previewBodyNode.innerHTML = `<div class="files-note">当前文件暂不支持在线预览。文件类型:${escapeHtml(payload.kind || 'unknown')}</div>`;
|
|
334
|
+
syncMarkdownToggleButton();
|
|
267
335
|
syncSaveButton();
|
|
268
336
|
}
|
|
269
337
|
|
|
@@ -397,6 +465,7 @@
|
|
|
397
465
|
state.loadingFile = true;
|
|
398
466
|
state.selectedPath = pathText;
|
|
399
467
|
state.selectedEntry = selectedEntry;
|
|
468
|
+
state.markdownViewMode = 'rendered';
|
|
400
469
|
renderList();
|
|
401
470
|
renderPreviewEmpty(pathText, '正在读取文件内容...');
|
|
402
471
|
try {
|
|
@@ -584,6 +653,13 @@
|
|
|
584
653
|
});
|
|
585
654
|
}
|
|
586
655
|
|
|
656
|
+
if (toggleMarkdownViewBtn) {
|
|
657
|
+
toggleMarkdownViewBtn.addEventListener('click', function () {
|
|
658
|
+
state.markdownViewMode = state.markdownViewMode === 'source' ? 'rendered' : 'source';
|
|
659
|
+
renderPreviewPayload(state.selectedFile);
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
|
|
587
663
|
renderList();
|
|
588
664
|
|
|
589
665
|
return {
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
configured: false,
|
|
6
6
|
available: false,
|
|
7
7
|
linkGuardBound: false,
|
|
8
|
-
linkOpenHandler: null
|
|
8
|
+
linkOpenHandler: null,
|
|
9
|
+
currentImageResolver: null
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
function escapeHtml(value) {
|
|
@@ -247,8 +248,14 @@
|
|
|
247
248
|
'[\uD83D\uDDBC\uFE0F点击查看图片:' + (safeText || escapeHtml(safeHref)) + ']'
|
|
248
249
|
);
|
|
249
250
|
}
|
|
250
|
-
//
|
|
251
|
-
|
|
251
|
+
// 相对路径:交给调用方按当前渲染上下文解析为可访问的 URL(未提供 resolver 时原样使用)
|
|
252
|
+
const resolvedHref = typeof runtime.currentImageResolver === 'function'
|
|
253
|
+
? runtime.currentImageResolver(safeHref)
|
|
254
|
+
: safeHref;
|
|
255
|
+
if (!resolvedHref) {
|
|
256
|
+
return safeText || escapeHtml(token ? token.text : text || '');
|
|
257
|
+
}
|
|
258
|
+
let output = '<img src="' + escapeHtml(resolvedHref) + '" alt="' + escapeHtml(text || '') + '"';
|
|
252
259
|
if (rawTitle) {
|
|
253
260
|
output += ' title="' + escapeHtml(rawTitle) + '"';
|
|
254
261
|
}
|
|
@@ -272,7 +279,7 @@
|
|
|
272
279
|
return Boolean(msg && msg.mode === 'agent' && msg.role === 'assistant');
|
|
273
280
|
}
|
|
274
281
|
|
|
275
|
-
function render(content) {
|
|
282
|
+
function render(content, options) {
|
|
276
283
|
const source = String(content == null ? '' : content);
|
|
277
284
|
if (!source) {
|
|
278
285
|
return '';
|
|
@@ -284,10 +291,15 @@
|
|
|
284
291
|
if (!markedApi) {
|
|
285
292
|
return '';
|
|
286
293
|
}
|
|
294
|
+
runtime.currentImageResolver = options && typeof options.imageUrlResolver === 'function'
|
|
295
|
+
? options.imageUrlResolver
|
|
296
|
+
: null;
|
|
287
297
|
try {
|
|
288
298
|
return String(markedApi.parse(source) || '');
|
|
289
299
|
} catch (e) {
|
|
290
300
|
return '';
|
|
301
|
+
} finally {
|
|
302
|
+
runtime.currentImageResolver = null;
|
|
291
303
|
}
|
|
292
304
|
}
|
|
293
305
|
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
.
|
|
1
|
+
.md-content {
|
|
2
2
|
color: var(--text);
|
|
3
3
|
font-size: 13px;
|
|
4
4
|
line-height: 1.6;
|
|
5
5
|
word-break: break-word;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
.
|
|
8
|
+
.md-content > :first-child {
|
|
9
9
|
margin-top: 0;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
.
|
|
12
|
+
.md-content > :last-child {
|
|
13
13
|
margin-bottom: 0;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
.
|
|
17
|
-
.
|
|
18
|
-
.
|
|
19
|
-
.
|
|
20
|
-
.
|
|
21
|
-
.
|
|
16
|
+
.md-content p,
|
|
17
|
+
.md-content ul,
|
|
18
|
+
.md-content ol,
|
|
19
|
+
.md-content blockquote,
|
|
20
|
+
.md-content pre,
|
|
21
|
+
.md-content table {
|
|
22
22
|
margin: 0.6em 0;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
.
|
|
26
|
-
.
|
|
25
|
+
.md-content ul,
|
|
26
|
+
.md-content ol {
|
|
27
27
|
padding-left: 1.4em;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
.
|
|
30
|
+
.md-content code {
|
|
31
31
|
font-family: var(--font-mono);
|
|
32
32
|
font-size: 12px;
|
|
33
33
|
padding: 1px 5px;
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
background: rgba(194, 149, 79, 0.16);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
.
|
|
38
|
+
.md-content pre {
|
|
39
39
|
border-radius: 8px;
|
|
40
40
|
padding: 9px 10px;
|
|
41
41
|
border: 1px solid #e6d2b7;
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
overflow-x: auto;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
.
|
|
46
|
+
.md-content pre code {
|
|
47
47
|
background: transparent;
|
|
48
48
|
border-radius: 0;
|
|
49
49
|
padding: 0;
|
|
@@ -51,26 +51,26 @@
|
|
|
51
51
|
line-height: 1.5;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
.
|
|
54
|
+
.md-content blockquote {
|
|
55
55
|
margin-left: 0;
|
|
56
56
|
padding-left: 10px;
|
|
57
57
|
border-left: 3px solid #dcb788;
|
|
58
58
|
color: #66492d;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
.
|
|
61
|
+
.md-content table {
|
|
62
62
|
border-collapse: collapse;
|
|
63
63
|
width: 100%;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
.
|
|
67
|
-
.
|
|
66
|
+
.md-content th,
|
|
67
|
+
.md-content td {
|
|
68
68
|
border: 1px solid #e6d2b7;
|
|
69
69
|
padding: 4px 6px;
|
|
70
70
|
text-align: left;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
.
|
|
73
|
+
.md-content a {
|
|
74
74
|
color: #8a4f17;
|
|
75
75
|
text-decoration-line: underline;
|
|
76
76
|
text-decoration-thickness: 2px;
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
text-decoration-color: rgba(138, 79, 23, 0.8);
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
.
|
|
82
|
-
.
|
|
81
|
+
.md-content a:hover,
|
|
82
|
+
.md-content a:focus-visible {
|
|
83
83
|
text-decoration-color: #8a4f17;
|
|
84
84
|
}
|
package/lib/web/server.js
CHANGED
|
@@ -45,6 +45,16 @@ const WEB_AGENT_CONTEXT_PER_MESSAGE_MAX_CHARS = 600;
|
|
|
45
45
|
const NATIVE_SESSION_RESUME_PROGRAMS = new Set(['claude', 'codex', 'opencode']);
|
|
46
46
|
const WEB_FILE_PREVIEW_MAX_BYTES = 512 * 1024;
|
|
47
47
|
const WEB_FILE_EDIT_MAX_BYTES = 2 * 1024 * 1024;
|
|
48
|
+
const WEB_FILE_RAW_MAX_CONCURRENT_PER_CONTAINER = 4;
|
|
49
|
+
const IMAGE_EXTENSION_CONTENT_TYPES = {
|
|
50
|
+
'.png': 'image/png',
|
|
51
|
+
'.jpg': 'image/jpeg',
|
|
52
|
+
'.jpeg': 'image/jpeg',
|
|
53
|
+
'.gif': 'image/gif',
|
|
54
|
+
'.webp': 'image/webp',
|
|
55
|
+
'.bmp': 'image/bmp',
|
|
56
|
+
'.ico': 'image/x-icon'
|
|
57
|
+
};
|
|
48
58
|
const WEB_AUTH_COOKIE_NAME = 'manyoyo_web_auth';
|
|
49
59
|
const WEB_AUTH_TTL_SECONDS = 12 * 60 * 60;
|
|
50
60
|
const WEB_SESSION_KEY_SEPARATOR = '~';
|
|
@@ -2930,6 +2940,34 @@ ${scriptSource}
|
|
|
2930
2940
|
__MANYOYO_NODE__`;
|
|
2931
2941
|
}
|
|
2932
2942
|
|
|
2943
|
+
function spawnContainerRawFileStream(ctx, containerName, filePath) {
|
|
2944
|
+
return spawn(
|
|
2945
|
+
ctx.dockerCmd,
|
|
2946
|
+
['exec', containerName, 'cat', '--', filePath],
|
|
2947
|
+
{ stdio: ['ignore', 'pipe', 'pipe'] }
|
|
2948
|
+
);
|
|
2949
|
+
}
|
|
2950
|
+
|
|
2951
|
+
const webRawFileStreamCounts = new Map();
|
|
2952
|
+
|
|
2953
|
+
function acquireWebRawFileStreamSlot(containerName) {
|
|
2954
|
+
const current = webRawFileStreamCounts.get(containerName) || 0;
|
|
2955
|
+
if (current >= WEB_FILE_RAW_MAX_CONCURRENT_PER_CONTAINER) {
|
|
2956
|
+
return false;
|
|
2957
|
+
}
|
|
2958
|
+
webRawFileStreamCounts.set(containerName, current + 1);
|
|
2959
|
+
return true;
|
|
2960
|
+
}
|
|
2961
|
+
|
|
2962
|
+
function releaseWebRawFileStreamSlot(containerName) {
|
|
2963
|
+
const current = webRawFileStreamCounts.get(containerName) || 0;
|
|
2964
|
+
if (current <= 1) {
|
|
2965
|
+
webRawFileStreamCounts.delete(containerName);
|
|
2966
|
+
return;
|
|
2967
|
+
}
|
|
2968
|
+
webRawFileStreamCounts.set(containerName, current - 1);
|
|
2969
|
+
}
|
|
2970
|
+
|
|
2933
2971
|
function inferFileLanguage(filePath) {
|
|
2934
2972
|
const ext = path.extname(String(filePath || '')).toLowerCase();
|
|
2935
2973
|
return FILE_LANGUAGE_MAP[ext] || 'text';
|
|
@@ -3018,9 +3056,11 @@ function buildContainerFileReadCommand(requestedPath, options = {}) {
|
|
|
3018
3056
|
return buildWebContainerNodeCommand(`
|
|
3019
3057
|
// __MANYOYO_FS_READ__
|
|
3020
3058
|
const fs = require('fs');
|
|
3059
|
+
const path = require('path');
|
|
3021
3060
|
|
|
3022
3061
|
const requestedPath = ${JSON.stringify(String(requestedPath || ''))};
|
|
3023
3062
|
const maxBytes = ${String(maxBytes)};
|
|
3063
|
+
const imageExtensions = new Set(${JSON.stringify(Object.keys(IMAGE_EXTENSION_CONTENT_TYPES))});
|
|
3024
3064
|
|
|
3025
3065
|
function looksBinary(buffer) {
|
|
3026
3066
|
const length = Math.min(buffer.length, 4096);
|
|
@@ -3044,32 +3084,66 @@ try {
|
|
|
3044
3084
|
throw new Error('目标不是文件: ' + realPath);
|
|
3045
3085
|
}
|
|
3046
3086
|
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
const buffer = Buffer.alloc(readBytes);
|
|
3050
|
-
const fd = fs.openSync(realPath, 'r');
|
|
3051
|
-
try {
|
|
3052
|
-
fs.readSync(fd, buffer, 0, readBytes, 0);
|
|
3053
|
-
} finally {
|
|
3054
|
-
fs.closeSync(fd);
|
|
3055
|
-
}
|
|
3056
|
-
|
|
3057
|
-
if (looksBinary(buffer)) {
|
|
3087
|
+
// 图片走 fs/raw 流式传输,这里只确认元信息,不把整份二进制读进内存/JSON
|
|
3088
|
+
if (imageExtensions.has(path.extname(realPath).toLowerCase())) {
|
|
3058
3089
|
process.stdout.write(JSON.stringify({
|
|
3059
3090
|
path: realPath,
|
|
3060
|
-
kind: '
|
|
3061
|
-
size
|
|
3062
|
-
truncated: maxBytes > 0 && size > maxBytes
|
|
3091
|
+
kind: 'image',
|
|
3092
|
+
size: stat.size
|
|
3063
3093
|
}));
|
|
3064
3094
|
} else {
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
}
|
|
3095
|
+
const size = stat.size;
|
|
3096
|
+
const readBytes = maxBytes > 0 ? Math.min(size, maxBytes) : size;
|
|
3097
|
+
const buffer = Buffer.alloc(readBytes);
|
|
3098
|
+
const fd = fs.openSync(realPath, 'r');
|
|
3099
|
+
try {
|
|
3100
|
+
fs.readSync(fd, buffer, 0, readBytes, 0);
|
|
3101
|
+
} finally {
|
|
3102
|
+
fs.closeSync(fd);
|
|
3103
|
+
}
|
|
3104
|
+
|
|
3105
|
+
if (looksBinary(buffer)) {
|
|
3106
|
+
process.stdout.write(JSON.stringify({
|
|
3107
|
+
path: realPath,
|
|
3108
|
+
kind: 'binary',
|
|
3109
|
+
size,
|
|
3110
|
+
truncated: maxBytes > 0 && size > maxBytes
|
|
3111
|
+
}));
|
|
3112
|
+
} else {
|
|
3113
|
+
process.stdout.write(JSON.stringify({
|
|
3114
|
+
path: realPath,
|
|
3115
|
+
kind: 'text',
|
|
3116
|
+
size,
|
|
3117
|
+
truncated: maxBytes > 0 && size > maxBytes,
|
|
3118
|
+
content: buffer.toString('utf8')
|
|
3119
|
+
}));
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
3122
|
+
} catch (e) {
|
|
3123
|
+
process.stdout.write(JSON.stringify({
|
|
3124
|
+
error: e && e.message ? e.message : '读取文件失败'
|
|
3125
|
+
}));
|
|
3126
|
+
}
|
|
3127
|
+
`);
|
|
3128
|
+
}
|
|
3129
|
+
|
|
3130
|
+
function buildContainerFileStatCommand(requestedPath) {
|
|
3131
|
+
return buildWebContainerNodeCommand(`
|
|
3132
|
+
// __MANYOYO_FS_STAT__
|
|
3133
|
+
const fs = require('fs');
|
|
3134
|
+
|
|
3135
|
+
const requestedPath = ${JSON.stringify(String(requestedPath || ''))};
|
|
3136
|
+
|
|
3137
|
+
try {
|
|
3138
|
+
const realPath = fs.realpathSync(requestedPath);
|
|
3139
|
+
const stat = fs.statSync(realPath);
|
|
3140
|
+
if (!stat.isFile()) {
|
|
3141
|
+
throw new Error('目标不是文件: ' + realPath);
|
|
3072
3142
|
}
|
|
3143
|
+
process.stdout.write(JSON.stringify({
|
|
3144
|
+
path: realPath,
|
|
3145
|
+
size: stat.size
|
|
3146
|
+
}));
|
|
3073
3147
|
} catch (e) {
|
|
3074
3148
|
process.stdout.write(JSON.stringify({
|
|
3075
3149
|
error: e && e.message ? e.message : '读取文件失败'
|
|
@@ -4316,6 +4390,73 @@ async function handleWebApi(req, res, pathname, ctx, state) {
|
|
|
4316
4390
|
sendJson(res, 200, payload);
|
|
4317
4391
|
}
|
|
4318
4392
|
},
|
|
4393
|
+
{
|
|
4394
|
+
method: 'GET',
|
|
4395
|
+
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/fs\/raw$/),
|
|
4396
|
+
handler: async match => {
|
|
4397
|
+
const sessionRef = getValidSessionRef(ctx, res, match[1]);
|
|
4398
|
+
if (!sessionRef) {
|
|
4399
|
+
return;
|
|
4400
|
+
}
|
|
4401
|
+
const requestUrl = new URL(req.url || '/api/sessions/x/fs/raw', 'http://localhost');
|
|
4402
|
+
const targetPath = String(requestUrl.searchParams.get('path') || '').trim();
|
|
4403
|
+
if (!targetPath) {
|
|
4404
|
+
sendJson(res, 400, { error: 'path 不能为空' });
|
|
4405
|
+
return;
|
|
4406
|
+
}
|
|
4407
|
+
const ext = path.extname(targetPath).toLowerCase();
|
|
4408
|
+
const contentType = IMAGE_EXTENSION_CONTENT_TYPES[ext];
|
|
4409
|
+
if (!contentType) {
|
|
4410
|
+
sendJson(res, 400, { error: '不支持预览该文件类型' });
|
|
4411
|
+
return;
|
|
4412
|
+
}
|
|
4413
|
+
|
|
4414
|
+
await ensureWebContainer(ctx, state, sessionRef.containerName, sessionRef);
|
|
4415
|
+
const statPayload = await execJsonCommandInWebContainer(
|
|
4416
|
+
ctx,
|
|
4417
|
+
sessionRef.containerName,
|
|
4418
|
+
buildContainerFileStatCommand(targetPath)
|
|
4419
|
+
);
|
|
4420
|
+
if (statPayload && statPayload.error) {
|
|
4421
|
+
sendJson(res, 404, { error: statPayload.error });
|
|
4422
|
+
return;
|
|
4423
|
+
}
|
|
4424
|
+
|
|
4425
|
+
if (!acquireWebRawFileStreamSlot(sessionRef.containerName)) {
|
|
4426
|
+
sendJson(res, 429, { error: '预览并发已达上限,请稍后重试' });
|
|
4427
|
+
return;
|
|
4428
|
+
}
|
|
4429
|
+
|
|
4430
|
+
let released = false;
|
|
4431
|
+
const releaseOnce = () => {
|
|
4432
|
+
if (released) {
|
|
4433
|
+
return;
|
|
4434
|
+
}
|
|
4435
|
+
released = true;
|
|
4436
|
+
releaseWebRawFileStreamSlot(sessionRef.containerName);
|
|
4437
|
+
};
|
|
4438
|
+
|
|
4439
|
+
const child = spawnContainerRawFileStream(ctx, sessionRef.containerName, statPayload.path);
|
|
4440
|
+
// stderr 单独消费,绝不混入响应体,避免管道缓冲区打满导致 cat 进程卡死
|
|
4441
|
+
child.stderr.on('data', () => {});
|
|
4442
|
+
child.on('error', () => releaseOnce());
|
|
4443
|
+
child.on('close', () => releaseOnce());
|
|
4444
|
+
res.on('close', () => {
|
|
4445
|
+
releaseOnce();
|
|
4446
|
+
if (!child.killed) {
|
|
4447
|
+
child.kill();
|
|
4448
|
+
}
|
|
4449
|
+
});
|
|
4450
|
+
|
|
4451
|
+
res.writeHead(200, {
|
|
4452
|
+
'Content-Type': contentType,
|
|
4453
|
+
'Content-Length': String(statPayload.size),
|
|
4454
|
+
'X-Content-Type-Options': 'nosniff',
|
|
4455
|
+
'Cache-Control': 'no-store'
|
|
4456
|
+
});
|
|
4457
|
+
child.stdout.pipe(res);
|
|
4458
|
+
}
|
|
4459
|
+
},
|
|
4319
4460
|
{
|
|
4320
4461
|
method: 'PUT',
|
|
4321
4462
|
match: currentPath => currentPath.match(/^\/api\/sessions\/([^/]+)\/fs\/write$/),
|