001/*
002 * (C) Copyright 2015-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.Set;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.blob.binary.BinaryManagerStatus;
028import org.nuxeo.ecm.core.model.Document;
029
030/**
031 * Service managing {@link Blob}s associated to a {@link Document} or a repository.
032 *
033 * @since 9.2
034 */
035public interface DocumentBlobManager {
036
037    /**
038     * Reads a {@link Blob} from storage.
039     *
040     * @param blobInfo the blob information
041     * @param repositoryName the repository to which this blob belongs
042     * @return a managed blob
043     * @since 7.2
044     */
045    Blob readBlob(BlobInfo blobInfo, String repositoryName) throws IOException;
046
047    /**
048     * Writes a {@link Blob} to storage and returns its key.
049     *
050     * @param blob the blob
051     * @param doc the document to which this blob belongs
052     * @param xpath the xpath of blob in doc
053     * @return the blob key
054     * @since 9.1
055     */
056    String writeBlob(Blob blob, Document doc, String xpath) throws IOException;
057
058    /**
059     * Gets an {@link InputStream} for a conversion to the given MIME type.
060     * <p>
061     * Like all {@link InputStream}, the result must be closed when done with it to avoid resource leaks.
062     *
063     * @param blob the blob
064     * @param mimeType the MIME type to convert to
065     * @param doc the document that holds the blob
066     * @return the stream, or {@code null} if no conversion is available for the given MIME type
067     * @since 7.2
068     */
069    InputStream getConvertedStream(Blob blob, String mimeType, DocumentModel doc) throws IOException;
070
071    /**
072     * Freezes the blobs' versions on a document version when it is created via a check in.
073     *
074     * @param doc the new document version
075     * @since 7.3
076     */
077    void freezeVersion(Document doc);
078
079    /**
080     * Notifies the blob manager that a set of xpaths have changed on a document.
081     *
082     * @param doc the document
083     * @param xpaths the set of changed xpaths
084     * @since 7.3
085     */
086    void notifyChanges(Document doc, Set<String> xpaths);
087
088    /**
089     * Garbage collect the unused binaries.
090     *
091     * @param delete if {@code false} don't actually delete the garbage collected binaries (but still return statistics
092     *            about them), if {@code true} delete them
093     * @return a status about the number of garbage collected binaries
094     * @since 7.4
095     */
096    BinaryManagerStatus garbageCollectBinaries(boolean delete);
097
098    /**
099     * Checks if a garbage collection of the binaries in progress.
100     *
101     * @return {@code true} if a garbage collection of the binaries is in progress
102     * @since 7.4
103     */
104    boolean isBinariesGarbageCollectionInProgress();
105
106    /**
107     * INTERNAL. Marks a binary as referenced during garbage collection. Called back by repository implementations
108     * during {@link #garbageCollectBinaries}.
109     *
110     * @param key the binary key
111     * @param repositoryName the repository name
112     * @since 7.4
113     */
114    void markReferencedBinary(String key, String repositoryName);
115
116}