Skip to content

Commit 282ef94

Browse files
committed
pack root level files in addition to directories
1 parent f6e8a58 commit 282ef94

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

src/main/java/com/faforever/api/deployment/LegacyFeaturedModDeploymentTask.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
1818
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
19+
import org.apache.commons.io.FilenameUtils;
1920
import org.springframework.beans.factory.config.BeanDefinition;
2021
import org.springframework.context.annotation.Scope;
2122
import org.springframework.stereotype.Component;
@@ -98,7 +99,7 @@ public void run() {
9899

99100
Deployment deployment = apiProperties.getDeployment();
100101
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);
102103

103104
if ("faf".equals(modName)) {
104105
createPatchedExe(version, fileIds, targetFolder).ifPresent(files::add);
@@ -198,12 +199,20 @@ private void updateStatus(String message) {
198199
* @return the list of deployed files
199200
*/
200201
@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) {
202204
updateStatus("Packaging files");
203205
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+
})
207216
.filter(Optional::isPresent)
208217
.map(Optional::get)
209218
.collect(Collectors.toList());
@@ -253,6 +262,31 @@ private Optional<StagedFile> packDirectory(Path directory, Short version, Path t
253262
return Optional.of(new StagedFile(fileId, tmpNxtFile, targetNxtFile, clientFileName));
254263
}
255264

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+
256290
private void checkoutCode(Path repositoryDirectory, String repoUrl, String branch) throws IOException {
257291
if (Files.notExists(repositoryDirectory)) {
258292
createDirectories(repositoryDirectory.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());

0 commit comments

Comments
 (0)