001/* 002 * (C) Copyright 2006-2016 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 */ 019package org.nuxeo.ecm.platform.filemanager.utils; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.net.MalformedURLException; 026import java.net.URL; 027import java.text.Normalizer; 028import java.text.Normalizer.Form; 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.PathRef; 036 037public final class FileManagerUtils { 038 039 // This is an utility class 040 private FileManagerUtils() { 041 } 042 043 /** 044 * Returns the contents of the file in a byte array. 045 * 046 * @deprecated since 7.2, use {@link IOUtils#toByteArray} instead 047 */ 048 @Deprecated 049 public static byte[] getBytesFromFile(File file) throws IOException { 050 try (InputStream in = new FileInputStream(file)) { 051 return IOUtils.toByteArray(in); 052 } 053 } 054 055 /** 056 * Returns the fileName of a file. 057 */ 058 public static String fetchFileName(File file) throws MalformedURLException { 059 // Fetching filename 060 URL pathUrl = file.toURI().toURL(); 061 String[] pathArray = pathUrl.getFile().split("/"); 062 return pathArray[pathArray.length - 1]; 063 } 064 065 /** 066 * Returns the fileName of an uploaded file. 067 * 068 * @param fullName the full name that we need to parse 069 * @return the FileName String 070 */ 071 // FIXME: badly named method 072 // FIXME: doesn't work in some corner cases, for instance a Unix filename 073 // with a \, or a DOS file with a / 074 public static String fetchFileName(String fullName) { 075 // Fetching filename 076 // first normalize input, as unicode can be decomposed (macOS behavior on WebDAV) 077 String ret = Normalizer.normalize(fullName, Form.NFC); 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 // We must use the "file:content/name" sub-property which is the only 106 // one on which we can rely 107 // Note that the "file:content" property is handled in a particular way 108 // by NXQL, so we must use "content/name" instead of 109 // "file:content/name". 110 return getExistingDocByPropertyName(documentManager, path, filename, "content/name"); 111 } 112 113 /** 114 * Looks if an existing Document with the same title exists. 115 */ 116 public static DocumentModel getExistingDocByTitle(CoreSession documentManager, String path, String title) { 117 return getExistingDocByPropertyName(documentManager, path, title, "dc:title"); 118 } 119 120 /** 121 * Looks if an existing Document has the same value for a given property. 122 */ 123 public static DocumentModel getExistingDocByPropertyName(CoreSession documentManager, String path, String value, 124 String propertyName) { 125 value = Normalizer.normalize(value, Normalizer.Form.NFC); 126 DocumentModel existing = null; 127 String parentId = documentManager.getDocument(new PathRef(path)).getId(); 128 String query = "SELECT * FROM Document WHERE ecm:parentId = '" + parentId + "' AND " + propertyName + " = '" 129 + value.replace("'", "\\\'") + "' AND ecm:isTrashed = 0"; 130 DocumentModelList docs = documentManager.query(query, 1); 131 if (docs.size() > 0) { 132 existing = docs.get(0); 133 } 134 return existing; 135 } 136 137}