package web.multitask.trismegistoservices.api; import lombok.AllArgsConstructor; import org.json.JSONObject; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import web.multitask.trismegistoservices.model.DriveRequest; import web.multitask.trismegistoservices.services.google.DriveService; import javax.annotation.Nullable; @RestController @CrossOrigin(origins = "*") @RequestMapping("drive") @AllArgsConstructor public class DriveApi { DriveService driveService; @PostMapping(path = "/public/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseEntity<?> uploadFile(@ModelAttribute DriveRequest request) { try { String folder_id = request.getFolder_id(); String file_name = request.getFile_name(); MultipartFile file = request.getFile(); String base64 = request.getBase64(); String id_file_response = driveService.uploadFile(folder_id, file_name, file, base64); JSONObject response = new JSONObject(); if (id_file_response == null) { response.put("status", false); response.put("message", "No se pudo subir el archivo," + " existe multiples razones por las cuales esto puede ocurrir," + " por favor verifique que el archivo no sea muy grande o que el formato sea correcto"); return ResponseEntity.badRequest().body(response.toMap()); } else { response.put("status", true); response.put("message", "Archivo subido correctamente"); response.put("file_id", id_file_response); response.put("file_url", "https://drive.google.com/file/d/" + id_file_response + "/view?usp=sharing"); response.put("download_url", "https://drive.google.com/uc?export=download&id="+id_file_response); return ResponseEntity.ok(response.toMap()); } } catch (Exception e) { return ResponseEntity.internalServerError().body(e.getMessage()); } } @GetMapping(path = "/public/download/{id}") public ResponseEntity<?> downloadFile(@PathVariable String id, @Nullable @RequestParam(name = "base64") Boolean base64) { return driveService.getFile(id, !(base64 == null)); } }