Search in sources :

Example 1 with SoftExecutor

use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.

the class IMSoftProvider method createSoftExecutor.

/**
 * @see org.fagu.fmv.soft.find.SoftProvider#createSoftExecutor(org.fagu.fmv.soft.Soft, java.io.File, java.util.List)
 */
@Override
public SoftExecutor createSoftExecutor(Soft soft, File execFile, List<String> parameters) {
    if (SOFT_MAGICK_NAME.equalsIgnoreCase(FilenameUtils.getBaseName(soft.getFile().getName()))) {
        SoftInfo softInfo = soft.getFirstInfo();
        if (softInfo instanceof VersionSoftInfo) {
            VersionSoftInfo versionSoftInfo = (VersionSoftInfo) softInfo;
            Optional<Version> version = versionSoftInfo.getVersion();
            if (version.isPresent() && version.get().isUpperOrEqualsThan(V7)) {
                List<String> newParams = new ArrayList<>(parameters);
                newParams.add(0, getName());
                return new SoftExecutor(this, execFile, newParams);
            }
        }
    }
    return super.createSoftExecutor(soft, execFile, parameters);
}
Also used : VersionSoftInfo(org.fagu.fmv.soft.find.info.VersionSoftInfo) SoftInfo(org.fagu.fmv.soft.find.SoftInfo) VersionSoftInfo(org.fagu.fmv.soft.find.info.VersionSoftInfo) VersionSoftPolicy.minVersion(org.fagu.fmv.soft.find.policy.VersionSoftPolicy.minVersion) Version(org.fagu.version.Version) ArrayList(java.util.ArrayList) SoftExecutor(org.fagu.fmv.soft.SoftExecutor)

Example 2 with SoftExecutor

use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.

the class GSExceptionKnowAnalyzeTestCase method testPDFToImage.

/**
 * @throws IOException
 */
@Test
@Ignore
public void testPDFToImage() throws IOException {
    StringJoiner joiner = new StringJoiner(";");
    List<Consumer<SoftExecutor>> consumers = Arrays.asList(null, se -> se.ifExceptionIsKnownDo(ek -> ek.onMessage(joiner::add).doThrow()));
    // }));
    for (Consumer<SoftExecutor> consumer : consumers) {
        runPdfToImage(null, "Permission denied", consumer);
        runPdfToImage("cheese.zip", "Undefined format", consumer);
        runPdfToImage("mp4.mp4", "Undefined format", consumer);
    }
    assertEquals("Permission denied;Undefined format;Undefined format", joiner.toString());
}
Also used : Resource(org.fagu.fmv.soft.Resource) Soft(org.fagu.fmv.soft.Soft) Arrays(java.util.Arrays) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) File(java.io.File) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) Ignore(org.junit.Ignore) StringJoiner(java.util.StringJoiner) Assert.fail(org.junit.Assert.fail) NestedException(org.fagu.fmv.soft.exec.exception.NestedException) SoftExecutor(org.fagu.fmv.soft.SoftExecutor) Assert.assertEquals(org.junit.Assert.assertEquals) Consumer(java.util.function.Consumer) SoftExecutor(org.fagu.fmv.soft.SoftExecutor) StringJoiner(java.util.StringJoiner) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with SoftExecutor

use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.

the class GSExceptionKnowAnalyzeTestCase method runPdfToImage.

// *************************************
/**
 * @param srcResource
 * @param expectedMessage
 * @param consumer
 * @throws IOException
 */
private void runPdfToImage(String srcResource, String expectedMessage, Consumer<SoftExecutor> consumer) throws IOException {
    File folder = new File(System.getProperty("java.io.tmpdir"), "gs-pdf2img-test");
    try {
        FileUtils.deleteDirectory(folder);
        folder.mkdirs();
        File srcFile = srcResource != null ? Resource.extract(folder, srcResource) : folder;
        File outFile = new File(srcFile.getPath() + ".jpg");
        try {
            Soft gsSoft = GS.search();
            List<String> list = new ArrayList<>();
            list.add("-sDEVICE=png16m");
            list.add("-sOutputFile=" + outFile.getPath());
            list.add("-r200");
            list.add("-dNOPAUSE");
            list.add("-dBATCH");
            list.add("-dSAFER");
            list.add("-dFirstPage=1");
            list.add("-dLastPage=1");
            list.add("-dUseCropBox");
            list.add(srcFile.getAbsolutePath());
            SoftExecutor customizeExecutor = gsSoft.withParameters(list).workingDirectory(srcFile.getParentFile());
            // .logCommandLine(System.out::println);
            if (consumer != null) {
                consumer.accept(customizeExecutor);
            }
            customizeExecutor.execute();
            fail();
        } catch (FMVExecuteException e) {
            if (e.isKnown()) {
                assertEquals(expectedMessage, e.getExceptionKnown().toString());
            } else {
                new NestedException(e).messageToLines().forEach(System.out::println);
                throw e;
            }
        }
    } finally {
        FileUtils.deleteDirectory(folder);
    }
}
Also used : FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) ArrayList(java.util.ArrayList) SoftExecutor(org.fagu.fmv.soft.SoftExecutor) NestedException(org.fagu.fmv.soft.exec.exception.NestedException) File(java.io.File) Soft(org.fagu.fmv.soft.Soft)

Example 4 with SoftExecutor

use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.

the class PdfSoftProvider method createSoftExecutor.

/**
 * @see org.fagu.fmv.soft.find.SoftProvider#createSoftExecutor(org.fagu.fmv.soft.Soft, java.io.File, java.util.List)
 */
@Override
public SoftExecutor createSoftExecutor(Soft soft, File execFile, List<String> parameters) {
    XPdfVersionSoftInfo softInfo = (XPdfVersionSoftInfo) soft.getFirstInfo();
    // On Windows & DOS, file.encoding=cp1252 -> do nothing
    if (softInfo != null && Provider.XPDF.equals(softInfo.getProvider()) && SystemUtils.IS_OS_WINDOWS && "UTF-8".equals(System.getProperty("file.encoding"))) {
        List<String> newParams = new ArrayList<>(parameters);
        if (!parameters.contains("-enc")) {
            newParams.add(0, "-enc");
            newParams.add(1, "UTF-8");
        }
        SoftExecutor softExecutor = new SoftExecutor(this, execFile, newParams);
        softExecutor.charset(StandardCharsets.UTF_8);
        // });
        return softExecutor;
    }
    return super.createSoftExecutor(soft, execFile, parameters);
}
Also used : ArrayList(java.util.ArrayList) SoftExecutor(org.fagu.fmv.soft.SoftExecutor)

Example 5 with SoftExecutor

use of org.fagu.fmv.soft.SoftExecutor in project fmv by f-agu.

the class ImageMetadatas method extract.

// *****************************************
/**
 * @param identifySoft
 * @param sourceFiles
 * @param logger
 * @param customizeExecutor
 * @return
 * @throws IOException
 */
private static Map<File, ImageMetadatas> extract(Soft identifySoft, Collection<File> sourceFiles, Consumer<String> logger, Consumer<SoftExecutor> customizeExecutor) throws IOException {
    Objects.requireNonNull(identifySoft);
    if (sourceFiles.isEmpty()) {
        return Collections.emptyMap();
    }
    final String boundary = "BOUNDARY";
    // prepare
    IMOperation op = new IMOperation();
    StringJoiner joiner = new StringJoiner("\n");
    joiner.add("==%f==");
    joiner.add("%[exif:*]%[date:*]%[xap:*]xy=%x %y");
    joiner.add("colorspace=%[colorspace]");
    joiner.add("wh=%w %h");
    joiner.add("cdepth=%z");
    joiner.add("compression=%C");
    joiner.add("compressionq=%Q");
    joiner.add("resunit=%U");
    joiner.add(boundary);
    op.ping().format(joiner.toString() + "\n");
    int size = sourceFiles.size();
    sourceFiles.forEach(file -> op.image(file, "[0]"));
    List<String> outputs = new ArrayList<>();
    SoftExecutor softExecutor = // 
    identifySoft.withParameters(op.toList()).addOutReadLine(// 
    outputs::add).logCommandLine(logger);
    if (customizeExecutor != null) {
        customizeExecutor.accept(softExecutor);
    }
    softExecutor.execute();
    // parse output
    Map<File, ImageMetadatas> outMap = new LinkedHashMap<>(size);
    Iterator<File> srcFileIterator = sourceFiles.iterator();
    TreeMap<String, String> params = null;
    File currentFile = null;
    for (String line : outputs) {
        if (StringUtils.isBlank(line) || boundary.equals(line)) {
            continue;
        }
        if ((line.startsWith("==") || line.startsWith(boundary + "==")) && line.endsWith("==")) {
            if (currentFile != null) {
                outMap.put(currentFile, new ImageMetadatas(params));
            }
            currentFile = srcFileIterator.next();
            if (StringUtils.substringBetween(line, "==").equals(currentFile.getName())) {
                params = new TreeMap<>();
                params.put("filename", currentFile.getName());
                continue;
            } else {
                params = null;
                currentFile = null;
            }
        }
        if (params == null) {
            continue;
        }
        int index = line.indexOf('=');
        String key = line.substring(0, index);
        String value = line.substring(index + 1);
        params.put(key.toLowerCase(), value);
    }
    if (currentFile != null) {
        outMap.put(currentFile, new ImageMetadatas(params));
    }
    return outMap;
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SoftExecutor(org.fagu.fmv.soft.SoftExecutor) File(java.io.File) StringJoiner(java.util.StringJoiner)

Aggregations

ArrayList (java.util.ArrayList)5 SoftExecutor (org.fagu.fmv.soft.SoftExecutor)5 File (java.io.File)3 StringJoiner (java.util.StringJoiner)2 Soft (org.fagu.fmv.soft.Soft)2 FMVExecuteException (org.fagu.fmv.soft.exec.exception.FMVExecuteException)2 NestedException (org.fagu.fmv.soft.exec.exception.NestedException)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 FileUtils (org.apache.commons.io.FileUtils)1 Resource (org.fagu.fmv.soft.Resource)1 SoftInfo (org.fagu.fmv.soft.find.SoftInfo)1 VersionSoftInfo (org.fagu.fmv.soft.find.info.VersionSoftInfo)1 VersionSoftPolicy.minVersion (org.fagu.fmv.soft.find.policy.VersionSoftPolicy.minVersion)1 Version (org.fagu.version.Version)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.fail (org.junit.Assert.fail)1