001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.functions.file; 020 021import lucee.commons.io.res.Resource; 022import lucee.commons.io.res.util.ResourceUtil; 023import lucee.commons.lang.StringUtil; 024import lucee.runtime.PageContext; 025import lucee.runtime.PageContextImpl; 026import lucee.runtime.exp.FunctionException; 027import lucee.runtime.exp.PageException; 028 029public class FileOpen { 030 031 public static Object call(PageContext pc, String path) throws PageException { 032 return call(pc, path,"read",((PageContextImpl)pc).getResourceCharset().name(),false); 033 } 034 public static Object call(PageContext pc, String path,String mode) throws PageException { 035 return call(pc, path,mode,((PageContextImpl)pc).getResourceCharset().name(),false); 036 } 037 038 public static Object call(PageContext pc, String path,String strMode, String charset) throws PageException { 039 return call(pc, path,strMode,charset,false); 040 } 041 042 public static Object call(PageContext pc, String path,String strMode, String charset,boolean seekable) throws PageException { 043 044 strMode=strMode.trim().toLowerCase(); 045 if(StringUtil.isEmpty(charset,true)) 046 charset=((PageContextImpl)pc).getResourceCharset().name(); 047 //try { 048 049 if("read".equals(strMode)) { 050 return new FileStreamWrapperRead(check(pc,ResourceUtil.toResourceExisting(pc, path)),charset,seekable); 051 } 052 if("readbinary".equals(strMode)) { 053 return new FileStreamWrapperReadBinary(check(pc,ResourceUtil.toResourceExisting(pc, path)),seekable); 054 } 055 if("write".equals(strMode)) { 056 return new FileStreamWrapperWrite(check(pc,ResourceUtil.toResourceNotExisting(pc, path)),charset,false,seekable); 057 } 058 if("append".equals(strMode)) { 059 return new FileStreamWrapperWrite(check(pc,ResourceUtil.toResourceNotExisting(pc, path)),charset,true,seekable); 060 } 061 if("readwrite".equals(strMode)) { 062 return new FileStreamWrapperReadWrite(check(pc,ResourceUtil.toResourceNotExisting(pc, path)),charset,seekable); 063 } 064 065 066 throw new FunctionException(pc,"FileOpen",2,"mode","invalid value ["+strMode+"], valid values for argument mode are [read,readBinary,append,write,readwrite]"); 067 068 069 070 } 071 private static Resource check(PageContext pc, Resource res) throws PageException { 072 pc.getConfig().getSecurityManager().checkFileLocation(res); 073 return res; 074 } 075 076}