refactor: update UploadComponent max file size and supported media types, enhance apiMiddleware for video streaming
This commit is contained in:
parent
941055102d
commit
05cc912335
3 changed files with 46 additions and 12 deletions
|
|
@ -78,7 +78,8 @@
|
||||||
"a11y": {
|
"a11y": {
|
||||||
"noAutofocus": "off",
|
"noAutofocus": "off",
|
||||||
"noNoninteractiveElementInteractions": "error",
|
"noNoninteractiveElementInteractions": "error",
|
||||||
"useSemanticElements": "off"
|
"useSemanticElements": "off",
|
||||||
|
"useMediaCaption": "off"
|
||||||
},
|
},
|
||||||
"complexity": {
|
"complexity": {
|
||||||
"noUselessFragments": "off"
|
"noUselessFragments": "off"
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ export const UploadComponent = ({
|
||||||
cb_onUpload,
|
cb_onUpload,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
userId,
|
userId,
|
||||||
maxMB = 5, // Default max size is 5MB
|
maxMB = 50, // Default max size is 50MB
|
||||||
maxFiles,
|
maxFiles,
|
||||||
}: UploadComponentProps) => {
|
}: UploadComponentProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
@ -107,6 +107,13 @@ export const UploadComponent = ({
|
||||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // xlsx
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // xlsx
|
||||||
"application/vnd.oasis.opendocument.text", // odt
|
"application/vnd.oasis.opendocument.text", // odt
|
||||||
"application/vnd.oasis.opendocument.spreadsheet", // ods
|
"application/vnd.oasis.opendocument.spreadsheet", // ods
|
||||||
|
"video/mp4", // mp4
|
||||||
|
"video/webm", // webm
|
||||||
|
"audio/mpeg", // mp3
|
||||||
|
"audio/wav", // wav
|
||||||
|
"video/x-msvideo", // avi
|
||||||
|
"image/gif", // gif
|
||||||
|
"image/svg+xml", // svg
|
||||||
];
|
];
|
||||||
|
|
||||||
const utils = api.useUtils();
|
const utils = api.useUtils();
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import { verifyToken } from "~/server/auth/jwt";
|
||||||
|
|
||||||
export const apisMiddleware = async (req: NextRequest) => {
|
export const apisMiddleware = async (req: NextRequest) => {
|
||||||
const { pathname, searchParams } = req.nextUrl;
|
const { pathname, searchParams } = req.nextUrl;
|
||||||
//console.log("APIs Middleware triggered for:", pathname);
|
|
||||||
|
|
||||||
// Only handle storage API routes
|
// Only handle storage API routes
|
||||||
if (!pathname.startsWith("/storage-api/")) {
|
if (!pathname.startsWith("/storage-api/")) {
|
||||||
|
|
@ -41,19 +40,18 @@ export const apisMiddleware = async (req: NextRequest) => {
|
||||||
|
|
||||||
// Build the storage API URL correctly
|
// Build the storage API URL correctly
|
||||||
const slug = pathname.replace("/storage-api/", "");
|
const slug = pathname.replace("/storage-api/", "");
|
||||||
|
|
||||||
params.set("token", env.STORAGE_TOKEN);
|
params.set("token", env.STORAGE_TOKEN);
|
||||||
|
|
||||||
const storageUrl = `${env.STORAGE_URL}/${slug}?${params.toString()}`;
|
const storageUrl = `${env.STORAGE_URL}/${slug}?${params.toString()}`;
|
||||||
|
|
||||||
//console.log("Proxying to:", storageUrl);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Forward the request to storage API
|
// Forward the request to storage API
|
||||||
const headers = new Headers(req.headers);
|
const headers = new Headers(req.headers);
|
||||||
headers.delete("host");
|
headers.delete("host");
|
||||||
headers.delete("cookie");
|
headers.delete("cookie");
|
||||||
|
|
||||||
|
// **CRITICAL: Preserve Range header for video streaming**
|
||||||
|
const rangeHeader = req.headers.get("range");
|
||||||
|
|
||||||
const response = await fetch(storageUrl, {
|
const response = await fetch(storageUrl, {
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
|
|
@ -110,9 +108,13 @@ export const apisMiddleware = async (req: NextRequest) => {
|
||||||
return new NextResponse("Empty response", { status: 500 });
|
return new NextResponse("Empty response", { status: 500 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// **IMPORTANT: Use correct status code for partial content**
|
||||||
|
const status =
|
||||||
|
rangeHeader && response.status === 206 ? 206 : response.status;
|
||||||
|
|
||||||
// Create response with proper headers
|
// Create response with proper headers
|
||||||
const proxyResponse = new NextResponse(data, {
|
const proxyResponse = new NextResponse(data, {
|
||||||
status: response.status,
|
status: status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -120,6 +122,8 @@ export const apisMiddleware = async (req: NextRequest) => {
|
||||||
const contentType = response.headers.get("Content-Type");
|
const contentType = response.headers.get("Content-Type");
|
||||||
const contentDisposition = response.headers.get("Content-Disposition");
|
const contentDisposition = response.headers.get("Content-Disposition");
|
||||||
const contentLength = response.headers.get("Content-Length");
|
const contentLength = response.headers.get("Content-Length");
|
||||||
|
const acceptRanges = response.headers.get("Accept-Ranges");
|
||||||
|
const contentRange = response.headers.get("Content-Range");
|
||||||
|
|
||||||
if (contentType) {
|
if (contentType) {
|
||||||
proxyResponse.headers.set("Content-Type", contentType);
|
proxyResponse.headers.set("Content-Type", contentType);
|
||||||
|
|
@ -128,16 +132,38 @@ export const apisMiddleware = async (req: NextRequest) => {
|
||||||
if (contentDisposition) {
|
if (contentDisposition) {
|
||||||
proxyResponse.headers.set("Content-Disposition", contentDisposition);
|
proxyResponse.headers.set("Content-Disposition", contentDisposition);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentLength) {
|
if (contentLength) {
|
||||||
proxyResponse.headers.set("Content-Length", contentLength);
|
proxyResponse.headers.set("Content-Length", contentLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// **CRITICAL: Copy Range-related headers for video streaming**
|
||||||
|
if (acceptRanges) {
|
||||||
|
proxyResponse.headers.set("Accept-Ranges", acceptRanges);
|
||||||
|
} else if (isVideoRequest) {
|
||||||
|
proxyResponse.headers.set("Accept-Ranges", "bytes");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentRange) {
|
||||||
|
proxyResponse.headers.set("Content-Range", contentRange);
|
||||||
|
}
|
||||||
|
|
||||||
// Set CORS and caching headers
|
// Set CORS and caching headers
|
||||||
proxyResponse.headers.set("Access-Control-Allow-Origin", "*");
|
proxyResponse.headers.set("Access-Control-Allow-Origin", "*");
|
||||||
|
|
||||||
|
// Different cache strategy for videos
|
||||||
|
if (isVideoRequest) {
|
||||||
proxyResponse.headers.set(
|
proxyResponse.headers.set(
|
||||||
"Cache-Control",
|
"Cache-Control",
|
||||||
"public, max-age=31536000, immutable",
|
"public, max-age=31536000, immutable",
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
proxyResponse.headers.set(
|
||||||
|
"Cache-Control",
|
||||||
|
"public, max-age=31536000, immutable",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
proxyResponse.headers.delete("X-Frame-Options");
|
proxyResponse.headers.delete("X-Frame-Options");
|
||||||
proxyResponse.headers.set(
|
proxyResponse.headers.set(
|
||||||
"Content-Security-Policy",
|
"Content-Security-Policy",
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue