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
fbbdc032
Commit
fbbdc032
authored
Nov 30, 2024
by
Sony Montoya Eslava
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[ADD] endpoint zip files
parent
62b5d96f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
2 deletions
+90
-2
DriveApi.java
.../java/web/multitask/trismegistoservices/api/DriveApi.java
+14
-0
IDriveService.java
...ltitask/trismegistoservices/interfaces/IDriveService.java
+3
-0
DriveService.java
...ask/trismegistoservices/services/google/DriveService.java
+73
-2
No files found.
src/main/java/web/multitask/trismegistoservices/api/DriveApi.java
View file @
fbbdc032
...
@@ -2,6 +2,7 @@ package web.multitask.trismegistoservices.api;
...
@@ -2,6 +2,7 @@ package web.multitask.trismegistoservices.api;
import
lombok.AllArgsConstructor
;
import
lombok.AllArgsConstructor
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
...
@@ -52,5 +53,17 @@ public class DriveApi {
...
@@ -52,5 +53,17 @@ public class DriveApi {
return
driveService
.
getFile
(
id
,
!(
base64
==
null
));
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
src/main/java/web/multitask/trismegistoservices/interfaces/IDriveService.java
View file @
fbbdc032
package
web
.
multitask
.
trismegistoservices
.
interfaces
;
package
web
.
multitask
.
trismegistoservices
.
interfaces
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
...
@@ -14,4 +15,5 @@ public interface IDriveService {
...
@@ -14,4 +15,5 @@ public interface IDriveService {
String
deleteFolder
(
String
folder_id
);
String
deleteFolder
(
String
folder_id
);
ResponseEntity
<?>
getFile
(
String
file_id
,
Boolean
base64
);
ResponseEntity
<?>
getFile
(
String
file_id
,
Boolean
base64
);
String
getFolder
(
String
folder_id
);
String
getFolder
(
String
folder_id
);
byte
[]
getZip
(
JSONArray
arrBody
);
}
}
\ No newline at end of file
src/main/java/web/multitask/trismegistoservices/services/google/DriveService.java
View file @
fbbdc032
...
@@ -3,9 +3,14 @@ package web.multitask.trismegistoservices.services.google;
...
@@ -3,9 +3,14 @@ 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.Collections
;
import
java.util.Collections
;
import
java.util.Objects
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
import
lombok.AllArgsConstructor
;
import
lombok.AllArgsConstructor
;
import
org.jetbrains.annotations.NotNull
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
...
@@ -118,6 +123,74 @@ public class DriveService implements IDriveService {
...
@@ -118,6 +123,74 @@ public class DriveService implements IDriveService {
}
}
@Override
@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
)
{
public
String
createFolder
(
String
folder_id
,
String
folder_name
)
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
throw
new
UnsupportedOperationException
(
"Unimplemented method 'createFolder'"
);
throw
new
UnsupportedOperationException
(
"Unimplemented method 'createFolder'"
);
...
@@ -140,5 +213,4 @@ public class DriveService implements IDriveService {
...
@@ -140,5 +213,4 @@ public class DriveService implements IDriveService {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
throw
new
UnsupportedOperationException
(
"Unimplemented method 'getFolder'"
);
throw
new
UnsupportedOperationException
(
"Unimplemented method 'getFolder'"
);
}
}
}
}
\ 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