UploadFolderHandler Documentation

Overview

The UploadFolderHandler class is an HTTP handler that enables authenticated users to upload entire folders (directories with multiple files) to their website's file structure. It provides a secure API endpoint for handling bulk file uploads via multipart form data, typically used by the web-based file management interface for uploading complete directory structures.


Purpose

This handler is part of the multi-tenant hosting service's file management system, allowing users to upload entire folder hierarchies in a single operation. It handles the complex process of parsing multipart form data containing multiple files and potentially nested directory structures, then saving them to the appropriate user directory while preserving the folder organization.


Core Functionality

This handler performs the following key operations:


1. Request Validation


2. Authentication & Authorization


3. Multipart Form Data Processing

The handler performs sophisticated multipart form parsing for folder uploads:


4. Folder Structure Preservation


5. Response Handling

The handler returns different responses based on the outcome:


Request Format

Method: POST

Content-Type: multipart/form-data; boundary={boundary}

Body: Multipart form data containing multiple files with relative paths

The request body follows the standard multipart/form-data format with multiple parts, each representing a file. Each part should contain:


Response Format

Success Response:

Error Response:


Response Codes


Security Features


Error Handling


Logging

The handler provides console logging for debugging:


Use Cases

This handler is typically invoked when users:


Dependencies

This handler relies on several helper methods and components:


Behavior Notes


Differences from UploadHandler

Key differences between UploadFolderHandler and the standard UploadHandler:


Performance Considerations


Potential Improvements

flowchart TD A(["Start handle"]) --> B{"Request method
== POST?
case-insensitive"} B -->|No| C["Send 405 Method Not Allowed"] B -->|Yes| D["Get sessionId via
getJavaSessionId exchange"] C --> Z(["End"]) D --> E["Get username from
SessionManager.getUsername sessionId"] E --> F{"Username
is null?"} F -->|Yes| G["Send 403 Forbidden
Close response body"] F -->|No| H["TRY: Begin folder upload processing"] G --> Z H --> I["Log: Reciving upload..."] I --> J["Get request headers"] J --> K["Extract Content-Type header
requestContentType = headers.getFirst Content-Type"] K --> L["Convert request body to bytes:
body = inputStreamToBytes exchange.getRequestBody"] L --> M["Process multipart folder data:
response = handleMultipartFormDataFolder body requestContentType user"] M --> N["Convert response to bytes:
responseBytes = response.getBytes UTF-8"] N --> O["Send 200 response
with responseBytes.length"] O --> P["Write response.getBytes
to OutputStream"] P --> Q["Close OutputStream"] Q --> Z H --> R["CATCH: Any Exception"] R --> S["Print stack trace:
e.printStackTrace"] S --> T["Create error response:
Upload failed: + e.getMessage"] T --> U["Send 500 Internal Server Error
with response.length"] U --> V["Write response bytes
to OutputStream
Close stream"] V --> Z