Implementing File Upload Logic

1. Implementing Multipart Upload Handler

Example: Spring MultipartFile

@PostMapping(value = "/files", consumes = MULTIPART_FORM_DATA_VALUE)
public FileMeta upload(@RequestPart("file") MultipartFile file,
                       @RequestPart("meta") Meta meta) throws IOException {
    return storage.store(file.getOriginalFilename(),
                         file.getContentType(),
                         file.getInputStream(),
                         file.getSize());
}

2. Implementing File Validation

CheckDetail
Size limitReject > max
MIME typeDetect from content (Apache Tika), not header
ExtensionAllow-list only
Magic bytesVerify file signature
FilenameSanitize; prevent path traversal

3. Implementing Storage Logic

BackendUse
Local FSDev only; not horizontally scalable
S3 / GCS / Azure BlobProduction cloud
MinIOS3-compatible self-hosted
DB BLOBSmall files, transactional needs

4. Implementing Chunked Upload

StepDetail
InitiateCreate upload session, get ID
Upload partsSequential or parallel chunks
CompleteServer assembles in order
S3 multipart5 MB min part, 10 000 max parts
tus.io protocolResumable upload standard

5. Implementing Resume Upload Logic

MechanismDetail
HEAD uploadReturns bytes received
Range headerClient resumes from offset
Session TTLCleanup incomplete uploads

6. Handling Large File Uploads

StrategyDetail
Stream, don't bufferAvoid OOM
Pre-signed PUT URLDirect browser → S3, bypass app
Chunked + parallelFaster + resumable
Async post-processVirus scan, transcode in background

7. Implementing Virus Scanning

ToolDetail
ClamAVOpen-source, INSTREAM scan
Cloud (S3 + Lambda)Trigger on upload
QuarantineMark unscanned; serve only after clean
Re-scanOn signature DB updates

8. Implementing Image Processing

OperationTool
ResizeThumbnailator, ImageMagick, libvips
Format convertWebP/AVIF for web
Strip EXIFRemove location/PII metadata
CDN transformCloudinary, imgproxy on demand

9. Implementing Cloud Storage Integration

Example: AWS S3 PutObject

S3Client s3 = S3Client.create();
s3.putObject(PutObjectRequest.builder()
    .bucket("uploads")
    .key("u/" + userId + "/" + uuid + "/" + safeName)
    .contentType(contentType)
    .serverSideEncryption(ServerSideEncryption.AES256)
    .build(), RequestBody.fromInputStream(stream, size));

10. Implementing Pre-Signed URLs

Example: Pre-signed PUT

S3Presigner presigner = S3Presigner.create();
PresignedPutObjectRequest pre = presigner.presignPutObject(b -> b
    .signatureDuration(Duration.ofMinutes(15))
    .putObjectRequest(p -> p.bucket("uploads").key(key).contentType(ct)));
return pre.url().toString();  // client uploads directly

11. Implementing Upload Progress Tracking

MechanismDetail
Client-sideXMLHttpRequest progress events
Server-sidePer-part offset for chunked uploads
WebSocket / SSEPush progress to UI

12. Implementing File Deletion Logic

PracticeDetail
Soft delete firstMark in DB; physical delete async
AuthorizationVerify owner / permission
CascadeDelete derived (thumbnails, conversions)
Audit logWho deleted, when
Lifecycle policiesS3 auto-purge old versions