Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
trismegisto-services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mauro Paolo Josue Zuñiga Mallqui
trismegisto-services
Commits
b21c5c8b
Commit
b21c5c8b
authored
Dec 02, 2024
by
Sony Montoya Eslava
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FIXED] seteo de headers para el endpoint de getfile - Drive
parent
fbbdc032
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
34 deletions
+58
-34
DriveService.java
...ask/trismegistoservices/services/google/DriveService.java
+28
-34
CommonUtils.java
.../web/multitask/trismegistoservices/utils/CommonUtils.java
+30
-0
No files found.
src/main/java/web/multitask/trismegistoservices/services/google/DriveService.java
View file @
b21c5c8b
...
@@ -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,7 +130,6 @@ public class DriveService implements IDriveService {
...
@@ -124,7 +130,6 @@ 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
);
...
@@ -132,64 +137,53 @@ public class DriveService implements IDriveService {
...
@@ -132,64 +137,53 @@ public class DriveService implements IDriveService {
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
...
...
src/main/java/web/multitask/trismegistoservices/utils/CommonUtils.java
View file @
b21c5c8b
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment