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
52b19f6d
Commit
52b19f6d
authored
Mar 02, 2026
by
Mauro Paolo Josue Zuñiga Mallqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[ADD] SUBIDA DE DRIVE POR AÑO
parent
f3731d0c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
33 deletions
+56
-33
IDriveService.java
...ltitask/trismegistoservices/interfaces/IDriveService.java
+1
-1
DriveRequest.java
...web/multitask/trismegistoservices/model/DriveRequest.java
+2
-0
driveRest.java
...ava/web/multitask/trismegistoservices/rest/driveRest.java
+2
-1
DriveService.java
...ask/trismegistoservices/services/google/DriveService.java
+51
-31
No files found.
src/main/java/web/multitask/trismegistoservices/interfaces/IDriveService.java
View file @
52b19f6d
...
...
@@ -9,7 +9,7 @@ import org.springframework.web.multipart.MultipartFile;
import
java.io.OutputStream
;
public
interface
IDriveService
{
String
uploadFile
(
String
folder_id
,
String
file_name
,
MultipartFile
file
,
String
base64
);
String
uploadFile
(
String
folder_id
,
String
file_name
,
MultipartFile
file
,
String
base64
,
boolean
do_year
);
String
createFolder
(
String
folder_id
,
String
folder_name
);
String
deleteFile
(
String
file_id
);
String
deleteFolder
(
String
folder_id
);
...
...
src/main/java/web/multitask/trismegistoservices/model/DriveRequest.java
View file @
52b19f6d
...
...
@@ -14,5 +14,6 @@ public class DriveRequest {
private
String
file_name
;
private
MultipartFile
file
;
private
String
base64
;
private
boolean
do_year
;
}
\ No newline at end of file
src/main/java/web/multitask/trismegistoservices/rest/driveRest.java
View file @
52b19f6d
...
...
@@ -27,7 +27,8 @@ public class driveRest {
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
);
boolean
do_year
=
request
.
isDo_year
();
String
id_file_response
=
driveService
.
uploadFile
(
folder_id
,
file_name
,
file
,
base64
,
do_year
);
JSONObject
response
=
new
JSONObject
();
if
(
id_file_response
==
null
)
{
response
.
put
(
"status"
,
false
);
...
...
src/main/java/web/multitask/trismegistoservices/services/google/DriveService.java
View file @
52b19f6d
...
...
@@ -9,14 +9,9 @@ import java.util.Objects;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
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
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
...
...
@@ -41,12 +36,39 @@ public class DriveService implements IDriveService {
private
final
CommonUtils
commonUtils
;
@Override
public
String
uploadFile
(
String
folder_id
,
@Nullable
String
file_name
,
MultipartFile
file
,
String
base64
)
{
public
String
uploadFile
(
String
folder_id
,
@Nullable
String
file_name
,
MultipartFile
file
,
String
base64
,
boolean
do_year
)
{
try
{
String
targetFolderId
=
folder_id
;
if
(
do_year
)
{
String
currentYear
=
String
.
valueOf
(
java
.
util
.
Calendar
.
getInstance
().
get
(
java
.
util
.
Calendar
.
YEAR
));
String
query
=
"name = '"
+
currentYear
+
"' and '"
+
folder_id
+
"' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
;
com
.
google
.
api
.
services
.
drive
.
model
.
FileList
result
=
googleConfig
.
getDrive
().
files
().
list
()
.
setQ
(
query
)
.
setFields
(
"files(id)"
)
.
execute
();
if
(
result
.
getFiles
()
!=
null
&&
!
result
.
getFiles
().
isEmpty
())
{
targetFolderId
=
result
.
getFiles
().
get
(
0
).
getId
();
}
else
{
File
folderMetadata
=
new
File
();
folderMetadata
.
setName
(
currentYear
);
folderMetadata
.
setMimeType
(
"application/vnd.google-apps.folder"
);
folderMetadata
.
setParents
(
Collections
.
singletonList
(
folder_id
));
File
folder
=
googleConfig
.
getDrive
().
files
().
create
(
folderMetadata
).
setFields
(
"id"
).
execute
();
targetFolderId
=
folder
.
getId
();
}
}
if
(
null
!=
file
)
{
File
fileMetadata
=
new
File
();
fileMetadata
.
setParents
(
Collections
.
singletonList
(
folder_id
));
fileMetadata
.
setName
(
file_name
==
null
?
file
.
getOriginalFilename
()
:
file_name
);
fileMetadata
.
setParents
(
Collections
.
singletonList
(
targetFolderId
));
fileMetadata
.
setName
(
file_name
==
null
?
file
.
getOriginalFilename
()
:
file_name
);
File
uploadFile
=
googleConfig
.
getDrive
()
.
files
()
.
create
(
fileMetadata
,
new
InputStreamContent
(
...
...
@@ -54,23 +76,23 @@ public class DriveService implements IDriveService {
new
ByteArrayInputStream
(
file
.
getBytes
())))
.
setFields
(
"id"
).
execute
();
return
uploadFile
.
getId
();
}
else
if
(!
base64
.
isEmpty
())
{
java
.
io
.
File
fileBytes
=
commonUtils
.
base64ToFile
(
base64
,
file_name
);
}
else
if
(
base64
!=
null
&&
!
base64
.
isEmpty
())
{
java
.
io
.
File
fileBytes
=
commonUtils
.
base64ToFile
(
base64
,
file_name
);
File
fileMetadata
=
new
File
();
fileMetadata
.
setParents
(
Collections
.
singletonList
(
folder_i
d
));
fileMetadata
.
setParents
(
Collections
.
singletonList
(
targetFolderI
d
));
fileMetadata
.
setName
(
file_name
);
File
uploadFile
=
googleConfig
.
getDrive
()
.
files
()
.
create
(
fileMetadata
,
new
InputStreamContent
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
,
Files
.
newInputStream
(
fileBytes
.
toPath
())))
"application/octet-stream"
,
java
.
nio
.
file
.
Files
.
newInputStream
(
fileBytes
.
toPath
())))
.
setFields
(
"id"
).
execute
();
return
uploadFile
.
getId
();
}
else
{
return
null
;
}
}
catch
(
Exception
e
)
{
System
.
out
.
printf
(
"Error: "
+
e
);
e
.
printStackTrace
(
);
}
return
null
;
}
...
...
@@ -83,10 +105,10 @@ public class DriveService implements IDriveService {
String
uudi
=
java
.
util
.
UUID
.
randomUUID
().
toString
();
String
nameFile
;
boolean
isHasExtension
=
false
;
try
{
try
{
nameFile
=
System
.
getProperty
(
"java.io.tmpdir"
)
+
"/"
+
uudi
+
"."
+
file
.
getName
().
split
(
"\\."
)[
1
];
isHasExtension
=
true
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
nameFile
=
System
.
getProperty
(
"java.io.tmpdir"
)
+
"/"
+
uudi
+
".pdf"
;
}
java
.
io
.
File
tempFile
=
new
java
.
io
.
File
(
nameFile
);
...
...
@@ -105,8 +127,7 @@ public class DriveService implements IDriveService {
.
put
(
"base64"
,
base64String
)
.
put
(
"file_name"
,
file
.
getName
()
+
(
isHasExtension
?
""
:
".pdf"
))
.
put
(
"status"
,
true
)
.
put
(
"message"
,
"OK"
).
toMap
()
);
.
put
(
"message"
,
"OK"
).
toMap
());
}
else
{
Resource
resource
=
commonUtils
.
fileToResource
(
tempFile
);
HttpHeaders
headers
=
new
HttpHeaders
();
...
...
@@ -146,10 +167,13 @@ public class DriveService implements IDriveService {
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
20
);
/*int numCores = Runtime.getRuntime().availableProcessors();
int maxThreads = 30;
ExecutorService executor = Executors.newFixedThreadPool(Math.min(numCores * 2, maxThreads));*/
/*
* int numCores = Runtime.getRuntime().availableProcessors();
* int maxThreads = 30;
*
* ExecutorService executor = Executors.newFixedThreadPool(Math.min(numCores *
* 2, maxThreads));
*/
try
{
List
<
CompletableFuture
<
Void
>>
futures
=
new
ArrayList
<>();
...
...
@@ -160,8 +184,9 @@ public class DriveService implements IDriveService {
String
fileId
=
reqFile
.
getString
(
"file_id"
);
futures
.
add
(
CompletableFuture
.
runAsync
(()
->
{
try
(
InputStream
inputStream
=
googleConfig
.
getDrive
().
files
().
get
(
fileId
).
executeMediaAsInputStream
();
FileOutputStream
fos
=
new
FileOutputStream
(
new
java
.
io
.
File
(
tempDir
,
fileName
)))
{
try
(
InputStream
inputStream
=
googleConfig
.
getDrive
().
files
().
get
(
fileId
)
.
executeMediaAsInputStream
();
FileOutputStream
fos
=
new
FileOutputStream
(
new
java
.
io
.
File
(
tempDir
,
fileName
)))
{
byte
[]
buffer
=
new
byte
[
1024
];
int
bytesRead
;
...
...
@@ -193,25 +218,21 @@ public class DriveService implements IDriveService {
@Override
public
String
createFolder
(
String
folder_id
,
String
folder_name
)
{
// TODO Auto-generated method stub
throw
new
UnsupportedOperationException
(
"Unimplemented method 'createFolder'"
);
}
@Override
public
String
deleteFile
(
String
file_id
)
{
// TODO Auto-generated method stub
throw
new
UnsupportedOperationException
(
"Unimplemented method 'deleteFile'"
);
}
@Override
public
String
deleteFolder
(
String
folder_id
)
{
// TODO Auto-generated method stub
throw
new
UnsupportedOperationException
(
"Unimplemented method 'deleteFolder'"
);
}
@Override
public
String
getFolder
(
String
folder_id
)
{
// TODO Auto-generated method stub
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