|
| 1 | +/* |
| 2 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
| 3 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template |
| 4 | + */ |
| 5 | +package edu.kit.datamanager.mappingservice.plugins.impl; |
| 6 | + |
| 7 | +import edu.kit.datamanager.mappingservice.plugins.IMappingPlugin; |
| 8 | +import edu.kit.datamanager.mappingservice.plugins.MappingPluginException; |
| 9 | +import edu.kit.datamanager.mappingservice.plugins.MappingPluginState; |
| 10 | +import edu.kit.datamanager.mappingservice.util.ShellRunnerUtil; |
| 11 | +import java.io.FileNotFoundException; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | +import org.springframework.util.MimeType; |
| 19 | + |
| 20 | +/** |
| 21 | + * Simple mapping service plugin calling image magick 'identify' to obtain image |
| 22 | + * metadata from a given image. |
| 23 | + * |
| 24 | + * @author jejkal |
| 25 | + */ |
| 26 | +public class IdentifyPlugin implements IMappingPlugin { |
| 27 | + |
| 28 | + static Logger LOG = LoggerFactory.getLogger(IdentifyPlugin.class); |
| 29 | + |
| 30 | + private boolean initialized = false; |
| 31 | + |
| 32 | + @Override |
| 33 | + public String name() { |
| 34 | + return "Identify"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String description() { |
| 39 | + return "Simple mapping service plugin calling image magick 'identify' to obtain image metadata from a given image."; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String version() { |
| 44 | + return "1.0.0"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String uri() { |
| 49 | + return "https://github.com/kit-data-manager/mapping-service"; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public MimeType[] inputTypes() { |
| 54 | + return new MimeType[]{MimeType.valueOf("application/octet-stream")}; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public MimeType[] outputTypes() { |
| 59 | + return new MimeType[]{MimeType.valueOf("application/octet-stream")}; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void setup() { |
| 64 | + if (Paths.get("/usr/bin/identify").toFile().exists()) { |
| 65 | + initialized = true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public MappingPluginState mapFile(Path mappingFile, Path inputFile, Path outputFile) throws MappingPluginException { |
| 71 | + try { |
| 72 | + FileOutputStream fout = new FileOutputStream(outputFile.toFile()); |
| 73 | + ShellRunnerUtil.run(fout, fout, "/usr/bin/identify", "-verbose", inputFile.toAbsolutePath().toString()); |
| 74 | + fout.flush(); |
| 75 | + fout.close(); |
| 76 | + } catch (IOException ex) { |
| 77 | + LOG.error("Failed to execute plugin.", ex); |
| 78 | + return MappingPluginState.EXECUTION_ERROR; |
| 79 | + } |
| 80 | + return MappingPluginState.SUCCESS; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments