/** Get a match from a stream of data. */ public static MagicMatch getMagicMatch(final byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** * get a match from a stream of data * * @param data DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** * get a match from a stream of data * * @param data DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** * get a match from a file * * @param file the file to match content in * @param extensionHints whether or not to use extension to optimize order of content tests * * @return the MagicMatch object representing a match in the file * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(File file, boolean extensionHints) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(file, extensionHints, false); }
/** * get a match from a file * * @param file the file to match content in * @param extensionHints whether or not to use extension to optimize order of content tests * * @return the MagicMatch object representing a match in the file * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(File file, boolean extensionHints) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(file, extensionHints, false); }
private MagicMatch getMatch(File inputFile) throws MagicMatchNotFoundException { final Path path = Paths.get(inputFile.getAbsolutePath()); try { byte[] data = Files.readAllBytes(path); return Magic.getMagicMatch(data, true); } catch (MagicException | MagicParseException | IOException e) { throw new IllegalStateException("Unable to detect mimetype of the file", e); } }
private static String perseMimeType(byte[] bytes){ try { MagicMatch match = Magic.getMagicMatch(bytes); String mimeType = match.getMimeType(); return mimeType; } catch (Exception e) { return null; } } }
private String getMimeMagic(Path filePath) { try { final MagicMatch match = Magic.getMagicMatch(filePath.toFile(), true, true); if (match == null) return null; return match.getMimeType(); } catch (MagicParseException | MagicMatchNotFoundException | MagicException e) { return null; } }
private Optional<String> guessExtension(byte[] bytes) { try { // No extension : try to guess it MagicMatch match = Magic.getMagicMatch(bytes); String guessedExtension = match.getExtension(); if (!Strings.isNullOrEmpty(guessedExtension)) { return Optional.of(guessedExtension); } else { return Optional.absent(); } } catch (Exception e) { this.logger.warn("Error while attempting to guess attachment extension", e); return Optional.absent(); } }
/** * DOCUMENT ME! * * @param args DOCUMENT ME! */ public static void main(String[] args) { try { if (args.length == 0) { System.err.println("usage: test <file>"); System.exit(1);; } File f = new File(args[0]); if (f.exists()) { MagicMatch match = Magic.getMagicMatch(f, true, false); System.out.println("filename: " + args[0]); printMagicMatch(System.out, match, ""); } else { System.err.println("file '" + f.getCanonicalPath() + "' not found"); } } catch (MagicMatchNotFoundException e) { System.out.println("no match found"); } catch (Exception e) { System.err.println("error: " + e); e.printStackTrace(System.err); } } }
MagicMatch match = Magic.getMagicMatch(f, true, false);
private String getMimeType(ExternalReferenceSPI externalReference, InvocationContext context) { if (!mimeTypes.containsKey(externalReference)) { InputStream inputStream = externalReference.openStream(context); try { byte[] bytes = new byte[64]; inputStream.read(bytes); mimeTypes.put(externalReference, Magic.getMagicMatch(bytes, true).getMimeType()); } catch (IOException e) { e.printStackTrace(); logger.debug("Failed to read from stream to determine mimetype", e); } catch (MagicParseException e) { e.printStackTrace(); logger.debug("Error calling mime magic", e); } catch (MagicMatchNotFoundException e) { e.printStackTrace(); logger.debug("Error calling mime magic", e); } catch (MagicException e) { e.printStackTrace(); logger.debug("Error calling mime magic", e); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); logger.debug("Failed to close stream after determining mimetype", e); } } } return mimeTypes.get(externalReference); }
bytes = new byte[64]; inputStream.read(bytes); magicMatch = Magic.getMagicMatch(bytes, true); } catch (IOException e) { e.printStackTrace();
bytes = new byte[64]; inputStream.read(bytes); mimeType = Magic.getMagicMatch(bytes, true).getMimeType(); } catch (IOException e) { e.printStackTrace();
fileType = FileType.SEQ; } else { final String mimeType = Magic.getMagicMatch(header, false).getMimeType(); log.info("File mime Type {}", mimeType); if (FileType.TEXT.getValue().equalsIgnoreCase(mimeType)) {
magicMatch = Magic.getMagicMatch(byteArray); mimeType = magicMatch.getMimeType(); } catch (MagicParseException e1) {
@Test public void getPhotoInputStream() throws IOException, G3GalleryException, MagicParseException, MagicMatchNotFoundException, MagicException { try { itemClient.getItem(createdPhotoId); }catch(G3ItemNotFoundException ex){ //item does not exist yet, so we need to add it first addPhoto(); } Item item1 = itemClient.getItem(createdPhotoId); String url = item1.getEntity().getFileUrl(); InputStream inputStream = itemClient.getPhotoInputStream(url); //conversion stuff.. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteStreams.copy(inputStream, outputStream); byte[] photoDownloadedAsAByteArray = outputStream.toByteArray(); // getMagicMatch accepts byte[], String contentType = Magic.getMagicMatch(photoDownloadedAsAByteArray).getMimeType(); // it should be an image file assertEquals("image/png", contentType); }