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