Skip to content

Commit dcaae39

Browse files
authored
fix: video preview style and static server (#4324)
* fix: video preview style and static server * fix: video preview style and static server
1 parent a3003c9 commit dcaae39

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

packages/express-file-server/src/node/express-file-server.contribution.ts

+37-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,47 @@ export class ExpressFileServerContribution implements ServerAppContribution {
4444
// 在允许的 contentType
4545
contentType
4646
) {
47-
ctx.set('Content-Type', contentType);
47+
const range = ctx.headers.range;
48+
49+
if (!fs.existsSync(filePath)) {
50+
ctx.status = 404;
51+
ctx.body = '文件未找到';
52+
return;
53+
}
54+
4855
if (this.appConfig.staticAllowOrigin) {
4956
ctx.set('Access-Control-Allow-Origin', this.appConfig.staticAllowOrigin);
5057
}
5158

52-
ctx.body = fs.createReadStream(filePath);
59+
const stats = await fs.promises.stat(filePath);
60+
const total = stats.size;
61+
62+
if (!range) {
63+
ctx.status = 200;
64+
ctx.set('Content-Type', contentType);
65+
ctx.set('Content-Length', String(total));
66+
ctx.body = fs.createReadStream(filePath);
67+
return;
68+
}
69+
70+
const parts = range.replace(/bytes=/, '').split('-');
71+
const start = parseInt(parts[0], 10);
72+
const end = parts[1] ? parseInt(parts[1], 10) : total - 1;
73+
74+
if (start >= total || end >= total || start > end) {
75+
ctx.status = 416; // Range Not Satisfiable
76+
ctx.set('Content-Range', `bytes */${total}`);
77+
return;
78+
}
79+
80+
ctx.status = 206;
81+
ctx.set('Content-Range', `bytes ${start}-${end}/${total}`);
82+
ctx.set('Accept-Ranges', 'bytes');
83+
ctx.set('Content-Length', String(end - start + 1));
84+
ctx.set('Content-Type', contentType);
85+
86+
const stream = fs.createReadStream(filePath, { start, end });
87+
ctx.body = stream;
5388
} else {
5489
ctx.status = 403;
5590
}

packages/file-scheme/src/browser/preview.view.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { memo } from 'react';
22

33
import { Disposable, useInjectable } from '@opensumi/ide-core-browser';
44
import { StaticResourceService } from '@opensumi/ide-core-browser/lib/static-resource';
@@ -16,15 +16,14 @@ const useResource = (resource: IResource) => {
1616
};
1717
};
1818

19-
export const VideoPreview: ReactEditorComponent<null> = (props) => {
19+
export const VideoPreview: ReactEditorComponent<null> = memo((props) => {
2020
const { src } = useResource(props.resource);
21-
2221
return (
2322
<div className={styles.kt_video_preview}>
24-
<video autoPlay controls className={styles.kt_video} src={src}></video>
23+
<video playsInline controls className={styles.kt_video} src={src} />
2524
</div>
2625
);
27-
};
26+
});
2827

2928
export const ImagePreview: ReactEditorComponent<null> = (props) => {
3029
const imgRef = React.useRef<HTMLImageElement>();

packages/file-scheme/src/browser/style.module.less

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@
4343

4444
.kt_video_preview {
4545
height: 100%;
46+
display: flex;
47+
justify-content: center;
48+
align-items: center;
49+
padding: 40px;
4650
}
4751

4852
.kt_video {
49-
width: 100%;
50-
height: 100%;
53+
max-width: 100%;
54+
max-height: 100%;
5155
}
5256

5357
.error-page {

0 commit comments

Comments
 (0)