Newer
Older
screenshot-server / src / main / java / com / screenshot / server / controller / UploadController.java
package com.screenshot.server.controller;

import com.screenshot.server.service.StorageService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class UploadController {

    private final StorageService storageService;

    public UploadController(StorageService storageService) {
        this.storageService = storageService;
    }

    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadFile(
            @RequestParam("file") MultipartFile file) {

        System.out.println("Получен файл: " + file.getOriginalFilename() + 
            " (" + file.getSize() + " bytes)");

        StorageService.SaveResult result = storageService.store(file);

        Map<String, Object> response = new HashMap<>();
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("success", result.success());
        response.put("message", result.message());

        if (result.success()) {
            response.put("filePath", result.filePath());
            response.put("fileSize", result.fileSize());
            return ResponseEntity.ok(response);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    @PostMapping("/upload/multiple")
    public ResponseEntity<Map<String, Object>> uploadMultipleFiles(
            @RequestParam("files") MultipartFile[] files) {

        System.out.println("Получено файлов: " + files.length);

        Map<String, Object> response = new HashMap<>();
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("totalFiles", files.length);

        int successCount = 0;
        int failCount = 0;

        for (MultipartFile file : files) {
            StorageService.SaveResult result = storageService.store(file);
            if (result.success()) {
                successCount++;
            } else {
                failCount++;
            }
        }

        response.put("successCount", successCount);
        response.put("failCount", failCount);
        response.put("success", failCount == 0);
        response.put("message", "Загружено: " + successCount + "/" + files.length);

        return ResponseEntity.ok(response);
    }

    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("timestamp", LocalDateTime.now().toString());
        response.put("service", "Screenshot Storage Server");
        return ResponseEntity.ok(response);
    }

    @GetMapping("/storage/info")
    public ResponseEntity<Map<String, Object>> storageInfo() {
        StorageService.StorageInfo info = storageService.getStorageInfo();

        Map<String, Object> response = new HashMap<>();
        response.put("path", info.path());
        response.put("fileCount", info.fileCount());
        response.put("totalSize", info.formattedSize());
        response.put("totalSizeBytes", info.totalSizeBytes());

        return ResponseEntity.ok(response);
    }

    @ExceptionHandler(org.springframework.web.multipart.MaxUploadSizeExceededException.class)
    public ResponseEntity<Map<String, Object>> handleMaxSizeException(
            org.springframework.web.multipart.MaxUploadSizeExceededException e) {

        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", "Файл превышает максимально допустимый размер");
        response.put("timestamp", LocalDateTime.now().toString());

        return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body(response);
    }
}