Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import './App.css'
import {Code, Grid} from "@mantine/core";
import {CardImage} from "./CardImage.tsx";
import {VideoAssets} from "./video-assets.tsx";

function App() {
return (
<>
<Grid>
<VideoAssets/>
<CardImage col={{span:12}} titleCard={"Background image css"} images={[]}>
<div style={{height:100,width:100,position:'relative'}}>

Expand Down
20 changes: 20 additions & 0 deletions examples/react/src/video-assets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {useRef} from "react";

export const VideoAssets = () => {
const videoRef = useRef(null);
return <>
<video src="/base/flower.mp4" controls ref={videoRef}/>
<div className="card">
<button
onClick={() => {
const video = videoRef.current;
if (video && video.duration > 0) {
video.currentTime = video.duration / 2;
}
}}
>
video.currentTime = video.duration / 2
</button>
</div>
</>
}
Binary file added examples/react/videos/flower.mp4
Binary file not shown.
3 changes: 2 additions & 1 deletion examples/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineConfig({
},
build: {
rollupOptions: {
external:['.png'],
external: ['.png'],
output: {
chunkFileNames: 'assets/js/[name]-[hash].js',
entryFileNames: 'assets/js/[name]-[hash].js',
Expand Down Expand Up @@ -42,6 +42,7 @@ export default defineConfig({
input: "public/**",
output: "/images/assets-a"
},
"videos/**",
{
input: "../../shared-assets/**",
output: "shared/images",
Expand Down
73 changes: 70 additions & 3 deletions packages/libs/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ function getContentType(file: string) {
return mime.lookup(file);
}

/**
* Parse Range header from request
* Returns { start, end } or null if no valid range
*/
function parseRange(
rangeHeader: string | undefined,
fileSize: number
): {start: number; end: number} | null {
if (!rangeHeader) return null;

const match = rangeHeader.match(/bytes=(\d*)-(\d*)/);
if (!match) return null;

const start = match[1] ? Number.parseInt(match[1], 10) : 0;
const end = match[2] ? Number.parseInt(match[2], 10) : fileSize - 1;

// Validate range
if (start >= fileSize || end >= fileSize || start > end) {
return null;
}

return {start, end};
}

function handleWriteToServe(
res: http.ServerResponse,
req: IncomingMessage,
Expand All @@ -57,14 +81,57 @@ function handleWriteToServe(
cacheOption: ICacheConfig = {}
) {
try {
// Get file stats for size
const stats = fs.statSync(path);
const fileSize = stats.size;

// Parse Range header
const rangeHeader = req.headers.range;
const range = parseRange(rangeHeader, fileSize);

// Set cache options
for (const key in cacheOption) {
res.setHeader(key, cacheOption[key]);
}
// res.setHeader("Cache-Control", "max-age=31536000, immutable");

// Always set Accept-Ranges header to indicate range support
res.setHeader("Accept-Ranges", "bytes");
res.setHeader("Content-Type", contentType);
res.write(fs.readFileSync(path));
res.end();

if (range) {
// Handle range request (for video seeking)
const {start, end} = range;
const contentLength = end - start + 1;

// Set 206 Partial Content status
res.statusCode = 206;
res.setHeader("Content-Range", `bytes ${start}-${end}/${fileSize}`);
res.setHeader("Content-Length", contentLength);

// Read and send only the requested range
const fileStream = fs.createReadStream(path, {start, end});
fileStream.pipe(res);

fileStream.on("error", (err) => {
console.error("Error streaming file:", err);
res.end();
});
} else {
// Handle normal request (no range)
res.statusCode = 200;
res.setHeader("Content-Length", fileSize);
// Stream the entire file
const fileStream = fs.createReadStream(path);
fileStream.pipe(res);

fileStream.on("error", (err) => {
console.error("Error streaming file:", err);
res.end();
});
}
} catch (e) {
console.error("Error in handleWriteToServe:", e);
res.statusCode = 500;
res.write("");
res.end();
}
Expand Down
Loading