com.codercowboy.photo.organizer.service.PhotoIndexer.java Source code

Java tutorial

Introduction

Here is the source code for com.codercowboy.photo.organizer.service.PhotoIndexer.java

Source

package com.codercowboy.photo.organizer.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;

import com.codercowboy.photo.organizer.service.v1.Photo;

/*
 * Copyright 2015 Jason Baker
 * 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.
 */

public class PhotoIndexer {
    public static List<Photo> indexDirectory(String directoryAbsolutePath) throws Exception {
        File directory = new File(directoryAbsolutePath);
        if (!directory.exists()) {
            throw new IOException("Directory for indexing does not exist: " + directoryAbsolutePath);
        }
        if (!directory.canRead()) {
            throw new IOException(
                    "Cannot read directory for indexing, permission denied: " + directoryAbsolutePath);
        }
        List<Photo> photos = new LinkedList<>();
        for (File f : FileUtils.listFiles(directory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)) {
            //TODO: this should be externally configurable
            if (".DS_Store".equals(f.getName())) {
                continue;
            }
            photos.add(indexFile(f));
        }
        return photos;
    }

    public static Photo indexFile(String fileAbsolutePath) throws Exception {
        return indexFile(new File(fileAbsolutePath));
    }

    public static Photo indexFile(File f) throws Exception {
        if (!f.exists()) {
            throw new IOException("Cannot index file, file does not exist: " + f.getAbsolutePath());
        }
        if (!f.canRead()) {
            throw new IOException(
                    "Cannot index file, file is not readable, permission denied:" + f.getAbsolutePath());
        }

        Photo p = new Photo();
        p.setName(f.getName());
        //TODO: this should be relative path to parent
        p.setFileRelativePath(f.getAbsolutePath());
        p.setFileSize(f.length());
        FileInputStream fis = new FileInputStream(f);
        try {
            p.setMd5Checksum(DigestUtils.md5Hex(fis));
        } finally {
            fis.close();
        }
        return p;
    }
}