-
Notifications
You must be signed in to change notification settings - Fork 19
The method createNewFile converts all the inputstream to memory. This… #980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4c656db
860e6bf
fea969c
e131aea
7d9ba6e
4b034f7
c61bbd9
bc33a6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,8 +3,13 @@ | |||||||||||||||
| import com.genexus.util.Encryption; | ||||||||||||||||
| import com.genexus.util.GXService; | ||||||||||||||||
|
|
||||||||||||||||
| import java.io.*; | ||||||||||||||||
| import org.apache.tika.Tika; | ||||||||||||||||
|
|
||||||||||||||||
| public class ExternalProviderHelper { | ||||||||||||||||
|
|
||||||||||||||||
| public static final int BUFFER_MARK_LIMIT = 128 * 1024; | ||||||||||||||||
|
|
||||||||||||||||
| public static String getServicePropertyValue(GXService s, String propName, boolean isSecure){ | ||||||||||||||||
| String value = s.getProperties().get(propName); | ||||||||||||||||
| if (value != null){ | ||||||||||||||||
|
|
@@ -23,4 +28,49 @@ public static String getEnvironmentVariable(String name, boolean required) throw | |||||||||||||||
| } | ||||||||||||||||
| return value; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public static InputStreamWithLength getInputStreamContentLength(InputStream input) throws IOException { | ||||||||||||||||
| File tempFile = File.createTempFile("upload-", ".tmp"); | ||||||||||||||||
|
||||||||||||||||
| File tempFile = File.createTempFile("upload-", ".tmp"); | |
| File tempFile = File.createTempFile("upload-", ".tmp"); | |
| tempFile.setReadable(false, false); // Disable read for all users | |
| tempFile.setWritable(false, false); // Disable write for all users | |
| tempFile.setExecutable(false, false); // Disable execute for all users | |
| tempFile.setReadable(true, true); // Enable read for owner only | |
| tempFile.setWritable(true, true); // Enable write for owner only |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
| import com.google.api.services.storage.model.StorageObject; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import com.google.cloud.storage.*; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
|
|
@@ -170,11 +169,12 @@ private void setBlobAcl(BlobId blobId, ResourceAccessControlList acl) { | |
| } | ||
|
|
||
| public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) { | ||
| try { | ||
| try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) { | ||
| BlobId blobId = BlobId.of(bucket, externalFileName); | ||
| BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); | ||
| byte[] targetArray = IOUtils.toByteArray(input); | ||
| storageClient.create(blobInfo, targetArray); | ||
|
|
||
| storageClient.createFrom(blobInfo, streamInfo.tempFile.toPath()); | ||
|
||
|
|
||
| setBlobAcl(blobId, acl); | ||
| return getResourceUrl(blobInfo, acl); | ||
| } catch (IOException ex) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an exception occurs after creating the temporary file but before returning the InputStreamWithLength, the temporary file will not be cleaned up, causing a resource leak. Consider adding a try-catch block to clean up the temp file on error.