001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.filemanager.utils;
023
024import java.io.File;
025import java.io.FileInputStream;
026import java.io.IOException;
027import java.io.InputStream;
028import java.net.MalformedURLException;
029import java.net.URL;
030import java.text.Normalizer;
031
032import org.apache.commons.io.IOUtils;
033import org.nuxeo.common.utils.IdUtils;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.LifeCycleConstants;
038import org.nuxeo.ecm.core.api.PathRef;
039
040public final class FileManagerUtils {
041
042    // This is an utility class
043    private FileManagerUtils() {
044    }
045
046    /**
047     * Returns the contents of the file in a byte array.
048     *
049     * @deprecated since 7.2, use {@link IOUtils#toByteArray} instead
050     */
051    @Deprecated
052    public static byte[] getBytesFromFile(File file) throws IOException {
053        try (InputStream in = new FileInputStream(file)) {
054            return IOUtils.toByteArray(in);
055        }
056    }
057
058    /**
059     * Returns the fileName of a file.
060     */
061    public static String fetchFileName(File file) throws MalformedURLException {
062        // Fetching filename
063        URL pathUrl = file.toURI().toURL();
064        String[] pathArray = pathUrl.getFile().split("/");
065        return pathArray[pathArray.length - 1];
066    }
067
068    /**
069     * Returns the fileName of an uploaded file.
070     *
071     * @param fullName the full name that we need to parse
072     * @return the FileName String
073     */
074    // FIXME: badly named method
075    // FIXME: doesn't work in some corner cases, for instance a Unix filename
076    // with a \, or a DOS file with a /
077    public static String fetchFileName(String fullName) {
078        // Fetching filename
079        String ret = fullName;
080        int lastWinSeparator = fullName.lastIndexOf('\\');
081        int lastUnixSeparator = fullName.lastIndexOf('/');
082        int lastSeparator = Math.max(lastWinSeparator, lastUnixSeparator);
083        if (lastSeparator != -1) {
084            ret = fullName.substring(lastSeparator + 1, fullName.length());
085        }
086        return ret;
087    }
088
089    /**
090     * Returns the title.
091     *
092     * @param filename the file name
093     * @return the title
094     */
095    public static String fetchTitle(String filename) {
096        String title = filename.trim();
097        if (title.length() == 0) {
098            title = IdUtils.generateStringId();
099        }
100        return title;
101    }
102
103    /**
104     * Looks if an existing Document with the same filename exists.
105     */
106    public static DocumentModel getExistingDocByFileName(CoreSession documentManager, String path, String filename)
107            {
108        // We must use the "file:content/name" sub-property which is the only
109        // one on which we can rely (and not "file:filename" which can possibly
110        // not be set), see https://jira.nuxeo.com/browse/NXP-10565
111        // Note that the "file:content" property is handled in a particular way
112        // by NXQL, so we must use "content/name" instead of
113        // "file:content/name".
114        return getExistingDocByPropertyName(documentManager, path, filename, "content/name");
115    }
116
117    /**
118     * Looks if an existing Document with the same title exists.
119     */
120    public static DocumentModel getExistingDocByTitle(CoreSession documentManager, String path, String title)
121            {
122        return getExistingDocByPropertyName(documentManager, path, title, "dc:title");
123    }
124
125    /**
126     * Looks if an existing Document has the same value for a given property.
127     */
128    public static DocumentModel getExistingDocByPropertyName(CoreSession documentManager, String path, String value,
129            String propertyName) {
130        value = Normalizer.normalize(value, Normalizer.Form.NFC);
131        DocumentModel existing = null;
132        String parentId = documentManager.getDocument(new PathRef(path)).getId();
133        String query = "SELECT * FROM Document WHERE ecm:parentId = '" + parentId + "' AND " + propertyName + " = '"
134                + value.replace("'", "\\\'") + "' AND ecm:currentLifeCycleState != '"
135                + LifeCycleConstants.DELETED_STATE + "'";
136        DocumentModelList docs = documentManager.query(query, 1);
137        if (docs.size() > 0) {
138            existing = docs.get(0);
139        }
140        return existing;
141    }
142
143}