View Javadoc
1   /**
2    * Copyright 2011, Aiki IT, FotoRenamer
3    * <p/>
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * <p/>
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * <p/>
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.aikiit.fotorenamer.image;
17  
18  import com.google.common.base.Strings;
19  import com.google.common.collect.Lists;
20  
21  import java.io.File;
22  import java.io.FilenameFilter;
23  import java.util.List;
24  
25  /**
26   * Filter to prevent wrong files from being manipulated by this tool. Currently
27   * only files with the extensions {@link #EXTENSIONS}.
28   *
29   * @author hirsch
30   * @version 2011-04-02, 13:52
31   */
32  class ImageFilenameFilter implements FilenameFilter {
33  
34      private static final List<String> EXTENSIONS = Lists.newArrayList("jpg", "png");
35  
36      /**
37       * Filter filenames in a directory for images.
38       *
39       * @param dir  Directory to filter filenames in.
40       * @param name Filename to filter.
41       * @return Return <code>true</code> if the given File is a directory and
42       * the file's prefix is part of {@link #EXTENSIONS}.
43       */
44      public final boolean accept(final File dir, final String name) {
45          return !(Strings.isNullOrEmpty(name) || dir == null) && new File(dir, name).isFile() && isSuffixExifExtractable(name);
46      }
47  
48      private static boolean isSuffixExifExtractable(final String name) {
49          if (!Strings.isNullOrEmpty(name)) {
50              String file = name.trim().toLowerCase();
51              for (String suffix : EXTENSIONS) {
52                  if (file.endsWith("." + suffix)) {
53                      return true;
54                  }
55              }
56          }
57          return false;
58  
59      }
60  }