diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java index 2fdc3673dd..82b5729a71 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/DataStore.java @@ -53,7 +53,7 @@ public class DataStore { } } - private void makeCommitsFromSnapshots(String name, Repository repository, List snapshots) throws IOException, GitAPIException { + private void makeCommitsFromSnapshots(String name, Repository repository, List snapshots) throws IOException, GitAPIException, SnapshotPostException { for (Snapshot snapshot : snapshots) { List files = new LinkedList(); files.addAll(snapshot.getSrcs()); diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java index 836bc2f42f..5ba82d20c7 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/model/ResourceFetcher.java @@ -10,6 +10,7 @@ import uk.ac.ic.wlgitbridge.data.model.db.PersistentStore; import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker; import uk.ac.ic.wlgitbridge.snapshot.base.Request; import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException; +import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException; import uk.ac.ic.wlgitbridge.util.Util; import java.io.ByteArrayOutputStream; @@ -28,7 +29,7 @@ public class ResourceFetcher { this.persistentStore = persistentStore; } - public RawFile get(String projectName, String url, String newPath, Repository repository, Map fetchedUrls) throws IOException { + public RawFile get(String projectName, String url, String newPath, Repository repository, Map fetchedUrls) throws IOException, SnapshotPostException { String path = persistentStore.getPathForURLInProject(projectName, url); byte[] contents; if (path == null) { diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java new file mode 100644 index 0000000000..a48d9bdb0b --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java @@ -0,0 +1,37 @@ +package uk.ac.ic.wlgitbridge.git.exception; + +import com.google.gson.JsonElement; +import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException; +import uk.ac.ic.wlgitbridge.util.Util; + +import java.util.Arrays; +import java.util.List; + +public class SizeLimitExceededException extends SnapshotPostException { + + private static String path = null; + + public SizeLimitExceededException(String path) { + super(); + this.path = path; + } + + @Override + public String getMessage() { + return "file too big"; + } + + @Override + public List getDescriptionLines() { + String filename = path != null ? "File '" + path + "' is" : "There's a file"; + return Arrays.asList( + filename + " too large to push to " + Util.getServiceName() + " via git", + "the recommended maximum file size is 15MiB" + ); + } + + @Override + public void fromJSON(JsonElement json) { + + } +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java index 45df790462..fc83040d4c 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/handler/hook/WriteLatexPutHook.java @@ -89,13 +89,13 @@ public class WriteLatexPutHook implements PreReceiveHook { } } - private RawDirectory getPushedDirectoryContents(Repository repository, ReceiveCommand receiveCommand) throws IOException { + private RawDirectory getPushedDirectoryContents(Repository repository, ReceiveCommand receiveCommand) throws IOException, SnapshotPostException { return new RepositoryObjectTreeWalker(repository, receiveCommand.getNewId()) .getDirectoryContents(); } - private RawDirectory getOldDirectoryContents(Repository repository) throws IOException { + private RawDirectory getOldDirectoryContents(Repository repository) throws IOException, SnapshotPostException { return new RepositoryObjectTreeWalker(repository).getDirectoryContents(); } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/util/RepositoryObjectTreeWalker.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/util/RepositoryObjectTreeWalker.java index 9ea31897da..8aab0fb0d6 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/util/RepositoryObjectTreeWalker.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/util/RepositoryObjectTreeWalker.java @@ -1,5 +1,6 @@ package uk.ac.ic.wlgitbridge.git.util; +import org.eclipse.jgit.errors.LargeObjectException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevWalk; @@ -7,6 +8,8 @@ import org.eclipse.jgit.treewalk.TreeWalk; import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory; import uk.ac.ic.wlgitbridge.data.filestore.RawFile; import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile; +import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException; +import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException; import java.io.IOException; import java.util.HashMap; @@ -33,7 +36,7 @@ public class RepositoryObjectTreeWalker { this(repository, repository.resolve("HEAD~" + fromHead)); } - public RawDirectory getDirectoryContents() throws IOException { + public RawDirectory getDirectoryContents() throws IOException, SnapshotPostException { return new RawDirectory(walkGitObjectTree()); } @@ -48,14 +51,21 @@ public class RepositoryObjectTreeWalker { return treeWalk; } - private Map walkGitObjectTree() throws IOException { + private Map walkGitObjectTree() throws IOException, SnapshotPostException { Map fileContentsTable = new HashMap(); if (treeWalk == null) { return fileContentsTable; } while (treeWalk.next()) { String path = treeWalk.getPathString(); - fileContentsTable.put(path, new RepositoryFile(path, repository.open(treeWalk.getObjectId(0)).getBytes())); + + try { + byte[] content = repository.open(treeWalk.getObjectId(0)).getBytes(); + fileContentsTable.put(path, new RepositoryFile(path, content)); + } + catch (LargeObjectException e) { + throw new SizeLimitExceededException(path); + } } return fileContentsTable; }