001/*
002 * (C) Copyright 2019 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.filemanager.api;
021
022import org.apache.commons.lang3.StringUtils;
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.CoreSession;
025
026/**
027 * Class containing everything needed to create or update a document from a {@code Blob} with the {@link FileManager}.
028 *
029 * @since 10.10
030 */
031public class FileImporterContext {
032
033    protected final CoreSession session;
034
035    protected final Blob blob;
036
037    protected final String parentPath;
038
039    protected String fileName;
040
041    protected boolean overwrite;
042
043    protected boolean mimeTypeCheck;
044
045    protected boolean excludeOneToMany;
046
047    protected boolean persistDocument;
048
049    protected boolean bypassAllowedSubtypeCheck;
050
051    public static Builder builder(CoreSession session, Blob blob, String parentPath) {
052        return new Builder(session, blob, parentPath);
053    }
054
055    protected FileImporterContext(Builder builder) {
056        session = builder.session;
057        blob = builder.blob;
058        parentPath = builder.parentPath;
059        overwrite = builder.overwrite;
060        mimeTypeCheck = builder.mimeTypeCheck;
061        excludeOneToMany = builder.excludeOneToMany;
062        fileName = StringUtils.defaultIfEmpty(builder.fileName, blob.getFilename());
063        persistDocument = builder.persistDocument;
064        bypassAllowedSubtypeCheck = builder.bypassAllowedSubtypeCheck;
065    }
066
067    public CoreSession getSession() {
068        return session;
069    }
070
071    public Blob getBlob() {
072        return blob;
073    }
074
075    public String getParentPath() {
076        return parentPath;
077    }
078
079    public String getFileName() {
080        return fileName;
081    }
082
083    public boolean isOverwrite() {
084        return overwrite;
085    }
086
087    public boolean isMimeTypeCheck() {
088        return mimeTypeCheck;
089    }
090
091    public boolean isExcludeOneToMany() {
092        return excludeOneToMany;
093    }
094
095    public boolean isPersistDocument() {
096        return persistDocument;
097    }
098
099    /**
100     * @since 11.3
101     */
102    public boolean isBypassAllowedSubtypeCheck() {
103        return bypassAllowedSubtypeCheck;
104    }
105
106    public static class Builder {
107
108        protected final CoreSession session;
109
110        protected final Blob blob;
111
112        protected final String parentPath;
113
114        protected String fileName;
115
116        protected boolean overwrite;
117
118        protected boolean mimeTypeCheck = true;
119
120        protected boolean excludeOneToMany;
121
122        protected boolean persistDocument = true;
123
124        protected boolean bypassAllowedSubtypeCheck;
125
126        public Builder(CoreSession session, Blob blob, String parentPath) {
127            this.session = session;
128            this.blob = blob;
129            this.parentPath = parentPath;
130        }
131
132        /**
133         * Overrides the file name from the given {@code blob}.
134         */
135        public Builder fileName(String fileName) {
136            this.fileName = fileName;
137            return this;
138        }
139
140        /**
141         * Whether to overwrite an existing file with the same title.
142         * <p>
143         * Defaults to {@code false}.
144         */
145        public Builder overwrite(boolean overwrite) {
146            this.overwrite = overwrite;
147            return this;
148        }
149
150        /**
151         * Whether to check the blob's mime-type against the file name.
152         * <p>
153         * Defaults to {@code true}.
154         */
155        public Builder mimeTypeCheck(boolean mimeTypeCheck) {
156            this.mimeTypeCheck = mimeTypeCheck;
157            return this;
158        }
159
160        /**
161         * Whether to exclude the importers creating more than one document for the given blob when selecting the
162         * importer.
163         * <p>
164         * Defaults to {@code false}.
165         */
166        public Builder excludeOneToMany(boolean excludeOneToMany) {
167            this.excludeOneToMany = excludeOneToMany;
168            return this;
169        }
170
171        /**
172         * Whether to persist the created or updated document.
173         * <p>
174         * If the document is not persisted, it's the caller's responsibility to persist it.
175         * <p>
176         * Defaults to {@code true}.
177         */
178        public Builder persistDocument(boolean persistDocument) {
179            this.persistDocument = persistDocument;
180            return this;
181        }
182
183        /**
184         * Whether to bypass the allowed subtype check.
185         * <p>
186         * Defaults to {@code false}.
187         * @since 11.3
188         */
189        public Builder bypassAllowedSubtypeCheck(boolean bypassAllowedSubtypeCheck) {
190            this.bypassAllowedSubtypeCheck = bypassAllowedSubtypeCheck;
191            return this;
192        }
193
194        public FileImporterContext build() {
195            return new FileImporterContext(this);
196        }
197    }
198
199}