|
41 | 41 | import java.io.File; |
42 | 42 | import java.io.IOException; |
43 | 43 | import java.io.InputStream; |
44 | | -import java.io.ByteArrayInputStream; |
45 | | -import java.io.ByteArrayOutputStream; |
46 | 44 | import java.io.StringReader; |
47 | 45 | import java.lang.reflect.InvocationTargetException; |
48 | 46 | import java.nio.file.Files; |
|
110 | 108 | import org.glassfish.jersey.server.mvc.Viewable; |
111 | 109 |
|
112 | 110 | import gov.osti.connectors.gitlab.Project; |
| 111 | +import java.io.FileInputStream; |
| 112 | +import org.apache.commons.codec.binary.Base64InputStream; |
113 | 113 |
|
114 | 114 | /** |
115 | 115 | * REST Web Service for Metadata. |
@@ -1034,18 +1034,11 @@ private Response doSubmit(String json, InputStream file, FormDataContentDisposit |
1034 | 1034 | md = em.find(DOECodeMetadata.class, md.getCodeId()); |
1035 | 1035 |
|
1036 | 1036 | 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()); |
1044 | 1038 | md.setFileName(fileName); |
1045 | 1039 |
|
1046 | 1040 | // convert to base64 for GitLab |
1047 | | - bais.reset(); |
1048 | | - base64 = convertBase64(bais); |
| 1041 | + base64 = convertBase64(new FileInputStream(fileName)); |
1049 | 1042 | } catch ( IOException e ) { |
1050 | 1043 | log.error ("File Upload Failed: " + e.getMessage()); |
1051 | 1044 | return ErrorResponse |
@@ -1181,18 +1174,11 @@ private Response doAnnounce(String json, InputStream file, FormDataContentDispos |
1181 | 1174 | md = em.find(DOECodeMetadata.class, md.getCodeId()); |
1182 | 1175 |
|
1183 | 1176 | 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()); |
1191 | 1178 | md.setFileName(fileName); |
1192 | 1179 |
|
1193 | 1180 | // convert to base64 for GitLab |
1194 | | - bais.reset(); |
1195 | | - base64 = convertBase64(bais); |
| 1181 | + base64 = convertBase64(new FileInputStream(fileName)); |
1196 | 1182 | } catch ( IOException e ) { |
1197 | 1183 | log.error ("File Upload Failed: " + e.getMessage()); |
1198 | 1184 | return ErrorResponse |
@@ -1602,20 +1588,15 @@ private static String writeFile(InputStream in, Long codeId, String fileName) th |
1602 | 1588 | } |
1603 | 1589 |
|
1604 | 1590 | /** |
1605 | | - * Convert a File InputStream to a BAse64 string. |
| 1591 | + * Convert a File InputStream to a Base64 string. |
1606 | 1592 | * |
1607 | 1593 | * @param in the InputStream containing the file content |
1608 | 1594 | * @return the Base64 string of the file |
1609 | 1595 | * @throws IOException on IO errors |
1610 | 1596 | */ |
1611 | 1597 | 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"); |
1619 | 1600 | } |
1620 | 1601 |
|
1621 | 1602 | /** |
|
0 commit comments