From 04dd18615cbacf2c708712e69aca19a769a3a8dc Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Wed, 23 Feb 2022 08:12:39 -0500
Subject: [PATCH] i2psnark: Load sytem mime types if available

---
 .../src/org/klomp/snark/web/MimeTypes.java    | 40 +++++++++++++++++++
 debian/apparmor/i2p                           |  3 ++
 2 files changed, 43 insertions(+)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/MimeTypes.java b/apps/i2psnark/java/src/org/klomp/snark/web/MimeTypes.java
index a5a973657b..5a10b7465d 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/web/MimeTypes.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/web/MimeTypes.java
@@ -14,6 +14,10 @@
 
 package org.klomp.snark.web;
 
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
 import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Map;
@@ -21,6 +25,9 @@ import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import java.util.concurrent.ConcurrentHashMap;
 
+import net.i2p.data.DataHelper;
+import net.i2p.util.SystemVersion;
+
 
 /* ------------------------------------------------------------ */
 /**
@@ -47,6 +54,8 @@ class MimeTypes
 
     public MimeTypes() {
         _mimeMap = new ConcurrentHashMap<String, String>();
+        if (!(SystemVersion.isWindows() || SystemVersion.isMac() || SystemVersion.getMaxMemory() < 100*1024*1024L))
+            loadSystemMimeTypes();
     }
 
     /* ------------------------------------------------------------ */
@@ -86,6 +95,37 @@ class MimeTypes
         }
     }
 
+    /**
+     *  Load mime types from /etc/mime.types
+     *  Format: mimetype suffix1 suffix2 ...
+     *
+     *  @since 0.9.54
+     */
+    private void loadSystemMimeTypes() {
+        BufferedReader in = null;
+        try {
+            in = new BufferedReader(new InputStreamReader(new FileInputStream("/etc/mime.types"), "ISO-8859-1"));
+            while (true) {
+                String line = in.readLine();
+                if (line == null)
+                    break;
+                if (line.startsWith("#"))
+                    continue;
+                String[] s = DataHelper.split(line, "[ \t]+", 16);
+                if (s.length < 2)
+                    continue;
+                for (int i = 1; i < s.length; i++) {
+                    _mimeMap.put(s[i].toLowerCase(Locale.US), s[0]);
+                    //System.out.println("Mapping: '" + s[i] + "' -> '" + s[0] + "'");
+                }
+            }
+            //System.out.println("Loaded " + _mimeMap.size() + " mime types from /etc/mime.types");
+        } catch (IOException ioe) {
+        } finally {
+            if (in != null) try { in.close(); } catch (IOException ioe) {}
+        }
+    }
+
     /* ------------------------------------------------------------ */
     /** Get the MIME type by filename extension.
      *
diff --git a/debian/apparmor/i2p b/debian/apparmor/i2p
index 7b6ae55d5e..147c085741 100644
--- a/debian/apparmor/i2p
+++ b/debian/apparmor/i2p
@@ -90,6 +90,9 @@
   @{PROC}/sys/net/ipv6/conf/all/temp_valid_lft            r,
   @{PROC}/sys/net/ipv6/conf/all/use_tempaddr              r,
 
+  # i2psnark
+  /etc/mime.types                                         r,
+
   # 'm' is needed by the I2P-Bote plugin
   /{,lib/live/mount/overlay/}tmp/                         rwm,
   owner /{,lib/live/mount/overlay/}tmp/hsperfdata_*/      rwk,
-- 
GitLab