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

Raise an error if files exceed the max file size

Michael Walker 8 лет назад
Родитель
Сommit
1f35b6dec3

+ 18 - 0
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/util/CastUtil.java

@@ -0,0 +1,18 @@
+package uk.ac.ic.wlgitbridge.bridge.util;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * Created by winston on 01/07/2017.
+ */
+public class CastUtil {
+
+    public static int assumeInt(long l) {
+        Preconditions.checkArgument(
+                l <= (long) Integer.MAX_VALUE
+                        && l >= (long) Integer.MIN_VALUE,
+                l + " cannot fit inside an int");
+        return (int) l;
+    }
+
+}

+ 0 - 7
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/data/filestore/RepositoryFile.java

@@ -1,7 +1,5 @@
 package uk.ac.ic.wlgitbridge.data.filestore;
 package uk.ac.ic.wlgitbridge.data.filestore;
 
 
-import java.util.Map.Entry;
-
 /**
 /**
  * Created by Winston on 16/11/14.
  * Created by Winston on 16/11/14.
  */
  */
@@ -10,11 +8,6 @@ public class RepositoryFile extends RawFile {
     private final String path;
     private final String path;
     private final byte[] contents;
     private final byte[] contents;
 
 
-    public RepositoryFile(Entry<String, byte[]> fileContents) {
-        path = fileContents.getKey();
-        contents = fileContents.getValue();
-    }
-
     public RepositoryFile(String path, byte[] contents) {
     public RepositoryFile(String path, byte[] contents) {
         this.path = path;
         this.path = path;
         this.contents = contents;
         this.contents = contents;

+ 11 - 4
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/SizeLimitExceededException.java

@@ -4,14 +4,21 @@ import uk.ac.ic.wlgitbridge.util.Util;
 
 
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.List;
 import java.util.List;
+import java.util.Optional;
 
 
 public class SizeLimitExceededException extends GitUserException {
 public class SizeLimitExceededException extends GitUserException {
 
 
-    private final String path;
+    private final Optional<String> path;
 
 
-    public SizeLimitExceededException(String path) {
-        super();
+    private final long actualSize;
+
+    private final long maxSize;
+
+    public SizeLimitExceededException(
+            Optional<String> path, long actualSize, long maxSize) {
         this.path = path;
         this.path = path;
+        this.actualSize = actualSize;
+        this.maxSize = maxSize;
     }
     }
 
 
     @Override
     @Override
@@ -22,7 +29,7 @@ public class SizeLimitExceededException extends GitUserException {
     @Override
     @Override
     public List<String> getDescriptionLines() {
     public List<String> getDescriptionLines() {
         String filename =
         String filename =
-                path != null ? "File '" + path + "' is" : "There's a file";
+                path.isPresent() ? "File '" + path.get() + "' is" : "There's a file";
         return Arrays.asList(
         return Arrays.asList(
                 filename + " too large to push to "
                 filename + " too large to push to "
                         + Util.getServiceName() + " via git",
                         + Util.getServiceName() + " via git",

+ 27 - 11
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/util/RepositoryObjectTreeWalker.java

@@ -1,19 +1,22 @@
 package uk.ac.ic.wlgitbridge.git.util;
 package uk.ac.ic.wlgitbridge.git.util;
 
 
-import org.eclipse.jgit.errors.LargeObjectException;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectLoader;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.treewalk.TreeWalk;
 import org.eclipse.jgit.treewalk.TreeWalk;
+import uk.ac.ic.wlgitbridge.bridge.util.CastUtil;
 import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
 import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
 import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
 import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
 import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile;
 import uk.ac.ic.wlgitbridge.data.filestore.RepositoryFile;
 import uk.ac.ic.wlgitbridge.git.exception.InvalidGitRepository;
 import uk.ac.ic.wlgitbridge.git.exception.InvalidGitRepository;
 import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException;
 import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException;
 
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
+import java.util.Optional;
 
 
 /**
 /**
  * Created by Winston on 16/11/14.
  * Created by Winston on 16/11/14.
@@ -46,7 +49,14 @@ public class RepositoryObjectTreeWalker {
 
 
     public RawDirectory getDirectoryContents(
     public RawDirectory getDirectoryContents(
     ) throws IOException, SizeLimitExceededException, InvalidGitRepository {
     ) throws IOException, SizeLimitExceededException, InvalidGitRepository {
-        return new RawDirectory(walkGitObjectTree());
+        return getDirectoryContents(50 * 1024 * 1024);
+    }
+
+    private RawDirectory getDirectoryContents(long maxFileSize)
+            throws IOException,
+                    SizeLimitExceededException,
+                    InvalidGitRepository {
+        return new RawDirectory(walkGitObjectTree(maxFileSize));
     }
     }
 
 
     private TreeWalk initTreeWalk(
     private TreeWalk initTreeWalk(
@@ -63,8 +73,10 @@ public class RepositoryObjectTreeWalker {
         return treeWalk;
         return treeWalk;
     }
     }
 
 
-    private Map<String, RawFile> walkGitObjectTree(
-    ) throws IOException, SizeLimitExceededException, InvalidGitRepository {
+    private Map<String, RawFile> walkGitObjectTree(long maxFileSize)
+            throws IOException,
+                    SizeLimitExceededException,
+                    InvalidGitRepository {
         Map<String, RawFile> fileContentsTable = new HashMap<>();
         Map<String, RawFile> fileContentsTable = new HashMap<>();
         if (treeWalk == null) {
         if (treeWalk == null) {
             return fileContentsTable;
             return fileContentsTable;
@@ -76,14 +88,18 @@ public class RepositoryObjectTreeWalker {
             if (!repository.hasObject(objectId)) {
             if (!repository.hasObject(objectId)) {
                 throw new InvalidGitRepository();
                 throw new InvalidGitRepository();
             }
             }
-            try {
-                byte[] content = repository.open(
-                        objectId
-                ).getBytes();
-                fileContentsTable.put(path, new RepositoryFile(path, content));
-            } catch (LargeObjectException e) {
-                throw new SizeLimitExceededException(path);
+            ObjectLoader obj = repository.open(objectId);
+            long size = obj.getSize();
+            if (size > maxFileSize) {
+                throw new SizeLimitExceededException(
+                        Optional.ofNullable(path), size, maxFileSize);
             }
             }
+            try (ByteArrayOutputStream o = new ByteArrayOutputStream(
+                    CastUtil.assumeInt(size))) {
+                obj.copyTo(o);
+                fileContentsTable.put(
+                        path, new RepositoryFile(path, o.toByteArray()));
+            };
         }
         }
         return fileContentsTable;
         return fileContentsTable;
     }
     }