fix: override MIME type for PDF uploads & fix build errors

- Fix PDF upload failing on Android due to wrong MIME type (image/pdf → application/pdf)
- Add safe JSON parsing for external storage responses with incorrect Content-Type headers
- Add detailed upload logging for debugging (file name, type, size, response status)
- Fix TypeScript error: missing prisma import in forum preview-report-posting route
- Fix ESLint warning: unescaped apostrophe in support-center page
- Add DEBUG_UPLOAD_FILE.md documentation for troubleshooting guide

Fixes upload issue where Android devices couldn't upload PDF files due to MIME type mismatch

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
2026-04-14 15:17:45 +08:00
parent a71997b4ef
commit 38239c52d6
4 changed files with 297 additions and 15 deletions

View File

@@ -2,14 +2,50 @@ import { funGetDirectoryNameByValue } from "@/app_modules/_global/fun/get";
import { NextResponse } from "next/server";
export async function POST(request: Request) {
const formData = await request.formData();
const dirId = formData.get("dirId");
const keyOfDirectory = await funGetDirectoryNameByValue({
value: dirId as string,
});
try {
const formData = await request.formData();
const dirId = formData.get("dirId");
const file = formData.get("file") as File | null;
// === LOGGING: Request Details ===
console.log("=== UPLOAD REQUEST START ===");
console.log("dirId:", dirId);
console.log("File details:");
console.log(" - Name:", file?.name);
console.log(" - Type:", file?.type);
console.log(" - Size:", file?.size, "bytes");
console.log(" - Size (KB):", file ? (file.size / 1024).toFixed(2) : "N/A");
console.log("===========================");
// FIX: Override MIME type jika salah (mobile app kadang kirim image/pdf)
let fixedFile = file;
if (file) {
const fileName = file.name.toLowerCase();
const originalType = file.type.toLowerCase();
// Jika file PDF tapi type bukan application/pdf, fix it
if (fileName.endsWith(".pdf") && originalType !== "application/pdf") {
console.log("⚠️ WARNING: PDF file has wrong MIME type:", originalType);
console.log("🔧 Overriding to: application/pdf");
// Create new File with correct MIME type
const buffer = await file.arrayBuffer();
fixedFile = new File([buffer], file.name, {
type: "application/pdf",
lastModified: file.lastModified,
});
// Rebuild formData with fixed file
formData.set("file", fixedFile);
}
}
const keyOfDirectory = await funGetDirectoryNameByValue({
value: dirId as string,
});
console.log("Directory key:", keyOfDirectory);
const res = await fetch("https://wibu-storage.wibudev.com/api/upload", {
method: "POST",
body: formData,
@@ -18,9 +54,70 @@ export async function POST(request: Request) {
},
});
const dataRes = await res.json();
// === LOGGING: Response Details ===
console.log("=== EXTERNAL STORAGE RESPONSE ===");
console.log("Status:", res.status);
console.log("Status Text:", res.statusText);
console.log("Content-Type:", res.headers.get("content-type"));
console.log("=================================");
// Cek content-type sebelum parse JSON
const contentType = res.headers.get("content-type") || "";
let dataRes;
// Try parse JSON untuk semua response (beberapa server salah set content-type)
try {
const rawResponse = await res.text();
// Coba parse sebagai JSON
try {
dataRes = JSON.parse(rawResponse);
console.log("✅ Successfully parsed response as JSON");
} catch {
// Bukan JSON - gunakan raw text
console.log("⚠️ Response is not JSON, using raw text");
if (res.ok) {
// Success tapi bukan JSON - return success response
return NextResponse.json(
{
success: true,
message: "Success upload file " + keyOfDirectory,
},
{ status: 200 }
);
} else {
return NextResponse.json(
{
success: false,
message: "Upload failed",
error: rawResponse.substring(0, 500),
fileDetails: {
name: file?.name,
type: file?.type,
size: file?.size,
}
},
{ status: res.status || 400 }
);
}
}
} catch (readError) {
console.log("❌ Failed to read response body");
console.log("Read error:", (readError as Error).message);
return NextResponse.json(
{
success: false,
message: "Failed to read response",
reason: (readError as Error).message,
},
{ status: 500 }
);
}
if (res.ok) {
console.log("✅ Upload SUCCESS");
return NextResponse.json(
{
success: true,
@@ -30,20 +127,34 @@ export async function POST(request: Request) {
{ status: 200 }
);
} else {
const errorText = await res.text();
console.log(`Failed upload ${keyOfDirectory}: ${errorText}`);
console.log("❌ Upload FAILED");
console.log("Response:", dataRes);
const errorMessage = dataRes.message || dataRes.error || JSON.stringify(dataRes);
return NextResponse.json(
{ success: false, message: errorText },
{ status: 400 }
{
success: false,
message: errorMessage || "Upload failed",
fileDetails: {
name: file?.name,
type: file?.type,
size: file?.size,
}
},
{ status: res.status || 400 }
);
}
} catch (error) {
console.log("Error upload >>", (error as Error).message || error);
console.log("=== CATCH ERROR ===");
console.log("Error:", (error as Error).message);
console.log("Stack:", (error as Error).stack);
console.log("===================");
return NextResponse.json(
{
success: false,
message: "Failed upload file",
reason: (error as Error).message || error,
reason: (error as Error).message || "Unknown error",
},
{ status: 500 }
);

View File

@@ -1,3 +1,4 @@
import { prisma } from "@/lib";
import { NextResponse } from "next/server";
export async function GET(