Example usage for java.nio.file.attribute BasicFileAttributes lastModifiedTime

List of usage examples for java.nio.file.attribute BasicFileAttributes lastModifiedTime

Introduction

In this page you can find the example usage for java.nio.file.attribute BasicFileAttributes lastModifiedTime.

Prototype

FileTime lastModifiedTime();

Source Link

Document

Returns the time of last modification.

Usage

From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java

/**
 * Invoked for a file in a directory./*from www.j a  v  a  2 s.co  m*/
 * Unless overridden, this method returns {@link java.nio.file.FileVisitResult#CONTINUE
 * CONTINUE}.
 *
 * @param aPath Path instance.
 * @param aFileAttributes File attribute instance.
 */
@Override
public FileVisitResult visitFile(Path aPath, BasicFileAttributes aFileAttributes) throws IOException {
    Logger appLogger = mAppMgr.getLogger(this, "visitFile");

    String pathFileName = aPath.toAbsolutePath().toString();
    if (mCrawlIgnore.isMatchedNormalized(pathFileName))
        appLogger.debug(String.format("Ignoring File: %s", pathFileName));
    else {
        File fsFile = aPath.toFile();
        if ((fsFile.canRead()) && (mBag != null)) {
            String crawlType = mCrawlQueue.getCrawlType();
            if (StringUtils.equals(crawlType, Connector.CRAWL_TYPE_INCREMENTAL)) {
                String docId = generateDocumentId(aPath);
                boolean docExistsInIndex = documentExistsInIndex(docId);
                if (docExistsInIndex) {
                    Date incDate = mCrawlQueue.getCrawlLastModified();
                    FileTime lastModifiedTime = aFileAttributes.lastModifiedTime();
                    Date lmDate = new Date(lastModifiedTime.toMillis());
                    if (lmDate.after(incDate))
                        processFile(aPath, aFileAttributes);
                } else
                    processFile(aPath, aFileAttributes);
            } else
                processFile(aPath, aFileAttributes);
        } else
            appLogger.warn(String.format("Access Failed: %s", pathFileName));
    }

    if (mAppMgr.isAlive())
        return FileVisitResult.CONTINUE;
    else
        return FileVisitResult.TERMINATE;
}

From source file:org.alfresco.extension.bulkimport.source.fs.FilesystemBulkImportItemVersion.java

private final synchronized void loadMetadataIfNecessary() {
    if (cachedMetadata == null) {
        cachedMetadata = metadataLoader.loadMetadata(metadataReference);
        contentIsInPlace = false;/*from  www .  ja  v a 2s .c o m*/

        if (contentReference != null) {
            try {
                final Path path = contentReference.toPath();
                final BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);

                // If not set in the metadata file, set the creation timestamp to what's on disk
                if (!cachedMetadata.getProperties().containsKey(ContentModel.PROP_CREATED.toString())
                        && !cachedMetadata.getProperties()
                                .containsKey(ContentModel.PROP_CREATED.toPrefixString())
                        && attributes.creationTime() != null) {
                    final Date created = new Date(attributes.creationTime().toMillis());
                    cachedMetadata.addProperty(ContentModel.PROP_CREATED.toString(), created);
                }

                // If not set in the metadata file, set the modification timestamp to what's on disk
                if (!cachedMetadata.getProperties().containsKey(ContentModel.PROP_MODIFIED.toString())
                        && !cachedMetadata.getProperties()
                                .containsKey(ContentModel.PROP_MODIFIED.toPrefixString())
                        && attributes.lastModifiedTime() != null) {
                    final Date modified = new Date(attributes.lastModifiedTime().toMillis());
                    cachedMetadata.addProperty(ContentModel.PROP_MODIFIED.toString(), modified);
                }

                // If an in-place import is possible, attempt to construct a content URL
                if (!contentReference.isDirectory()
                        && isInContentStore(configuredContentStore, contentReference)) {
                    final ContentData contentData = buildContentProperty(mimeTypeService,
                            configuredContentStore, contentReference);

                    if (contentData != null) {
                        // We have valid in-place content
                        contentIsInPlace = true;
                        cachedMetadata.addProperty(ContentModel.PROP_CONTENT.toString(), contentData);
                    } else {
                        if (warn(FilesystemBulkImportItem.log))
                            warn(FilesystemBulkImportItem.log, "Unable to in-place import '"
                                    + getFileName(contentReference) + "'. Will stream it instead.");
                    }
                }
            } catch (final IOException ioe) {
                // Not much we can do in this case - log it and keep on truckin'
                if (warn(FilesystemBulkImportItem.log))
                    warn(FilesystemBulkImportItem.log,
                            "Unable to read file attributes for " + contentReference.getAbsolutePath()
                                    + ". Creation and modification timestamps will be system generated.",
                            ioe);
            }
        }
    }
}

From source file:gobblin.example.simplejson.SimpleJsonSource.java

@Override
public List<WorkUnit> getWorkunits(SourceState state) {
    List<WorkUnit> workUnits = Lists.newArrayList();

    if (!state.contains(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL)) {
        return workUnits;
    }//w ww  .  j  a  v a 2s.c o m

    // Create a single snapshot-type extract for all files
    Extract extract = new Extract(state, Extract.TableType.SNAPSHOT_ONLY,
            state.getProp(ConfigurationKeys.EXTRACT_NAMESPACE_NAME_KEY, "ExampleNamespace"), "ExampleTable");

    String filesToPull = state.getProp(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL);
    File tempFileDir = new File("test_temp/"); // TODO: Delete the dir after completion.
    tempFileDir.mkdir();
    String tempFileDirAbsolute = "";
    try {
        tempFileDirAbsolute = tempFileDir.getCanonicalPath(); // Retrieve absolute path of temp folder
    } catch (IOException e) {
        e.printStackTrace();
    }

    int nameCount = 0;
    int csvCount = 0;
    for (String file : Splitter.on(',').omitEmptyStrings().split(filesToPull)) {
        Iterator it = FileUtils.iterateFiles(new File(file), null, true);
        while (it.hasNext()) {
            try {
                File newFile = (File) it.next();
                String basePath = newFile.getCanonicalPath(); // Retrieve absolute path of source
                Path path = newFile.toPath();

                //call to rest api:, provide with file basePath
                String extension = "";
                System.out.println("basePath is" + basePath);
                int i = basePath.lastIndexOf('.');
                System.out.println("i");
                if (i > 0) {
                    extension = basePath.substring(i + 1);
                }

                String url_file_name = "";
                int j = basePath.lastIndexOf('/');
                if (j > 0) {
                    url_file_name = basePath.substring(j + 1);
                }

                //hand off to rest api
                if (extension.equals("csv")) {
                    System.out.println("CSVCSVCSV");
                    csvCount += 1;
                    System.out.println("CURL________________________________________");
                    //Include basePath, filename, location you want to store file
                    System.out.println(
                            "curl http://localhost:8080" + basePath + "/" + Integer.toString(nameCount));
                    //10.0.2.2 is localhost from vagrant
                    // Insert the nameCount so that it can be joined back later.
                    Process p = Runtime.getRuntime()
                            .exec("curl http://localhost:8080" + basePath + "/" + Integer.toString(nameCount));
                    // String myUrl = "http://localhost:8080/parse" + basePath + "&" + url_file_name + "&" + tempFileDirAbsolute;
                    // System.out.println("------------------------------");
                    // System.out.println(myUrl);
                    // try {
                    //   URL url = new URL(myUrl);
                    //   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //   connection.setRequestMethod("GET");
                    //   connection.connect();
                    // } catch (Exception e) {
                    //   e.printStackTrace();
                    // }

                }
                // Print filename and associated metadata 
                System.out.println(basePath);
                BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
                // System.out.println("  creationTime: " + attr.creationTime());
                // System.out.println("  lastAccessTime: " + attr.lastAccessTime());
                // System.out.println("  lastModifiedTime: " + attr.lastModifiedTime());
                // System.out.println("  isDirectory: " + attr.isDirectory());
                // System.out.println("  isOther: " + attr.isOther());
                // System.out.println("  isRegularFile: " + attr.isRegularFile());
                // System.out.println("  isSymbolicLink: " + attr.isSymbolicLink());
                // System.out.println("  size: " + attr.size()); 
                // System.out.println(" ");

                //creating intermediate JSON
                JSONObject intermediate = new JSONObject();
                intermediate.put("filename", basePath);
                intermediate.put("timestamp", String.valueOf((new Date()).getTime()));
                intermediate.put("namespace", getMacAddress());

                intermediate.put("creationTime", String.valueOf(attr.creationTime()));
                intermediate.put("lastAccessTime", String.valueOf(attr.lastAccessTime()));
                intermediate.put("lastModifiedTime", String.valueOf(attr.lastModifiedTime()));
                intermediate.put("isDirectory", String.valueOf(attr.isDirectory()));
                intermediate.put("isOther", String.valueOf(attr.isOther()));
                intermediate.put("isRegularFile", String.valueOf(attr.isRegularFile()));
                intermediate.put("isSymbolicLink", String.valueOf(attr.isSymbolicLink()));
                intermediate.put("size", attr.size());

                // Create intermediate temp file
                nameCount += 1;
                String intermediateName = "/generated" + String.valueOf(nameCount) + ".json";
                String finalName = tempFileDirAbsolute + intermediateName;
                FileWriter generated = new FileWriter(finalName);
                generated.write(intermediate.toJSONString());
                generated.flush();
                generated.close();

                // Create one work unit for each file to pull
                WorkUnit workUnit = new WorkUnit(state, extract);
                workUnit.setProp(SOURCE_FILE_KEY, finalName);
                workUnits.add(workUnit);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // write out number of files found to temp file
        try {
            FileWriter numCsvFiles = new FileWriter(tempFileDirAbsolute + "/numCsvFiles.txt");
            numCsvFiles.write("" + csvCount);
            numCsvFiles.flush();
            numCsvFiles.close();

            FileWriter numFiles = new FileWriter(tempFileDirAbsolute + "/numFiles.txt");
            numFiles.write("" + nameCount);
            numFiles.flush();
            numFiles.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return workUnits;
}

From source file:ch.mattrero.foldersync.FoldersSynchronizer.java

void syncTree(final Path sourceSubDir) {

    SyncStatus status = null;/* www  .ja va 2  s.c o m*/
    BasicFileAttributes fromAttributes = null;
    BasicFileAttributes toAttributes = null;

    try (final DirectoryStream<Path> sourceStream = Files.newDirectoryStream(sourceSubDir);
            DirectoryStream<Path> backupStream = Files
                    .newDirectoryStream(resolveBackupItemPath(sourceSubDir))) {

        final Iterator<Path> sourceIterator = sourceStream.iterator();
        final Iterator<Path> backupIterator = backupStream.iterator();

        Path sourceItem = (sourceIterator.hasNext() ? sourceIterator.next() : null);
        Path backupItem = (backupIterator.hasNext() ? backupIterator.next() : null);

        while (sourceItem != null || backupItem != null) {
            if (sourceItem == null) {
                status = DELETED;
            } else if (backupItem == null) {
                status = ADDED;
            } else if (sourceDir.relativize(sourceItem).compareTo(backupDir.relativize(backupItem)) < 0) {
                status = ADDED;
            } else if (sourceDir.relativize(sourceItem).compareTo(backupDir.relativize(backupItem)) > 0) {
                status = DELETED;
            } else if (Files.isDirectory(sourceItem) != Files.isDirectory(backupItem)) {
                status = MODIFIED;
            } else if (Files.isDirectory(sourceItem)) {
                status = SYNCHRONIZED;
            } else {
                fromAttributes = Files.readAttributes(sourceItem, BasicFileAttributes.class);
                toAttributes = Files.readAttributes(backupItem, BasicFileAttributes.class);

                if (Math.abs(fromAttributes.lastModifiedTime().toMillis()
                        - toAttributes.lastModifiedTime().toMillis()) > 0) {
                    status = MODIFIED;
                } else if (fromAttributes.size() != toAttributes.size()) {
                    status = MODIFIED;
                } else {
                    status = SYNCHRONIZED;
                }
            }

            switch (status) {
            case ADDED:
                syncAdded(sourceItem);
                sourceItem = (sourceIterator.hasNext() ? sourceIterator.next() : null);
                break;
            case DELETED:
                syncDeleted(sourceDir.resolve(backupDir.relativize(backupItem)));
                backupItem = (backupIterator.hasNext() ? backupIterator.next() : null);
                break;
            case MODIFIED:
                syncModified(sourceItem);
            case SYNCHRONIZED:
            default:
                if (Files.isDirectory(sourceItem)) {
                    syncTree(sourceItem);
                }
                sourceItem = (sourceIterator.hasNext() ? sourceIterator.next() : null);
                backupItem = (backupIterator.hasNext() ? backupIterator.next() : null);
                break;
            }
        }

    } catch (final IOException | SecurityException e) {
        logger.debug("Failed to sync tree " + sourceSubDir, e);
    }
}

From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java

private void processFile(Path aPath, BasicFileAttributes aFileAttributes) throws IOException {
    Logger appLogger = mAppMgr.getLogger(this, "processFile");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from  www.j av a  2s .c om*/

    File fsFile = aPath.toFile();
    String docId = generateDocumentId(aPath);
    String pathFileName = aPath.toAbsolutePath().toString();
    appLogger.debug(String.format("Processing File (%s): %s", docId, pathFileName));

    boolean isFileFlat = true;
    Document fsDocument = new Document(Constants.FS_DOCUMENT_TYPE, mBag);
    DataBag fileBag = fsDocument.getBag();
    fileBag.resetValuesWithDefaults();
    fileBag.setValueByName("nsd_id", docId);
    String fileName = fsFile.getName();
    fileBag.setValueByName("nsd_url", fsFile.toURI().toURL().toString());
    String viewURL = createViewURL(docId);
    fileBag.setValueByName("nsd_url_view", viewURL);
    fileBag.setValueByName("nsd_url_display", viewURL);
    fileBag.setValueByName("nsd_name", fileName);
    fileBag.setValueByName("nsd_file_name", fileName);
    fileBag.setValueByName("nsd_file_size", aFileAttributes.size());
    FileTime creationTime = aFileAttributes.creationTime();
    Date cDate = new Date(creationTime.toMillis());
    fileBag.setValueByName("nsd_doc_created_ts", cDate);
    FileTime lastModifiedTime = aFileAttributes.lastModifiedTime();
    Date lmDate = new Date(lastModifiedTime.toMillis());
    fileBag.setValueByName("nsd_doc_modified_ts", lmDate);
    fileBag.setValueByName("nsd_crawl_type", mCrawlQueue.getCrawlType());

    DataField dataField = fileBag.getFirstFieldByFeatureName(Field.FEATURE_IS_CONTENT);
    if (dataField != null) {
        ContentExtractor contentExtractor = new ContentExtractor(mAppMgr);
        contentExtractor.setCfgPropertyPrefix(Constants.CFG_PROPERTY_PREFIX + ".extract");
        try {
            String mimeType = contentExtractor.detectType(fsFile);
            if (StringUtils.isNotEmpty(mimeType))
                fileBag.setValueByName("nsd_mime_type", mimeType);
            if (isExpandableCSVFile(mimeType)) {
                isFileFlat = false;
                processCSVFile(aPath, aFileAttributes, viewURL);
            } else
                contentExtractor.process(pathFileName, dataField);
        } catch (NSException e) {
            String msgStr = String.format("%s: %s", pathFileName, e.getMessage());
            appLogger.error(msgStr);
        }
    }

    if (isFileFlat) {
        fileBag.setValueByName("nsd_doc_hash", fsDocument.generateUniqueHash(false));
        saveAddQueueDocument(fsDocument, stopWatch);
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java

private void processCSVFile(Path aPath, BasicFileAttributes aFileAttributes, String aViewURL)
        throws IOException {
    String docId;/*from ww  w .  jav  a2 s.co  m*/
    StopWatch stopWatch;
    Document fsDocument;
    Logger appLogger = mAppMgr.getLogger(this, "processCSVFile");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    File fsFile = aPath.toFile();
    String pathFileName = aPath.toAbsolutePath().toString();

    appLogger.debug(String.format("Processing CSV File: %s", pathFileName));

    CSVDocument csvDocument = new CSVDocument(mAppMgr, mBag);
    csvDocument.open(pathFileName);

    int row = 1;
    DataBag csvBag = csvDocument.extractNext();
    while (csvBag != null) {
        stopWatch = new StopWatch();
        stopWatch.start();

        docId = csvBag.generateUniqueHash(true);
        appLogger.debug(String.format(" Expanding Row [%d]: %s", row++, docId));

        csvBag.setValueByName("nsd_id", mIdValuePrefix + docId);
        csvBag.setValueByName("nsd_url", fsFile.toURI().toURL().toString());
        csvBag.setValueByName("nsd_url_view", aViewURL);
        csvBag.setValueByName("nsd_url_display", aViewURL);
        csvBag.setValueByName("nsd_file_name", fsFile.getName());
        csvBag.setValueByName("nsd_mime_type", Content.CONTENT_TYPE_TXT_CSV);
        FileTime creationTime = aFileAttributes.creationTime();
        Date cDate = new Date(creationTime.toMillis());
        csvBag.setValueByName("nsd_doc_created_ts", cDate);
        FileTime lastModifiedTime = aFileAttributes.lastModifiedTime();
        Date lmDate = new Date(lastModifiedTime.toMillis());
        csvBag.setValueByName("nsd_doc_modified_ts", lmDate);
        csvBag.setValueByName("nsd_crawl_type", mCrawlQueue.getCrawlType());
        fsDocument = new Document(Constants.FS_DOCUMENT_TYPE, csvBag);
        csvBag.setValueByName("nsd_doc_hash", fsDocument.generateUniqueHash(false));

        saveAddQueueDocument(fsDocument, stopWatch);

        csvBag = csvDocument.extractNext();
    }

    csvDocument.close();

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:com.github.zhanhb.ckfinder.download.PathPartial.java

/**
 * Serve the specified resource, optionally including the data content.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param content Should the content be included?
 * @param path the resource to serve//from  ww  w .  j a v a  2  s .  c  o m
 *
 * @exception IOException if an input/output error occurs
 */
private void serveResource(HttpServletRequest request, HttpServletResponse response, boolean content, Path path)
        throws IOException, ServletException {
    ActionContext context = new ActionContext().put(HttpServletRequest.class, request)
            .put(HttpServletResponse.class, response).put(ServletContext.class, request.getServletContext())
            .put(Path.class, path);
    if (path == null) {
        notFound.handle(context);
        return;
    }
    BasicFileAttributes attr;
    try {
        attr = Files.readAttributes(path, BasicFileAttributes.class);
    } catch (IOException ex) {
        notFound.handle(context);
        return;
    }
    context.put(BasicFileAttributes.class, attr);

    boolean isError = response.getStatus() >= HttpServletResponse.SC_BAD_REQUEST;
    // Check if the conditions specified in the optional If headers are
    // satisfied.
    // Checking If headers
    boolean included = (request.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH) != null);
    String etag = this.eTag.getValue(context);
    if (!included && !isError && !checkIfHeaders(request, response, attr, etag)) {
        return;
    }
    // Find content type.
    String contentType = contentTypeResolver.getValue(context);
    // Get content length
    long contentLength = attr.size();
    // Special case for zero length files, which would cause a
    // (silent) ISE
    boolean serveContent = content && contentLength != 0;
    Range[] ranges = null;
    if (!isError) {
        if (useAcceptRanges) {
            // Accept ranges header
            response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
        }
        // Parse range specifier
        ranges = serveContent ? parseRange(request, response, attr, etag) : FULL;
        // ETag header
        response.setHeader(HttpHeaders.ETAG, etag);
        // Last-Modified header
        response.setDateHeader(HttpHeaders.LAST_MODIFIED, attr.lastModifiedTime().toMillis());
    }
    ServletOutputStream ostream = null;
    if (serveContent) {
        ostream = response.getOutputStream();
    }

    String disposition = contentDisposition.getValue(context);
    if (disposition != null) {
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
    }

    // Check to see if a Filter, Valve of wrapper has written some content.
    // If it has, disable range requests and setting of a content length
    // since neither can be done reliably.
    if (isError || ranges == FULL) {
        // Set the appropriate output headers
        if (contentType != null) {
            log.debug("serveFile: contentType='{}'", contentType);
            response.setContentType(contentType);
        }
        if (contentLength >= 0) {
            setContentLengthLong(response, contentLength);
        }
        // Copy the input stream to our output stream (if requested)
        if (serveContent) {
            log.trace("Serving bytes");
            Files.copy(path, ostream);
        }
    } else if (ranges != null && ranges.length != 0) {
        // Partial content response.
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
        if (ranges.length == 1) {
            Range range = ranges[0];
            response.addHeader(HttpHeaders.CONTENT_RANGE, range.toString());
            long length = range.end - range.start + 1;
            setContentLengthLong(response, length);
            if (contentType != null) {
                log.debug("serveFile: contentType='{}'", contentType);
                response.setContentType(contentType);
            }
            if (serveContent) {
                try (InputStream stream = Files.newInputStream(path)) {
                    copyRange(stream, ostream, range, new byte[Math.min((int) length, 8192)]);
                }
            }
        } else {
            response.setContentType("multipart/byteranges; boundary=" + MIME_SEPARATION);
            if (serveContent) {
                copy(path, ostream, ranges, contentType, new byte[Math.min((int) contentLength, 8192)]);
            }
        }
    }
}

From source file:org.tinymediamanager.core.entities.MediaFile.java

License:asdf

/**
 * Gathers the media information via the native mediainfo lib.
 * //  w w  w . ja  va 2  s .  com
 * @param force
 *          forces the execution, will not stop on already imported files
 */
public void gatherMediaInformation(boolean force) {
    // check for supported filetype
    if (!isValidMediainfoFormat()) {
        // okay, we have no valid MI file, be sure it will not be triggered any more
        if (StringUtils.isBlank(getContainerFormat())) {
            setContainerFormat(getExtension());
        }
        return;
    }

    // mediainfo already gathered
    if (!force && !getContainerFormat().isEmpty()) {
        return;
    }

    // gather subtitle infos independent of MI
    if (getType() == MediaFileType.SUBTITLE) {
        gatherSubtitleInformation();
    }

    // file size and last modified
    try {
        BasicFileAttributes attrs = Files.readAttributes(getFileAsPath(), BasicFileAttributes.class);
        filedate = attrs.lastModifiedTime().toMillis();
        setFilesize(attrs.size());
    } catch (IOException e) {
        if (miSnapshot == null) { // maybe we set it already (from ISO) so only display message when empty
            LOGGER.warn("could not get file information (size/date): " + e.getMessage());
        }
        // do not set/return here - we might have set it already... and the next check does check for a 0-byte file
        // setContainerFormat(getExtension());
        // return;
    }

    // do not work further on 0 byte files
    if (getFilesize() == 0) {
        LOGGER.warn("0 Byte file detected: " + this.filename);
        // set container format to do not trigger it again
        setContainerFormat(getExtension());
        return;
    }

    // do not work further on subtitles/NFO files
    if (type == MediaFileType.SUBTITLE || type == MediaFileType.NFO) {
        // set container format to do not trigger it again
        setContainerFormat(getExtension());
        return;
    }

    // get media info
    LOGGER.debug("start MediaInfo for " + this.getFileAsPath());
    long discFilesSizes = 0L;
    if (isISO) {
        discFilesSizes = getMediaInfoSnapshotFromISO();
    } else {
        getMediaInfoSnapshot();
    }

    if (miSnapshot == null) {
        // MI could not be opened
        LOGGER.error("error getting MediaInfo for " + this.filename);
        // set container format to do not trigger it again
        setContainerFormat(getExtension());
        closeMediaInfo();
        return;
    }
    LOGGER.trace("got MI");

    String height = "";
    String scanType = "";
    String width = "";
    String videoCodec = "";

    switch (type) {
    case VIDEO:
    case VIDEO_EXTRA:
    case SAMPLE:
    case TRAILER:
        height = getMediaInfo(StreamKind.Video, 0, "Height");
        scanType = getMediaInfo(StreamKind.Video, 0, "ScanType");
        width = getMediaInfo(StreamKind.Video, 0, "Width");
        videoCodec = getMediaInfo(StreamKind.Video, 0, "CodecID/Hint", "Format");

        // fix for Microsoft VC-1
        if (StringUtils.containsIgnoreCase(videoCodec, "Microsoft")) {
            videoCodec = getMediaInfo(StreamKind.Video, 0, "Format");
        }

        // *****************
        // get audio streams
        // *****************
        // int streams = getMediaInfo().streamCount(StreamKind.Audio);
        int streams = 0;
        if (streams == 0) {
            // fallback 1
            String cnt = getMediaInfo(StreamKind.General, 0, "AudioCount");
            try {
                streams = Integer.parseInt(cnt);
            } catch (Exception e) {
                streams = 0;
            }
        }
        if (streams == 0) {
            // fallback 2
            String cnt = getMediaInfo(StreamKind.Audio, 0, "StreamCount");
            try {
                streams = Integer.parseInt(cnt);
            } catch (Exception e) {
                streams = 0;
            }
        }
        audioStreams.clear();
        for (int i = 0; i < streams; i++) {
            MediaFileAudioStream stream = new MediaFileAudioStream();
            String audioCodec = getMediaInfo(StreamKind.Audio, i, "CodecID/Hint", "Format");
            audioCodec = audioCodec.replaceAll("\\p{Punct}", "");

            String audioAddition = getMediaInfo(StreamKind.Audio, i, "Format_Profile");
            if ("dts".equalsIgnoreCase(audioCodec) && StringUtils.isNotBlank(audioAddition)) {
                if (audioAddition.contains("ES")) {
                    audioCodec = "DTSHD-ES";
                }
                if (audioAddition.contains("HRA")) {
                    audioCodec = "DTSHD-HRA";
                }
                if (audioAddition.contains("MA")) {
                    audioCodec = "DTSHD-MA";
                }
            }
            stream.setCodec(audioCodec);

            // AAC sometimes codes channels into Channel(s)_Original
            String channels = getMediaInfo(StreamKind.Audio, i, "Channel(s)_Original", "Channel(s)");
            stream.setChannels(StringUtils.isEmpty(channels) ? "" : channels + "ch");
            try {
                String br = getMediaInfo(StreamKind.Audio, i, "BitRate");
                stream.setBitrate(Integer.parseInt(br) / 1024);
            } catch (Exception ignored) {
            }
            String language = getMediaInfo(StreamKind.Audio, i, "Language/String", "Language");
            if (language.isEmpty()) {
                if (!isDiscFile()) { // video_ts parsed 'ts' as Tsonga
                    // try to parse from filename
                    String shortname = getBasename().toLowerCase(Locale.ROOT);
                    stream.setLanguage(parseLanguageFromString(shortname));
                }
            } else {
                stream.setLanguage(parseLanguageFromString(language));
            }
            audioStreams.add(stream);
        }

        // ********************
        // get subtitle streams
        // ********************
        // streams = getMediaInfo().streamCount(StreamKind.Text);
        streams = 0;
        if (streams == 0) {
            // fallback 1
            String cnt = getMediaInfo(StreamKind.General, 0, "TextCount");
            try {
                streams = Integer.parseInt(cnt);
            } catch (Exception e) {
                streams = 0;
            }
        }
        if (streams == 0) {
            // fallback 2
            String cnt = getMediaInfo(StreamKind.Text, 0, "StreamCount");
            try {
                streams = Integer.parseInt(cnt);
            } catch (Exception e) {
                streams = 0;
            }
        }

        subtitles.clear();
        for (int i = 0; i < streams; i++) {
            MediaFileSubtitle stream = new MediaFileSubtitle();

            String codec = getMediaInfo(StreamKind.Text, i, "CodecID/Hint", "Format");
            stream.setCodec(codec.replaceAll("\\p{Punct}", ""));
            String lang = getMediaInfo(StreamKind.Text, i, "Language/String", "Language");
            stream.setLanguage(parseLanguageFromString(lang));

            String forced = getMediaInfo(StreamKind.Text, i, "Forced");
            boolean b = forced.equalsIgnoreCase("true") || forced.equalsIgnoreCase("yes");
            stream.setForced(b);

            subtitles.add(stream);
        }

        // detect 3D video (mainly from MKV files)
        // sample Mediainfo output:
        // MultiView_Count : 2
        // MultiView_Layout : Top-Bottom (left eye first)
        // MultiView_Layout : Side by Side (left eye first)
        String mvc = getMediaInfo(StreamKind.Video, 0, "MultiView_Count");
        if (!StringUtils.isEmpty(mvc) && mvc.equals("2")) {
            video3DFormat = VIDEO_3D;
            String mvl = getMediaInfo(StreamKind.Video, 0, "MultiView_Layout").toLowerCase(Locale.ROOT);
            LOGGER.debug("3D detected :) " + mvl);
            if (!StringUtils.isEmpty(mvl) && mvl.contains("top") && mvl.contains("bottom")) {
                video3DFormat = VIDEO_3D_TAB;
            }
            if (!StringUtils.isEmpty(mvl) && mvl.contains("side")) {
                video3DFormat = VIDEO_3D_SBS;
            }
        }

        break;

    case AUDIO:
        MediaFileAudioStream stream = new MediaFileAudioStream();
        String audioCodec = getMediaInfo(StreamKind.Audio, 0, "CodecID/Hint", "Format");
        stream.setCodec(audioCodec.replaceAll("\\p{Punct}", ""));
        String channels = getMediaInfo(StreamKind.Audio, 0, "Channel(s)");
        stream.setChannels(StringUtils.isEmpty(channels) ? "" : channels + "ch");
        try {
            String br = getMediaInfo(StreamKind.Audio, 0, "BitRate");
            stream.setBitrate(Integer.parseInt(br) / 1024);
        } catch (Exception e) {
        }
        String language = getMediaInfo(StreamKind.Audio, 0, "Language/String", "Language");
        if (language.isEmpty()) {
            // try to parse from filename
            String shortname = getBasename().toLowerCase(Locale.ROOT);
            stream.setLanguage(parseLanguageFromString(shortname));
        } else {
            stream.setLanguage(parseLanguageFromString(language));
        }
        audioStreams.clear();
        audioStreams.add(stream);
        break;

    case POSTER:
    case BANNER:
    case FANART:
    case THUMB:
    case EXTRAFANART:
    case GRAPHIC:
    case SEASON_POSTER:
    case LOGO:
    case CLEARLOGO:
    case CLEARART:
    case DISCART:
    case EXTRATHUMB:
        height = getMediaInfo(StreamKind.Image, 0, "Height");
        // scanType = getMediaInfo(StreamKind.Image, 0, "ScanType"); // no scantype on graphics
        width = getMediaInfo(StreamKind.Image, 0, "Width");
        videoCodec = getMediaInfo(StreamKind.Image, 0, "CodecID/Hint", "Format");
        // System.out.println(height + "-" + width + "-" + videoCodec);
        break;

    case NFO: // do nothing here, but do not display default warning (since we got the filedate)
        break;

    default:
        LOGGER.warn("no mediainformation handling for MediaFile type " + getType() + " yet.");
        break;
    }

    // video codec
    // e.g. XviD, x264, DivX 5, MPEG-4 Visual, AVC, etc.
    // get first token (e.g. DivX 5 => DivX)
    setVideoCodec(StringUtils.isEmpty(videoCodec) ? "" : new Scanner(videoCodec).next());

    // container format for all except subtitles (subtitle container format is handled another way)
    if (type == MediaFileType.SUBTITLE) {
        setContainerFormat(getExtension());
    } else {
        String extensions = getMediaInfo(StreamKind.General, 0, "Codec/Extensions", "Format");
        // get first extension
        setContainerFormat(
                StringUtils.isBlank(extensions) ? "" : new Scanner(extensions).next().toLowerCase(Locale.ROOT));

        // if container format is still empty -> insert the extension
        if (StringUtils.isBlank(containerFormat)) {
            setContainerFormat(getExtension());
        }
    }

    if (height.isEmpty() || scanType.isEmpty()) {
        setExactVideoFormat("");
    } else {
        setExactVideoFormat(height + Character.toLowerCase(scanType.charAt(0)));
    }

    // video dimension
    if (!width.isEmpty()) {
        try {
            setVideoWidth(Integer.parseInt(width));
        } catch (NumberFormatException e) {
            setVideoWidth(0);
        }
    }
    if (!height.isEmpty()) {
        try {
            setVideoHeight(Integer.parseInt(height));
        } catch (NumberFormatException e) {
            setVideoHeight(0);
        }
    }

    switch (type) {
    case VIDEO:
    case VIDEO_EXTRA:
    case SAMPLE:
    case TRAILER:
    case AUDIO:
        // overall bitrate (OverallBitRate/String)
        String br = getMediaInfo(StreamKind.General, 0, "OverallBitRate");
        if (!br.isEmpty()) {
            try {
                setOverallBitRate(Integer.parseInt(br) / 1024); // in kbps
            } catch (NumberFormatException e) {
                setOverallBitRate(0);
            }
        }

        // Duration;Play time of the stream in ms
        // Duration/String;Play time in format : XXx YYy only, YYy omited if zero
        // Duration/String1;Play time in format : HHh MMmn SSs MMMms, XX om.if.z.
        // Duration/String2;Play time in format : XXx YYy only, YYy omited if zero
        // Duration/String3;Play time in format : HH:MM:SS.MMM
        if (!isISO) {
            // ISO files get duration accumulated with snapshot
            String dur = getMediaInfo(StreamKind.General, 0, "Duration");
            if (!dur.isEmpty()) {
                try {
                    Double d = Double.parseDouble(dur);
                    setDuration(d.intValue() / 1000);
                } catch (NumberFormatException e) {
                    setDuration(0);
                }
            }
        } else {
            // do some sanity check, to see, if we have an invalid DVD structure
            // eg when the sum(filesize) way higher than ISO size
            LOGGER.trace("ISO size:" + filesize + "  dataSize:" + discFilesSizes + "  = diff:"
                    + Math.abs(discFilesSizes - filesize));
            if (discFilesSizes > 0 && filesize > 0) {
                long gig = 1024 * 1024 * 1024;
                if (Math.abs(discFilesSizes - filesize) > gig) {
                    LOGGER.error("ISO file seems to have an invalid structure - ignore duration");
                    // we set the ISO duration to zero,
                    // so the standard getDuration() will always get the scraped duration
                    setDuration(0);
                }
            }
        }
    default:
        break;
    }

    LOGGER.trace("extracted MI");
    // close mediainfo lib
    closeMediaInfo();
    LOGGER.trace("closed MI");
}

From source file:net.mozq.picto.core.ProcessCore.java

public static void findFiles(ProcessCondition processCondition, Consumer<ProcessData> processDataSetter,
        BooleanSupplier processStopper) throws IOException {

    Set<FileVisitOption> fileVisitOptionSet;
    if (processCondition.isFollowLinks()) {
        fileVisitOptionSet = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    } else {/*from w  w  w  . j  a va 2s .co  m*/
        fileVisitOptionSet = Collections.emptySet();
    }

    Files.walkFileTree(processCondition.getSrcRootPath(), fileVisitOptionSet, processCondition.getDept(),
            new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    if (processStopper.getAsBoolean()) {
                        return FileVisitResult.TERMINATE;
                    }

                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                    if (attrs.isDirectory()) {
                        return FileVisitResult.SKIP_SUBTREE;
                    }

                    if (processStopper.getAsBoolean()) {
                        return FileVisitResult.TERMINATE;
                    }

                    if (!processCondition.getPathFilter().accept(file, attrs)) {
                        return FileVisitResult.SKIP_SUBTREE;
                    }

                    Path rootRelativeSubPath = processCondition.getSrcRootPath().relativize(file.getParent());

                    ImageMetadata imageMetadata = getImageMetadata(file);

                    Date baseDate;
                    if (processCondition.isChangeFileCreationDate()
                            || processCondition.isChangeFileModifiedDate()
                            || processCondition.isChangeFileAccessDate()
                            || processCondition.isChangeExifDate()) {
                        baseDate = getBaseDate(processCondition, file, attrs, imageMetadata);
                    } else {
                        baseDate = null;
                    }

                    String destSubPathname = processCondition.getDestSubPathFormat().format(varName -> {
                        try {
                            switch (varName) {
                            case "Now":
                                return new Date();
                            case "ParentSubPath":
                                return rootRelativeSubPath.toString();
                            case "FileName":
                                return file.getFileName().toString();
                            case "BaseName":
                                return FileUtilz.getBaseName(file.getFileName().toString());
                            case "Extension":
                                return FileUtilz.getExt(file.getFileName().toString());
                            case "Size":
                                return Long.valueOf(Files.size(file));
                            case "CreationDate":
                                return (processCondition.isChangeFileCreationDate()) ? baseDate
                                        : new Date(attrs.creationTime().toMillis());
                            case "ModifiedDate":
                                return (processCondition.isChangeFileModifiedDate()) ? baseDate
                                        : new Date(attrs.lastModifiedTime().toMillis());
                            case "AccessDate":
                                return (processCondition.isChangeFileAccessDate()) ? baseDate
                                        : new Date(attrs.lastAccessTime().toMillis());
                            case "PhotoTakenDate":
                                return (processCondition.isChangeExifDate()) ? baseDate
                                        : getPhotoTakenDate(file, imageMetadata);
                            case "Width":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXIF_IMAGE_WIDTH);
                            case "Height":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXIF_IMAGE_LENGTH);
                            case "FNumber":
                                return getEXIFDoubleValue(imageMetadata, ExifTagConstants.EXIF_TAG_FNUMBER);
                            case "Aperture":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
                            case "MaxAperture":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_MAX_APERTURE_VALUE);
                            case "ISO":
                                return getEXIFIntValue(imageMetadata, ExifTagConstants.EXIF_TAG_ISO);
                            case "FocalLength":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_FOCAL_LENGTH); // ?
                            case "FocalLength35mm":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_FOCAL_LENGTH_IN_35MM_FORMAT);
                            case "ShutterSpeed":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
                            case "Exposure":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_EXPOSURE); // 
                            case "ExposureTime":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXPOSURE_TIME); // 
                            case "ExposureMode":
                                return getEXIFIntValue(imageMetadata, ExifTagConstants.EXIF_TAG_EXPOSURE_MODE);
                            case "ExposureProgram":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_EXPOSURE_PROGRAM);
                            case "Brightness":
                                return getEXIFDoubleValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE);
                            case "WhiteBalance":
                                return getEXIFIntValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_WHITE_BALANCE_1);
                            case "LightSource":
                                return getEXIFIntValue(imageMetadata, ExifTagConstants.EXIF_TAG_LIGHT_SOURCE);
                            case "Lens":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_LENS);
                            case "LensMake":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_LENS_MAKE);
                            case "LensModel":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_LENS_MODEL);
                            case "LensSerialNumber":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_LENS_SERIAL_NUMBER);
                            case "Make":
                                return getEXIFStringValue(imageMetadata, TiffTagConstants.TIFF_TAG_MAKE);
                            case "Model":
                                return getEXIFStringValue(imageMetadata, TiffTagConstants.TIFF_TAG_MODEL);
                            case "SerialNumber":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_SERIAL_NUMBER);
                            case "Software":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_SOFTWARE);
                            case "ProcessingSoftware":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_PROCESSING_SOFTWARE);
                            case "OwnerName":
                                return getEXIFStringValue(imageMetadata, ExifTagConstants.EXIF_TAG_OWNER_NAME);
                            case "CameraOwnerName":
                                return getEXIFStringValue(imageMetadata,
                                        ExifTagConstants.EXIF_TAG_CAMERA_OWNER_NAME);
                            case "GPSLat":
                                return getEXIFGpsLat(imageMetadata);
                            case "GPSLatDeg":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE,
                                        0);
                            case "GPSLatMin":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE,
                                        1);
                            case "GPSLatSec":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE,
                                        2);
                            case "GPSLatRef":
                                return getEXIFStringValue(imageMetadata,
                                        GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
                            case "GPSLon":
                                return getEXIFGpsLon(imageMetadata);
                            case "GPSLonDeg":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE,
                                        0);
                            case "GPSLonMin":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE,
                                        1);
                            case "GPSLonSec":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE,
                                        2);
                            case "GPSLonRef":
                                return getEXIFStringValue(imageMetadata,
                                        GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
                            case "GPSAlt":
                                return getEXIFDoubleValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_ALTITUDE);
                            case "GPSAltRef":
                                return getEXIFIntValue(imageMetadata, GpsTagConstants.GPS_TAG_GPS_ALTITUDE_REF);
                            default:
                                throw new PictoInvalidDestinationPathException(Messages
                                        .getString("message.warn.invalid.destSubPath.varName", varName));
                            }
                        } catch (PictoException e) {
                            throw e;
                        } catch (Exception e) {
                            throw new PictoInvalidDestinationPathException(
                                    Messages.getString("message.warn.invalid.destSubPath.pattern"), e);
                        }
                    });

                    Path destSubPath = processCondition.getDestRootPath().resolve(destSubPathname).normalize();

                    if (!destSubPath.startsWith(processCondition.getDestRootPath())) {
                        throw new PictoInvalidDestinationPathException(
                                Messages.getString("message.warn.invalid.destination.path", destSubPath));
                    }

                    ProcessData processData = new ProcessData();
                    processData.setSrcPath(file);
                    processData.setSrcFileAttributes(attrs);
                    processData.setDestPath(destSubPath);
                    processData.setBaseDate(baseDate);

                    processDataSetter.accept(processData);

                    return FileVisitResult.CONTINUE;
                }
            });
}

From source file:gov.noaa.pfel.coastwatch.util.FileVisitorDNLS.java

/** Invoked before entering a directory. */
public FileVisitResult preVisitDirectory(Path tDir, BasicFileAttributes attrs) throws IOException {

    String ttDir = String2.replaceAll(tDir.toString(), fromSlash, toSlash) + toSlash;
    if (ttDir.equals(dir)) {
        if (debugMode)
            String2.log(">> initial dir");
        return FileVisitResult.CONTINUE;
    }/*from   ww w. j a  v  a2  s  . c  o  m*/

    //skip because it doesn't match pathRegex?
    if (pathPattern != null && !pathPattern.matcher(ttDir).matches()) {
        if (debugMode)
            String2.log(">> doesn't match pathRegex: " + ttDir + " regex=" + pathRegex);
        return FileVisitResult.SKIP_SUBTREE;
    }

    if (directoriesToo) {
        directoryPA.add(ttDir);
        namePA.add("");
        lastModifiedPA.add(attrs.lastModifiedTime().toMillis());
        sizePA.add(0);
    }

    if (debugMode)
        String2.log(">> recursive=" + recursive + " dir=" + ttDir);
    return recursive ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
}