package ovh.herisson.Clyde.EndPoints;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ovh.herisson.Clyde.Services.StorageService;
import ovh.herisson.Clyde.Tables.FileType;
import ovh.herisson.Clyde.Tables.StorageFile;

@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class StorageController {

    private final StorageService storageServ;

    public StorageController(StorageService storageServ){
        this.storageServ= storageServ;
    }


    @PostMapping("/upload/{fileType}")
    public ResponseEntity<StorageFile> handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) {

		StorageFile fileEntry;
		try {
			fileEntry = storageServ.store(file,fileType);
			
		} catch(Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
		}


        return new ResponseEntity<>(fileEntry, HttpStatus.OK);
    }
}
