|
16 | 16 | import lombok.extern.slf4j.Slf4j; |
17 | 17 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
18 | 18 | import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; |
| 19 | +import org.apache.commons.io.FilenameUtils; |
19 | 20 | import org.springframework.beans.factory.config.BeanDefinition; |
20 | 21 | import org.springframework.context.annotation.Scope; |
21 | 22 | import org.springframework.stereotype.Component; |
@@ -98,7 +99,7 @@ public void run() { |
98 | 99 |
|
99 | 100 | Deployment deployment = apiProperties.getDeployment(); |
100 | 101 | Path targetFolder = Paths.get(deployment.getFeaturedModsTargetDirectory(), String.format(deployment.getFilesDirectoryFormat(), modName)); |
101 | | - List<StagedFile> files = packageDirectories(repositoryDirectory, version, fileIds, targetFolder); |
| 102 | + List<StagedFile> files = packageFiles(repositoryDirectory, version, fileIds, targetFolder); |
102 | 103 |
|
103 | 104 | if ("faf".equals(modName)) { |
104 | 105 | createPatchedExe(version, fileIds, targetFolder).ifPresent(files::add); |
@@ -198,12 +199,20 @@ private void updateStatus(String message) { |
198 | 199 | * @return the list of deployed files |
199 | 200 | */ |
200 | 201 | @SneakyThrows |
201 | | - private List<StagedFile> packageDirectories(Path repositoryDirectory, short version, Map<String, Short> fileIds, Path targetFolder) { |
| 202 | + @SuppressWarnings("unchecked") |
| 203 | + private List<StagedFile> packageFiles(Path repositoryDirectory, short version, Map<String, Short> fileIds, Path targetFolder) { |
202 | 204 | updateStatus("Packaging files"); |
203 | 205 | try (Stream<Path> stream = Files.list(repositoryDirectory)) { |
204 | | - return stream |
205 | | - .filter((path) -> Files.isDirectory(path) && !path.getFileName().toString().startsWith(".")) |
206 | | - .map(path -> packDirectory(path, version, targetFolder, fileIds)) |
| 206 | + return (List<StagedFile>) stream |
| 207 | + .map(path -> { |
| 208 | + if (Files.isDirectory(path) && !path.getFileName().toString().startsWith(".")) { |
| 209 | + return packDirectory(path, version, targetFolder, fileIds); |
| 210 | + } else if (Files.isRegularFile(path)) { |
| 211 | + return packFile(path, version, targetFolder, fileIds); |
| 212 | + } else{ |
| 213 | + return Optional.empty(); |
| 214 | + } |
| 215 | + }) |
207 | 216 | .filter(Optional::isPresent) |
208 | 217 | .map(Optional::get) |
209 | 218 | .collect(Collectors.toList()); |
@@ -253,6 +262,31 @@ private Optional<StagedFile> packDirectory(Path directory, Short version, Path t |
253 | 262 | return Optional.of(new StagedFile(fileId, tmpNxtFile, targetNxtFile, clientFileName)); |
254 | 263 | } |
255 | 264 |
|
| 265 | + /** |
| 266 | + * Creates a ZIP file with the file ending configured in {@link #featuredMod}. The content of the ZIP file is the |
| 267 | + * content of the directory. If no file ID is available, an empty optional is returned. |
| 268 | + */ |
| 269 | + @SneakyThrows |
| 270 | + private Optional<StagedFile> packFile(Path file, Short version, Path targetFolder, Map<String, Short> fileIds) { |
| 271 | + String fullFileName = file.getFileName().toString(); |
| 272 | + String baseName = FilenameUtils.getBaseName(fullFileName); |
| 273 | + String extension = FilenameUtils.getExtension(fullFileName); |
| 274 | + Path targetFile = targetFolder.resolve(String.format("%s_%d.%s", baseName, version, extension)); |
| 275 | + Path tmpFile = toTmpFile(targetFile); |
| 276 | + |
| 277 | + Short fileId = fileIds.get(fullFileName); |
| 278 | + if (fileId == null) { |
| 279 | + log.debug("Skipping file '{}' because there's no file ID available", fullFileName); |
| 280 | + return Optional.empty(); |
| 281 | + } |
| 282 | + |
| 283 | + log.trace("Copying '{}' to '{}'", file, targetFolder); |
| 284 | + |
| 285 | + createDirectories(targetFolder, FilePermissionUtil.directoryPermissionFileAttributes()); |
| 286 | + Files.copy(file, tmpFile, StandardCopyOption.REPLACE_EXISTING); |
| 287 | + return Optional.of(new StagedFile(fileId, tmpFile, targetFile, fullFileName)); |
| 288 | + } |
| 289 | + |
256 | 290 | private void checkoutCode(Path repositoryDirectory, String repoUrl, String branch) throws IOException { |
257 | 291 | if (Files.notExists(repositoryDirectory)) { |
258 | 292 | createDirectories(repositoryDirectory.getParent(), FilePermissionUtil.directoryPermissionFileAttributes()); |
|
0 commit comments