|
9 | 9 | ********************************************************************************/ |
10 | 10 | package org.eclipse.openvsx; |
11 | 11 |
|
| 12 | +import com.google.common.io.ByteStreams; |
12 | 13 | import jakarta.persistence.EntityManager; |
13 | 14 | import jakarta.transaction.Transactional; |
14 | 15 | import jakarta.transaction.Transactional.TxType; |
| 16 | +import org.apache.commons.io.FileUtils; |
15 | 17 | import org.apache.commons.lang3.StringUtils; |
16 | 18 | import org.eclipse.openvsx.admin.RemoveFileJobRequest; |
17 | 19 | import org.eclipse.openvsx.cache.CacheService; |
@@ -57,6 +59,9 @@ public class ExtensionService { |
57 | 59 | @Value("${ovsx.publishing.require-license:false}") |
58 | 60 | boolean requireLicense; |
59 | 61 |
|
| 62 | + @Value("${ovsx.publishing.max-content-size:" + MAX_CONTENT_SIZE + "}") |
| 63 | + int maxContentSize; |
| 64 | + |
60 | 65 | public ExtensionService( |
61 | 66 | EntityManager entityManager, |
62 | 67 | RepositoryService repositories, |
@@ -107,17 +112,19 @@ private void doPublish(TempFile extensionFile, String binaryName, PersonalAccess |
107 | 112 | } |
108 | 113 |
|
109 | 114 | private TempFile createExtensionFile(InputStream content) { |
110 | | - try (var input = new BufferedInputStream(content)) { |
111 | | - input.mark(0); |
112 | | - var skipped = input.skip(MAX_CONTENT_SIZE + 1); |
113 | | - if (skipped > MAX_CONTENT_SIZE) { |
114 | | - throw new ErrorResultException("The extension package exceeds the size limit of 512 MB.", HttpStatus.PAYLOAD_TOO_LARGE); |
115 | | - } |
116 | | - |
| 115 | + try (var input = ByteStreams.limit(new BufferedInputStream(content), maxContentSize + 1)) { |
| 116 | + long size; |
117 | 117 | var extensionFile = new TempFile("extension_", ".vsix"); |
118 | 118 | try(var out = Files.newOutputStream(extensionFile.getPath())) { |
119 | | - input.reset(); |
120 | | - input.transferTo(out); |
| 119 | + size = input.transferTo(out); |
| 120 | + } |
| 121 | + |
| 122 | + if (size > maxContentSize) { |
| 123 | + try { |
| 124 | + extensionFile.close(); |
| 125 | + } catch (IOException _) {} |
| 126 | + var maxSize = FileUtils.byteCountToDisplaySize(maxContentSize); |
| 127 | + throw new ErrorResultException("The extension package exceeds the size limit of " + maxSize + ".", HttpStatus.PAYLOAD_TOO_LARGE); |
121 | 128 | } |
122 | 129 |
|
123 | 130 | return extensionFile; |
|
0 commit comments