HTTP Server

Professional File Uploads

Production-grade configuration for XyPriss's native XHSC-powered file upload system.

Strategic Configuration

For robustness, avoid relative paths in production. Define absolute storage outside the application source. You can use the built-in getMimes helper to automatically translate extension lists into verified MIME types for the XHSC engine.

typescript
import { createServer, getMimes } from "xypriss";

const server = createServer({
    fileUpload: {
        enabled: true,
        maxFileSize: 1024 * 1024 * 500, // 500MB
        destination: "/var/lib/xypriss/uploads",
        // Map file extensions automatically to MIME types
        allowedMimeTypes: getMimes([".pdf", ".docx", ".zip", ".jpg", ".png"]),
        useSubDir: true, // Prevents directory clutter
    },
});

Perimeter Security

  • Magic Number Validation: Verifies file headers to ensure types match extensions.
  • Early Termination: Drops connection immediately if limits are exceeded.

Best Practices

  • Auth First: Always place authorization middleware before the upload handler.
  • Cleanup: Implement a cron job to delete aborted/orphaned temporary files.

MIME Type Helpers (getMimes)

XyPriss provides built-in utilities getMimes and getMime to generate deduplicated MIME arrays without third-party dependencies.

typescript
import { getMimes, getMime } from "xypriss";

// Array resolution & deduplication
const allowedMimes = getMimes([".jpg", ".png", ".pdf"]);
// Output: ["image/jpeg", "image/png", "application/pdf"]

// Single extension resolution
const mime = getMime(".webp"); // Output: "image/webp"
Dedicated MIME Utilities Documentation
For complete details on MIME resolution, extension mapping, controller validation, and global fallbacks, see the dedicated MIME Utilities (getMimes) Documentation.

Multi-Field Management

Use .fields() for transactional requests containing different types of assets.

typescript
const jobApplicationUpload = app.upload.fields([
    { name: "cv", maxCount: 1, allowedExtensions: [".pdf"] },
    { name: "photo", maxCount: 1, allowedExtensions: [".jpg", ".png"] },
]);

app.post("/apply", jobApplicationUpload, (req, res) => {
    const { cv, photo } = (req as any).files;
    res.success({ message: "Application received" });
});
Error Handling
XyPriss throws a FileUploadError when validation fails, allowing you to provide clean feedback to the client without exposing system internals.
MIME Utilities (getMimes)

Learn how to resolve and validate MIME types natively with getMimes.