From 78753bc22f140f863aa3fe56b1c59699ca3e2fa8 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 26 Sep 2011 22:29:07 -0400
Subject: [PATCH] Protect DownloadZipServlet with an AccessRestrictionFilter.
---
src/com/gitblit/utils/FileUtils.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/src/com/gitblit/utils/FileUtils.java b/src/com/gitblit/utils/FileUtils.java
index 468b2a8..73bef34 100644
--- a/src/com/gitblit/utils/FileUtils.java
+++ b/src/com/gitblit/utils/FileUtils.java
@@ -16,9 +16,12 @@
package com.gitblit.utils;
import java.io.BufferedReader;
+import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
/**
@@ -56,4 +59,48 @@
}
return sb.toString();
}
+
+ /**
+ * Writes the string content to the file.
+ *
+ * @param file
+ * @param content
+ */
+ public static void writeContent(File file, String content) {
+ try {
+ OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file),
+ Charset.forName("UTF-8"));
+ BufferedWriter writer = new BufferedWriter(os);
+ writer.append(content);
+ writer.close();
+ } catch (Throwable t) {
+ System.err.println("Failed to write content of " + file.getAbsolutePath());
+ t.printStackTrace();
+ }
+ }
+
+ /**
+ * Recursively traverses a folder and its subfolders to calculate the total
+ * size in bytes.
+ *
+ * @param directory
+ * @return folder size in bytes
+ */
+ public static long folderSize(File directory) {
+ if (directory == null || !directory.exists()) {
+ return -1;
+ }
+ if (directory.isFile()) {
+ return directory.length();
+ }
+ long length = 0;
+ for (File file : directory.listFiles()) {
+ if (file.isFile()) {
+ length += file.length();
+ } else {
+ length += folderSize(file);
+ }
+ }
+ return length;
+ }
}
--
Gitblit v1.9.1