Commit b21c5c8b by Sony Montoya Eslava

[FIXED] seteo de headers para el endpoint de getfile - Drive

parent fbbdc032
...@@ -2,9 +2,13 @@ package web.multitask.trismegistoservices.services.google; ...@@ -2,9 +2,13 @@ package web.multitask.trismegistoservices.services.google;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
...@@ -99,7 +103,9 @@ public class DriveService implements IDriveService { ...@@ -99,7 +103,9 @@ public class DriveService implements IDriveService {
} else { } else {
Resource resource = commonUtils.fileToResource(tempFile); Resource resource = commonUtils.fileToResource(tempFile);
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
try { try {
...@@ -124,72 +130,60 @@ public class DriveService implements IDriveService { ...@@ -124,72 +130,60 @@ public class DriveService implements IDriveService {
@Override @Override
public byte[] getZip(JSONArray reqArrFiles) { public byte[] getZip(JSONArray reqArrFiles) {
try { String tempDirPath = System.getProperty("java.io.tmpdir") + "/" + java.util.UUID.randomUUID();
String tempDirPath = System.getProperty("java.io.tmpdir") + "/" + java.util.UUID.randomUUID(); java.io.File tempDir = new java.io.File(tempDirPath);
java.io.File tempDir = new java.io.File(tempDirPath);
if (!tempDir.exists()) { if (!tempDir.exists()) {
tempDir.mkdirs(); tempDir.mkdirs();
} }
ExecutorService executor = Executors.newFixedThreadPool(20);
/*int numCores = Runtime.getRuntime().availableProcessors();
int maxThreads = 30;
ExecutorService executor = Executors.newFixedThreadPool(Math.min(numCores * 2, maxThreads));*/
try {
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (int i = 0; i < reqArrFiles.length(); i++) { for (int i = 0; i < reqArrFiles.length(); i++) {
JSONObject reqFile = reqArrFiles.getJSONObject(i); JSONObject reqFile = reqArrFiles.getJSONObject(i);
String fileName = reqFile.getString("file_name"); String fileName = reqFile.getString("file_name");
String fileId = reqFile.getString("file_id"); String fileId = reqFile.getString("file_id");
InputStream inputStream = googleConfig.getDrive().files().get(fileId).executeMediaAsInputStream(); futures.add(CompletableFuture.runAsync(() -> {
try (InputStream inputStream = googleConfig.getDrive().files().get(fileId).executeMediaAsInputStream();
java.io.File tempFile = new java.io.File(tempDir, fileName); FileOutputStream fos = new FileOutputStream(new java.io.File(tempDir, fileName))) {
try (FileOutputStream fos = new FileOutputStream(tempFile)) { byte[] buffer = new byte[1024];
byte[] buffer = new byte[1024]; int bytesRead;
int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) {
while ((bytesRead = inputStream.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead);
fos.write(buffer, 0, bytesRead); }
} catch (Exception e) {
throw new RuntimeException("Error al descargar archivo: " + fileName, e);
} }
} }, executor));
} }
ByteArrayOutputStream byteArrayOutputStream = getByteArrayOutputStream(tempDir); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
ByteArrayOutputStream byteArrayOutputStream = CommonUtils.getByteArrayOutputStream(tempDir);
for (java.io.File file : Objects.requireNonNull(tempDir.listFiles())) { for (java.io.File file : Objects.requireNonNull(tempDir.listFiles())) {
file.delete(); file.delete();
} }
tempDir.delete(); tempDir.delete();
return byteArrayOutputStream.toByteArray(); return byteArrayOutputStream.toByteArray();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Error al crear el ZIP", e); throw new RuntimeException("Error al crear el ZIP", e);
} finally {
executor.shutdown();
} }
} }
@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 @Override
public String createFolder(String folder_id, String folder_name) { public String createFolder(String folder_id, String folder_name) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
......
package web.multitask.trismegistoservices.utils; package web.multitask.trismegistoservices.utils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Service @Service
public class CommonUtils { public class CommonUtils {
...@@ -94,4 +99,28 @@ public class CommonUtils { ...@@ -94,4 +99,28 @@ public class CommonUtils {
return false; return false;
} }
} }
@NotNull
public 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;
}
} }
\ 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