content: /*
   * Copyright (C) 2023 AlexMofer
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package io.github.alexmofer.android.support.utils;
  
  import android.text.TextUtils;
  import android.webkit.MimeTypeMap;
  
  import androidx.annotation.Nullable;
  
  /**
   * MIME工具
   * Created by Alex on 2023/6/22.
   */
  public class MimeTypeUtils {
  
      private MimeTypeUtils() {
          //no instance
      }
  
      private static String getFileExtensionFromUrl(String url) {
          if (!TextUtils.isEmpty(url)) {
              int fragment = url.lastIndexOf('#');
              if (fragment > 0) {
                  url = url.substring(0, fragment);
              }
              int query = url.lastIndexOf('?');
              if (query > 0) {
                  url = url.substring(0, query);
              }
              int filenamePos = url.lastIndexOf('/');
              String filename =
                      0 <= filenamePos ? url.substring(filenamePos + 1) : url;
              if (!filename.isEmpty()) {
                  int dotPos = filename.lastIndexOf('.');
                  if (0 <= dotPos) {
                      return filename.substring(dotPos + 1);
                  }
              }
          }
          return "";
      }
  
      /**
       * 获取MIME类型
       *
       * @param url 链接
       * @return MIME类型
       */
      @Nullable
      public static String getMimeType(String url) {
          String extension = MimeTypeMap.getFileExtensionFromUrl(url);
          if (TextUtils.isEmpty(extension)) {
              // 忽略非法字符，再次获取
              extension = getFileExtensionFromUrl(url);
          }
          return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
      }
  }
  