<commit_before>/*
 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
 */

package org.nuxeo.ecm.platform.filemanager.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.PathRef;

public final class FileManagerUtils {

    private static final Log log = LogFactory.getLog(FileManagerUtils.class);

    // This is an utility class
    private FileManagerUtils() {
    }

    /**
     * Returns the contents of the file in a byte array.
     *
     * @param file
     * @return the byte array
     * @throws IOException
     */
    public static byte[] getBytesFromFile(File file) throws IOException {

        FileInputStream is = new FileInputStream(file);

        long length = file.length();
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];
        try {
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                    && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }

            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read InputStream");
            }
        } finally {
            // Close the input stream and return bytes
            is.close();
        }
        return bytes;
    }

    /**
     * Returns the fileName of a file.
     *
     * @param file
     * @return the FileName String
     */
    public static String fetchFileName(File file) throws MalformedURLException {
        // Fetching filename
        URL pathUrl = file.toURL();
        String[] pathArray = pathUrl.getFile().split("/");
        return pathArray[pathArray.length - 1];
    }

    /**
     * Returns the fileName of an uploadedfile.
     *
     * @param fullName the full name that we need to parse
     * @return the FileName String
     */
    public static String fetchFileName(String fullName) {
        // Fetching filename
        String ret = fullName;
        int lastWinSeparator = fullName.lastIndexOf("\\");
        int lastUnixSeparator = fullName.lastIndexOf("/");
        int lastSeparator = Math.max(lastWinSeparator, lastUnixSeparator);
        if (lastSeparator != -1) {
            ret = fullName.substring(lastSeparator + 1, fullName.length());
        }
        return ret;
    }

    /**
     * Returns the title.
     *
     * @param filename the file name
     * @return the title
     */
    public static String fetchTitle(String filename) {
        if (filename.startsWith(".")) {
            return filename;
        }
        // Fetching title
        int dot = filename.lastIndexOf(".");
        String title = filename;
        if (dot != -1) {
            title = filename.substring(0, filename.lastIndexOf("."));
        }
        return title;
    }

    /**
     * Looking if an existing Document with the same filename exists.
     *
     * @param documentManager
     * @param path
     * @param filename
     */
    public static DocumentModel getExistingDocByFileName(
            CoreSession documentManager, String path, String filename)
            throws ClientException {
        DocumentModel existing = null;
        DocumentRef pathRef = new PathRef(path);
        DocumentModelList docList = documentManager.getChildren(pathRef);
        for (DocumentModel doc : docList) {
            String existFileName = (String) doc.getProperty("file", "filename");
            if (existFileName != null && existFileName.equals(filename)) {
                existing = doc;
                break;
            }
        }
        return existing;
    }

    /**
     * Looking if an existing Document with the same title exists.
     *
     * @param documentManager
     * @param path
     * @param title
     */
    public static DocumentModel getExistingDocByTitle(
            CoreSession documentManager, String path, String title)
            throws ClientException {
        DocumentModel existing = null;
        DocumentRef pathRef = new PathRef(path);
        DocumentModelList docList = documentManager.getChildren(pathRef);
        for (DocumentModel doc : docList) {
            String existTitle = (String) doc.getProperty("dublincore", "title");
            if (existTitle != null && existTitle.equals(title)) {
                existing = doc;
                break;
            }
        }
        return existing;
    }

}<commit_msg>NXP-2483 Drag&drop from desktop shouldn't check the trash for similar titles<commit_after>/*
 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
 */

package org.nuxeo.ecm.platform.filemanager.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.PathRef;

public final class FileManagerUtils {

    private static final Log log = LogFactory.getLog(FileManagerUtils.class);

    // This is an utility class
    private FileManagerUtils() {
    }

    /**
     * Returns the contents of the file in a byte array.
     *
     * @param file
     * @return the byte array
     * @throws IOException
     */
    public static byte[] getBytesFromFile(File file) throws IOException {

        FileInputStream is = new FileInputStream(file);

        long length = file.length();
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];
        try {
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                    && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }

            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read InputStream");
            }
        } finally {
            // Close the input stream and return bytes
            is.close();
        }
        return bytes;
    }

    /**
     * Returns the fileName of a file.
     *
     * @param file
     * @return the FileName String
     */
    public static String fetchFileName(File file) throws MalformedURLException {
        // Fetching filename
        URL pathUrl = file.toURL();
        String[] pathArray = pathUrl.getFile().split("/");
        return pathArray[pathArray.length - 1];
    }

    /**
     * Returns the fileName of an uploadedfile.
     *
     * @param fullName the full name that we need to parse
     * @return the FileName String
     */
    public static String fetchFileName(String fullName) {
        // Fetching filename
        String ret = fullName;
        int lastWinSeparator = fullName.lastIndexOf("\\");
        int lastUnixSeparator = fullName.lastIndexOf("/");
        int lastSeparator = Math.max(lastWinSeparator, lastUnixSeparator);
        if (lastSeparator != -1) {
            ret = fullName.substring(lastSeparator + 1, fullName.length());
        }
        return ret;
    }

    /**
     * Returns the title.
     *
     * @param filename the file name
     * @return the title
     */
    public static String fetchTitle(String filename) {
        if (filename.startsWith(".")) {
            return filename;
        }
        // Fetching title
        int dot = filename.lastIndexOf(".");
        String title = filename;
        if (dot != -1) {
            title = filename.substring(0, filename.lastIndexOf("."));
        }
        return title;
    }

    /**
     * Looking if an existing Document with the same filename exists.
     *
     * @param documentManager
     * @param path
     * @param filename
     */
    public static DocumentModel getExistingDocByFileName(
            CoreSession documentManager, String path, String filename)
            throws ClientException {
        DocumentModel existing = null;
        DocumentRef pathRef = new PathRef(path);
        DocumentModelList docList = documentManager.getChildren(pathRef);
        for (DocumentModel doc : docList) {
            String currentLifeCycleState = doc.getCurrentLifeCycleState();
            // CB: NXP-2483 - This check should be done only for the documents which aren't deleted
            if (!"deleted".equals(currentLifeCycleState)) {
                String existFileName = (String) doc.getProperty("file", "filename");
                if (existFileName != null && existFileName.equals(filename)) {
                    existing = doc;
                    break;
                }
            }
        }
        return existing;
    }

    /**
     * Looking if an existing Document with the same title exists.
     *
     * @param documentManager
     * @param path
     * @param title
     */
    public static DocumentModel getExistingDocByTitle(
            CoreSession documentManager, String path, String title)
            throws ClientException {
        DocumentModel existing = null;
        DocumentRef pathRef = new PathRef(path);
        DocumentModelList docList = documentManager.getChildren(pathRef);
        for (DocumentModel doc : docList) {
            String currentLifeCycleState = doc.getCurrentLifeCycleState();
            // CB: NXP-2483 - This check should be done only for the documents which aren't deleted
            if (!"deleted".equals(currentLifeCycleState)) {
                String existTitle = (String) doc.getProperty("dublincore", "title");
                if (existTitle != null && existTitle.equals(title)) {
                    existing = doc;
                    break;
                }
            }
        }
        return existing;
    }

}
{'fraction_non_alphanumeric': 0.09183070034637154, 'fraction_numerical': 0.007265354397229027, 'mean_word_length': 2.178839957035446, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3409030', 'n_tokens_mistral': 3198, 'n_tokens_neox': 3090, 'n_words': 972}