LCOV - code coverage report
Current view: top level - server/mime - DefaultFileExtensionRegistry.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 31 37 83.8 %
Date: 2022-11-19 15:00:39 Functions: 7 10 70.0 %

          Line data    Source code
       1             : // Copyright (C) 2013 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.mime;
      16             : 
      17             : import com.google.common.collect.ImmutableMap;
      18             : import com.google.common.flogger.FluentLogger;
      19             : import eu.medsea.mimeutil.MimeException;
      20             : import eu.medsea.mimeutil.MimeType;
      21             : import eu.medsea.mimeutil.MimeUtil;
      22             : import eu.medsea.mimeutil.detector.MimeDetector;
      23             : import java.io.File;
      24             : import java.io.IOException;
      25             : import java.io.InputStream;
      26             : import java.net.URL;
      27             : import java.util.Collection;
      28             : import java.util.Collections;
      29             : import java.util.Map;
      30             : import java.util.Properties;
      31             : 
      32             : /** Loads mime types from {@code mime-types.properties} at specificity of 2. */
      33         138 : public class DefaultFileExtensionRegistry extends MimeDetector {
      34         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      35             : 
      36             :   private static final ImmutableMap<String, MimeType> TYPES;
      37             : 
      38             :   static {
      39         138 :     Properties prop = new Properties();
      40         138 :     try (InputStream in =
      41         138 :         DefaultFileExtensionRegistry.class.getResourceAsStream("mime-types.properties")) {
      42         138 :       prop.load(in);
      43           0 :     } catch (IOException e) {
      44           0 :       logger.atWarning().withCause(e).log("Cannot load mime-types.properties");
      45         138 :     }
      46             : 
      47         138 :     ImmutableMap.Builder<String, MimeType> b = ImmutableMap.builder();
      48         138 :     for (Map.Entry<Object, Object> e : prop.entrySet()) {
      49         138 :       MimeType type = new FileExtensionMimeType((String) e.getValue());
      50         138 :       b.put((String) e.getKey(), type);
      51         138 :       MimeUtil.addKnownMimeType(type);
      52         138 :     }
      53         138 :     TYPES = b.build();
      54         138 :   }
      55             : 
      56             :   @Override
      57             :   public String getDescription() {
      58         138 :     return getClass().getName();
      59             :   }
      60             : 
      61             :   @Override
      62             :   protected Collection<MimeType> getMimeTypesFileName(String name) {
      63          24 :     int s = name.lastIndexOf('/');
      64          24 :     if (s >= 0) {
      65           1 :       name = name.substring(s + 1);
      66             :     }
      67             : 
      68          24 :     MimeType type = TYPES.get(name);
      69          24 :     if (type != null) {
      70           0 :       return Collections.singletonList(type);
      71             :     }
      72             : 
      73          24 :     int d = name.lastIndexOf('.');
      74          24 :     if (0 < d) {
      75          22 :       type = TYPES.get(name.substring(d + 1));
      76          22 :       if (type != null) {
      77          22 :         return Collections.singletonList(type);
      78             :       }
      79             :     }
      80             : 
      81           6 :     return Collections.emptyList();
      82             :   }
      83             : 
      84             :   @Override
      85             :   protected Collection<MimeType> getMimeTypesFile(File file) {
      86           0 :     return getMimeTypesFileName(file.getName());
      87             :   }
      88             : 
      89             :   @Override
      90             :   protected Collection<MimeType> getMimeTypesURL(URL url) {
      91           0 :     return getMimeTypesFileName(url.getPath());
      92             :   }
      93             : 
      94             :   @Override
      95             :   protected Collection<MimeType> getMimeTypesInputStream(InputStream arg0) {
      96           0 :     return Collections.emptyList();
      97             :   }
      98             : 
      99             :   @Override
     100             :   protected Collection<MimeType> getMimeTypesByteArray(byte[] arg0) {
     101          24 :     return Collections.emptyList();
     102             :   }
     103             : 
     104             :   private static final class FileExtensionMimeType extends MimeType {
     105             :     private static final long serialVersionUID = 1L;
     106             : 
     107             :     FileExtensionMimeType(String mimeType) throws MimeException {
     108         138 :       super(mimeType);
     109         138 :     }
     110             : 
     111             :     @Override
     112             :     public int getSpecificity() {
     113          22 :       return 2;
     114             :     }
     115             :   }
     116             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750