Просмотр исходного кода

Merge pull request #9 from overleaf/file-too-big-exception

Provide sensible error message for LargeObjectException
Marc Egea i Sala 10 лет назад
Родитель
Сommit
0669f53fcb

+ 1 - 1
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<Snapshot> snapshots) throws IOException, GitAPIException {
+    private void makeCommitsFromSnapshots(String name, Repository repository, List<Snapshot> snapshots) throws IOException, GitAPIException, SnapshotPostException {
         for (Snapshot snapshot : snapshots) {
             List<RawFile> files = new LinkedList<RawFile>();
             files.addAll(snapshot.getSrcs());

+ 2 - 1
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<String, byte[]> fetchedUrls) throws IOException {
+    public RawFile get(String projectName, String url, String newPath, Repository repository, Map<String, byte[]> fetchedUrls) throws IOException, SnapshotPostException {
         String path = persistentStore.getPathForURLInProject(projectName, url);
         byte[] contents;
         if (path == null) {

+ 37 - 0
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<String> 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) {
+
+    }
+}

+ 2 - 2
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();
     }
 

+ 13 - 3
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<String, RawFile> walkGitObjectTree() throws IOException {
+    private Map<String, RawFile> walkGitObjectTree() throws IOException, SnapshotPostException {
         Map<String, RawFile> fileContentsTable = new HashMap<String, RawFile>();
         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;
     }