|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.audio; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.xml.sax.SAXException; |
| 24 | + |
| 25 | +import org.apache.tika.extractor.EmbeddedDocumentExtractor; |
| 26 | +import org.apache.tika.extractor.EmbeddedDocumentUtil; |
| 27 | +import org.apache.tika.io.TikaInputStream; |
| 28 | +import org.apache.tika.metadata.HttpHeaders; |
| 29 | +import org.apache.tika.metadata.Metadata; |
| 30 | +import org.apache.tika.metadata.TikaCoreProperties; |
| 31 | +import org.apache.tika.parser.ParseContext; |
| 32 | +import org.apache.tika.parser.mp3.ID3Tags; |
| 33 | +import org.apache.tika.sax.XHTMLContentHandler; |
| 34 | + |
| 35 | +/** |
| 36 | + * Picks the picture that stands for an audio file among its embedded |
| 37 | + * pictures and sends them all to the embedded document extractor. That |
| 38 | + * picture is emitted as a |
| 39 | + * {@link TikaCoreProperties.EmbeddedResourceType#THUMBNAIL}, like the |
| 40 | + * preview image of the document container formats, so a client can find |
| 41 | + * the representative image of any file the same way; the other pictures |
| 42 | + * are {@link TikaCoreProperties.EmbeddedResourceType#INLINE}. See TIKA-4850. |
| 43 | + */ |
| 44 | +public final class CoverArt { |
| 45 | + |
| 46 | + /** |
| 47 | + * The ID3v2 APIC picture type of the front cover, shared by the FLAC |
| 48 | + * and Vorbis picture blocks. |
| 49 | + */ |
| 50 | + public static final int FRONT_COVER = 3; |
| 51 | + |
| 52 | + /** |
| 53 | + * The ID3v2 APIC picture type "Other". Many taggers store the main |
| 54 | + * cover art with this type instead of marking it a front cover. |
| 55 | + */ |
| 56 | + public static final int OTHER = 0; |
| 57 | + |
| 58 | + private CoverArt() { |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * One embedded picture of an audio file. The type is the ID3v2 APIC |
| 63 | + * picture type, shared by the FLAC and Vorbis picture blocks; mime type |
| 64 | + * and description may be null or empty. |
| 65 | + */ |
| 66 | + public record Picture(int type, String mimeType, String description, byte[] data) { |
| 67 | + |
| 68 | + /** |
| 69 | + * The type as {@link #thumbnailIndex(List)} sees it: a value beyond |
| 70 | + * the ID3 picture type table counts as unknown. |
| 71 | + */ |
| 72 | + int normalizedType() { |
| 73 | + return type >= ID3Tags.PICTURE_TYPES.length ? -1 : type; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the index of the picture to mark as the thumbnail: the first |
| 79 | + * front cover; else the first picture whose type is "Other" or unknown, |
| 80 | + * which is where taggers put the main art when they do not classify it, |
| 81 | + * rather than e.g. a back cover or a leaflet that happens to come first; |
| 82 | + * else the first picture. |
| 83 | + * |
| 84 | + * @param pictureTypes the picture types in file order; a negative value |
| 85 | + * for a picture whose type is unknown |
| 86 | + * @return the index, or -1 if there are no pictures |
| 87 | + */ |
| 88 | + public static int thumbnailIndex(List<Integer> pictureTypes) { |
| 89 | + if (pictureTypes.isEmpty()) { |
| 90 | + return -1; |
| 91 | + } |
| 92 | + int front = pictureTypes.indexOf(FRONT_COVER); |
| 93 | + if (front >= 0) { |
| 94 | + return front; |
| 95 | + } |
| 96 | + for (int i = 0; i < pictureTypes.size(); i++) { |
| 97 | + if (pictureTypes.get(i) <= OTHER) { |
| 98 | + return i; |
| 99 | + } |
| 100 | + } |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * The resource type of the picture at the given index. |
| 106 | + */ |
| 107 | + public static TikaCoreProperties.EmbeddedResourceType resourceType(int index, |
| 108 | + int thumbnailIndex) { |
| 109 | + return index == thumbnailIndex ? TikaCoreProperties.EmbeddedResourceType.THUMBNAIL |
| 110 | + : TikaCoreProperties.EmbeddedResourceType.INLINE; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Sends the pictures of one audio file to the embedded document |
| 115 | + * extractor: the one {@link #thumbnailIndex(List)} picks as the |
| 116 | + * THUMBNAIL, the others as INLINE pictures. The pictures only become |
| 117 | + * embedded documents, no metadata is recorded on the audio document |
| 118 | + * itself. Call once per file, with all of its pictures, so exactly one |
| 119 | + * of them is the thumbnail. |
| 120 | + */ |
| 121 | + public static void extractPictures(List<Picture> pictures, XHTMLContentHandler xhtml, |
| 122 | + ParseContext context) throws IOException, SAXException { |
| 123 | + if (pictures.isEmpty()) { |
| 124 | + return; |
| 125 | + } |
| 126 | + List<Integer> pictureTypes = new ArrayList<>(); |
| 127 | + for (Picture picture : pictures) { |
| 128 | + pictureTypes.add(picture.normalizedType()); |
| 129 | + } |
| 130 | + int thumbnailIndex = thumbnailIndex(pictureTypes); |
| 131 | + EmbeddedDocumentExtractor extractor = |
| 132 | + EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context); |
| 133 | + for (int i = 0; i < pictures.size(); i++) { |
| 134 | + Picture picture = pictures.get(i); |
| 135 | + Metadata pictureMetadata = Metadata.newInstance(context); |
| 136 | + pictureMetadata.set(TikaCoreProperties.EMBEDDED_RESOURCE_TYPE, |
| 137 | + resourceType(i, thumbnailIndex).name()); |
| 138 | + if (picture.mimeType() != null && !picture.mimeType().isEmpty()) { |
| 139 | + pictureMetadata.set(HttpHeaders.CONTENT_TYPE, picture.mimeType()); |
| 140 | + } |
| 141 | + if (picture.description() != null && !picture.description().isEmpty()) { |
| 142 | + pictureMetadata.set(TikaCoreProperties.TITLE, picture.description()); |
| 143 | + } |
| 144 | + if (picture.type() >= 0 && picture.type() < ID3Tags.PICTURE_TYPES.length) { |
| 145 | + pictureMetadata.set(TikaCoreProperties.DESCRIPTION, |
| 146 | + ID3Tags.PICTURE_TYPES[picture.type()]); |
| 147 | + } |
| 148 | + if (extractor.shouldParseEmbedded(pictureMetadata, context)) { |
| 149 | + try (TikaInputStream pictureStream = TikaInputStream.get(picture.data())) { |
| 150 | + extractor.parseEmbedded(pictureStream, xhtml, pictureMetadata, context, true); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments