| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 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.gitiles; |
| 16 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 17 | import com.google.common.base.Enums; |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| David Pursehouse | c962197 | 2016-10-17 14:56:57 +0900 | [diff] [blame] | 20 | import java.util.Arrays; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 21 | import java.util.Optional; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 22 | import org.eclipse.jgit.api.ArchiveCommand; |
| 23 | import org.eclipse.jgit.archive.TarFormat; |
| 24 | import org.eclipse.jgit.archive.Tbz2Format; |
| 25 | import org.eclipse.jgit.archive.TgzFormat; |
| 26 | import org.eclipse.jgit.archive.TxzFormat; |
| 27 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 28 | |
| Dave Borowitz | 29168c6 | 2014-04-16 12:08:23 -0700 | [diff] [blame] | 29 | public enum ArchiveFormat { |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 30 | TGZ("application/x-gzip", new TgzFormat()), |
| 31 | TAR("application/x-tar", new TarFormat()), |
| 32 | TBZ2("application/x-bzip2", new Tbz2Format()), |
| 33 | TXZ("application/x-xz", new TxzFormat()); |
| 34 | // Zip is not supported because it may be interpreted by a Java plugin as a |
| 35 | // valid JAR file, whose code would have access to cookies on the domain. |
| 36 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 37 | private static final ImmutableMap<String, ArchiveFormat> BY_EXT; |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 38 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 39 | static { |
| 40 | ImmutableMap.Builder<String, ArchiveFormat> byExt = ImmutableMap.builder(); |
| 41 | for (ArchiveFormat format : ArchiveFormat.values()) { |
| 42 | for (String ext : format.getSuffixes()) { |
| 43 | byExt.put(ext.toLowerCase(), format); |
| 44 | } |
| 45 | } |
| 46 | BY_EXT = byExt.build(); |
| 47 | } |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 48 | |
| Dave Borowitz | 29168c6 | 2014-04-16 12:08:23 -0700 | [diff] [blame] | 49 | /** Unregister all JGit archive formats supported by Gitiles. */ |
| 50 | public static void unregisterAll() { |
| 51 | for (ArchiveFormat fmt : values()) { |
| Dave Borowitz | 36eb26d | 2014-04-19 16:42:32 -0700 | [diff] [blame] | 52 | ArchiveCommand.unregisterFormat(fmt.getRegisteredName()); |
| Dave Borowitz | 29168c6 | 2014-04-16 12:08:23 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| David Pursehouse | ee4b4d7 | 2019-06-05 13:31:11 +0900 | [diff] [blame] | 56 | @SuppressWarnings("ImmutableEnumChecker") // ArchiveCommand.Format is effectively immutable. |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 57 | private final ArchiveCommand.Format<?> format; |
| David Pursehouse | d79cca7 | 2019-06-10 12:32:46 +0900 | [diff] [blame] | 58 | |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 59 | private final String mimeType; |
| 60 | |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 61 | ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) { |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 62 | this.format = format; |
| 63 | this.mimeType = mimeType; |
| Dave Borowitz | 36eb26d | 2014-04-19 16:42:32 -0700 | [diff] [blame] | 64 | ArchiveCommand.registerFormat(getRegisteredName(), format); |
| 65 | } |
| 66 | |
| 67 | String getRegisteredName() { |
| 68 | return getShortName(); |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | String getShortName() { |
| 72 | return name().toLowerCase(); |
| 73 | } |
| 74 | |
| 75 | String getMimeType() { |
| 76 | return mimeType; |
| 77 | } |
| 78 | |
| 79 | String getDefaultSuffix() { |
| 80 | return getSuffixes().iterator().next(); |
| 81 | } |
| 82 | |
| 83 | Iterable<String> getSuffixes() { |
| 84 | return format.suffixes(); |
| 85 | } |
| 86 | |
| 87 | static ArchiveFormat getDefault(Config cfg) { |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 88 | for (String allowed : cfg.getStringList("archive", null, "format")) { |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 89 | ArchiveFormat result = |
| 90 | Enums.getIfPresent(ArchiveFormat.class, allowed.toUpperCase()).orNull(); |
| 91 | if (result != null) { |
| 92 | return result; |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | return TGZ; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 98 | static ImmutableSet<String> allExtensions() { |
| 99 | return BY_EXT.keySet(); |
| 100 | } |
| 101 | |
| 102 | static Optional<ArchiveFormat> byExtension(String ext, Config cfg) { |
| 103 | ArchiveFormat format = BY_EXT.get(ext); |
| 104 | if (format == null) { |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 105 | return Optional.empty(); |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 106 | } |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 107 | String[] formats = cfg.getStringList("archive", null, "format"); |
| 108 | if (formats.length == 0) { |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 109 | return Optional.of(format); |
| 110 | } |
| David Pursehouse | c962197 | 2016-10-17 14:56:57 +0900 | [diff] [blame] | 111 | return Arrays.stream(formats) |
| 112 | .filter(format.name()::equalsIgnoreCase) |
| 113 | .findFirst() |
| 114 | .map(x -> format); |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 115 | } |
| 116 | } |