Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit aca9004

Browse files
author
sowerstl
committed
Improved Base64 conversion for GitLab uploaded file.
1 parent 4086ab7 commit aca9004

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

src/main/java/gov/osti/services/Metadata.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
import java.io.File;
4242
import java.io.IOException;
4343
import java.io.InputStream;
44-
import java.io.ByteArrayInputStream;
45-
import java.io.ByteArrayOutputStream;
4644
import java.io.StringReader;
4745
import java.lang.reflect.InvocationTargetException;
4846
import java.nio.file.Files;
@@ -110,6 +108,8 @@
110108
import org.glassfish.jersey.server.mvc.Viewable;
111109

112110
import gov.osti.connectors.gitlab.Project;
111+
import java.io.FileInputStream;
112+
import org.apache.commons.codec.binary.Base64InputStream;
113113

114114
/**
115115
* REST Web Service for Metadata.
@@ -1034,18 +1034,11 @@ private Response doSubmit(String json, InputStream file, FormDataContentDisposit
10341034
md = em.find(DOECodeMetadata.class, md.getCodeId());
10351035

10361036
try {
1037-
// can only read InputStream once, so copy in order to reset for base64 conversion
1038-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
1039-
org.apache.commons.io.IOUtils.copy(file, baos);
1040-
byte[] bytes = baos.toByteArray();
1041-
1042-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
1043-
fileName = writeFile(bais, md.getCodeId(), fileInfo.getFileName());
1037+
fileName = writeFile(file, md.getCodeId(), fileInfo.getFileName());
10441038
md.setFileName(fileName);
10451039

10461040
// convert to base64 for GitLab
1047-
bais.reset();
1048-
base64 = convertBase64(bais);
1041+
base64 = convertBase64(new FileInputStream(fileName));
10491042
} catch ( IOException e ) {
10501043
log.error ("File Upload Failed: " + e.getMessage());
10511044
return ErrorResponse
@@ -1181,18 +1174,11 @@ private Response doAnnounce(String json, InputStream file, FormDataContentDispos
11811174
md = em.find(DOECodeMetadata.class, md.getCodeId());
11821175

11831176
try {
1184-
// can only read InputStream once, so copy in order to reset for base64 conversion
1185-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
1186-
org.apache.commons.io.IOUtils.copy(file, baos);
1187-
byte[] bytes = baos.toByteArray();
1188-
1189-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
1190-
fileName = writeFile(bais, md.getCodeId(), fileInfo.getFileName());
1177+
fileName = writeFile(file, md.getCodeId(), fileInfo.getFileName());
11911178
md.setFileName(fileName);
11921179

11931180
// convert to base64 for GitLab
1194-
bais.reset();
1195-
base64 = convertBase64(bais);
1181+
base64 = convertBase64(new FileInputStream(fileName));
11961182
} catch ( IOException e ) {
11971183
log.error ("File Upload Failed: " + e.getMessage());
11981184
return ErrorResponse
@@ -1602,20 +1588,15 @@ private static String writeFile(InputStream in, Long codeId, String fileName) th
16021588
}
16031589

16041590
/**
1605-
* Convert a File InputStream to a BAse64 string.
1591+
* Convert a File InputStream to a Base64 string.
16061592
*
16071593
* @param in the InputStream containing the file content
16081594
* @return the Base64 string of the file
16091595
* @throws IOException on IO errors
16101596
*/
16111597
private static String convertBase64(InputStream in) throws IOException {
1612-
byte[] fileBytes = IOUtils.toByteArray(in);
1613-
1614-
in.read(fileBytes, 0, fileBytes.length);
1615-
in.close();
1616-
String base64Str = Base64.encodeBase64String(fileBytes);
1617-
1618-
return base64Str;
1598+
Base64InputStream b64in = new Base64InputStream(in, true);
1599+
return IOUtils.toString(b64in, "UTF-8");
16191600
}
16201601

16211602
/**

0 commit comments

Comments
 (0)