Skip to content

Commit

Permalink
Don't read file bytes twice
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Mar 3, 2024
1 parent d6240bb commit 81eda49
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.InputStream;
import org.jetbrains.annotations.Nullable;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

public interface FileService {

Expand All @@ -20,6 +21,8 @@ public interface FileService {

void write(InputStream inputStream, String path, @Nullable String contentType) throws IOException;

void write(MultipartFile file, byte[] fileBytes, String path, @Nullable String contentType) throws IOException;

default void moveFile(String oldPath, String newPath) throws IOException {
this.move(oldPath, newPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@ConditionalOnProperty(value = "hangar.storage.type", havingValue = "local", matchIfMissing = true)
Expand Down Expand Up @@ -61,6 +62,13 @@ public void write(final InputStream inputStream, final String path, final @Nulla
Files.copy(inputStream, p, StandardCopyOption.REPLACE_EXISTING);
}

@Override
public void write(final MultipartFile file, final byte[] fileBytes, final String path, @Nullable final String contentType) throws IOException {
try (final InputStream in = file.getInputStream()) {
this.write(in, path, contentType);
}
}

@Override
public void move(final String oldPathString, final String newPathString) throws IOException {
final Path oldPath = Path.of(oldPathString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.S3Object;

Expand Down Expand Up @@ -75,11 +76,22 @@ public void write(final InputStream inputStream, final String path, final @Nulla
}
}

@Override
public void write(final MultipartFile file, final byte[] fileBytes, final String path, @Nullable final String contentType) throws IOException {
final S3Resource resource = (S3Resource) this.getResource(path);
resource.setObjectMetadata(ObjectMetadata.builder().contentType(contentType).build());
try (final OutputStream outputStream = resource.getOutputStream()) {
outputStream.write(fileBytes);
}
}

@Override
public void moveFile(final String oldPath, final String newPath) throws IOException {
if (!oldPath.startsWith(this.getRoot()) && newPath.startsWith(this.getRoot())) {
// upload from file to s3
this.write(Files.newInputStream(Path.of(oldPath)), newPath, null);
try (final InputStream in = Files.newInputStream(Path.of(oldPath))) {
this.write(in, newPath, null);
}
} else if (oldPath.startsWith(this.getRoot()) && newPath.startsWith(this.getRoot())) {
// "rename" in s3
this.s3Client.copyObject((builder -> builder
Expand All @@ -96,7 +108,9 @@ public void moveFile(final String oldPath, final String newPath) throws IOExcept
public void move(final String oldPath, final String newPath) throws IOException {
if (!oldPath.startsWith(this.getRoot()) && newPath.startsWith(this.getRoot())) {
// upload from file to s3
this.write(Files.newInputStream(Path.of(oldPath)), newPath, null);
try (final InputStream in = Files.newInputStream(Path.of(oldPath))) {
this.write(in, newPath, null);
}
} else if (oldPath.startsWith(this.getRoot()) && newPath.startsWith(this.getRoot())) {
// There is no renaming and there are no directories, so we have to copy and delete individual objects
final String sourceDirectory = oldPath.replace(this.getRoot(), "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.papermc.hangar.util.CryptoUtils;
import io.papermc.hangar.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
Expand Down Expand Up @@ -171,9 +172,12 @@ private String createPendingFile(final MultipartFile file, final String channel,
// read file
final String tmpDir = this.fileService.resolve(userTempDir, platformToResolve.name());
final String tmpPluginFile = this.fileService.resolve(tmpDir, pluginFileName);
final byte[] bytes = file.getInputStream().readAllBytes();
// write
this.fileService.write(file.getInputStream(), tmpPluginFile, null);
final byte[] bytes;
try (final InputStream in = file.getInputStream()) {
bytes = in.readAllBytes();
}
this.fileService.write(file, bytes, tmpPluginFile, null);

// load meta
pluginDataFile = this.pluginDataService.loadMeta(pluginFileName, bytes, this.getHangarPrincipal().getUserId());
} catch (final ConfigurateException configurateException) {
Expand Down

0 comments on commit 81eda49

Please sign in to comment.