Commit fbbdc032 by Sony Montoya Eslava

[ADD] endpoint zip files

parent 62b5d96f
......@@ -2,6 +2,7 @@ package web.multitask.trismegistoservices.api;
import lombok.AllArgsConstructor;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
......@@ -52,5 +53,17 @@ public class DriveApi {
return driveService.getFile(id, !(base64 == null));
}
@PostMapping("/public/download/zip")
public ResponseEntity<?> downloadZip(@RequestBody String json) {
try {
JSONArray arrBody = new JSONArray(json);
byte[] zip = driveService.getZip(arrBody);
return ResponseEntity.ok().body(zip);
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}
}
\ No newline at end of file
package web.multitask.trismegistoservices.interfaces;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
......@@ -14,4 +15,5 @@ public interface IDriveService {
String deleteFolder(String folder_id);
ResponseEntity<?> getFile(String file_id,Boolean base64);
String getFolder(String folder_id);
byte[] getZip(JSONArray arrBody);
}
\ No newline at end of file
......@@ -3,9 +3,14 @@ package web.multitask.trismegistoservices.services.google;
import java.io.*;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
......@@ -118,6 +123,74 @@ public class DriveService implements IDriveService {
}
@Override
public byte[] getZip(JSONArray reqArrFiles) {
try {
String tempDirPath = System.getProperty("java.io.tmpdir") + "/" + java.util.UUID.randomUUID();
java.io.File tempDir = new java.io.File(tempDirPath);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
for (int i = 0; i < reqArrFiles.length(); i++) {
JSONObject reqFile = reqArrFiles.getJSONObject(i);
String fileName = reqFile.getString("file_name");
String fileId = reqFile.getString("file_id");
InputStream inputStream = googleConfig.getDrive().files().get(fileId).executeMediaAsInputStream();
java.io.File tempFile = new java.io.File(tempDir, fileName);
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
}
ByteArrayOutputStream byteArrayOutputStream = getByteArrayOutputStream(tempDir);
for (java.io.File file : Objects.requireNonNull(tempDir.listFiles())) {
file.delete();
}
tempDir.delete();
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new RuntimeException("Error al crear el ZIP", e);
}
}
@NotNull
private static ByteArrayOutputStream getByteArrayOutputStream(java.io.File tempDir) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) {
for (java.io.File file : tempDir.listFiles()) {
try (FileInputStream fis = new FileInputStream(file)) {
ZipEntry zipEntry = new ZipEntry(file.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
zipOut.closeEntry();
}
}
}
return byteArrayOutputStream;
}
@Override
public String createFolder(String folder_id, String folder_name) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'createFolder'");
......@@ -140,5 +213,4 @@ public class DriveService implements IDriveService {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getFolder'");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment