id = request.getParameter("inode");
}
String url = (request.getParameter("path") != null) ? request.getParameter("path"): request.getParameter("url");
Identifier identifier = null;
File file = null;
//Contentlet contentlet = null;
String inode = null;
Boolean fileAsContent = false;
if(UtilMethods.isSet(url)) {
//If path is the dotasset portlet
if(url.startsWith("/dotAsset")) {
StringTokenizer _st = new StringTokenizer(url, "/");
String _fileName = null;
while(_st.hasMoreElements()){
_fileName = _st.nextToken();
}
id = UtilMethods.getFileName(_fileName); // Sets the identifier
} else {
//If it's a regular path
Host currentHost;
try {
currentHost = WebAPILocator.getHostWebAPI().getCurrentHost(request);
String path = LiveCache.getPathFromCache(url, currentHost);
String cinode=path.split("/")[3];
id=APILocator.getIdentifierAPI().findFromInode(cinode).getId();
} catch (Exception e) {
Logger.error(ResizeImageServlet.class, e.getMessage(), e);
throw new ServletException(e.getMessage(), e);
}
}
}
// if we have an id
if( UtilMethods.isSet(id) ) {
try {
identifier = APILocator.getIdentifierAPI().loadFromCache(id);
if(identifier != null && (InodeUtils.isSet(identifier.getInode()))) {
//it's an identifier
String path = null;
try {
path = (liveMode) ? LiveCache.getPathFromCache(identifier.getURI(), identifier.getHostId())
: WorkingCache.getPathFromCache(identifier.getURI(), identifier.getHostId());
} catch(DotContentletStateException e) {
path = WorkingCache.getPathFromCache(identifier.getURI(), identifier.getHostId());
}
if(path==null) {
Logger.debug(this.getClass(), "Can't find path with URI " + identifier.getURI());
return;
}
inode = UtilMethods.getFileName(path);
} else {
//it might be an inode
try {
file = APILocator.getFileAPI().find(id, user, true);
} catch(Exception e) {
Contentlet c = APILocator.getContentletAPI().find(id, user, true);
fileAsContent = UtilMethods.isSet(c);
}
if(file != null && (InodeUtils.isSet(file.getInode()))) {
inode = file.getInode();
identifier = APILocator.getIdentifierAPI().find(file.getIdentifier());
} else{
//Finally it's not a file and we did the first round trip to database
//then we do the second round trip to database to find the identifier,
//but after this the next hit will find it cached.
identifier = APILocator.getIdentifierAPI().find(id);
if(identifier != null && (InodeUtils.isSet(identifier.getInode())) ) {
String path = LiveCache.getPathFromCache(identifier.getURI(), identifier.getHostId());
inode = UtilMethods.getFileName(path);
}
}
}
} catch (Exception e) {
System.out.println(e);
}
}
if (!InodeUtils.isSet(inode) && !fileAsContent) {
response.sendError(404);
return;
}
try {
//Checking permissions
/**
* Build a fake proxy file object so we
* can get inheritable permissions on it
* without having to hit cache or db
*/
file = null;
if(identifier!=null && UtilMethods.isSet(identifier.getInode())){
file = new File();
file.setIdentifier(identifier.getInode());
}
else{
try {
file = APILocator.getFileAPI().find(inode, user, true);
identifier.setId(file.getIdentifier());
} catch (Exception e) {
Logger.error(ResizeImageServlet.class,e.getMessage(),e);
}
}
if (file!=null && !permissionAPI.doesUserHavePermission(file, PERMISSION_READ, user, true)) {
if(user == null)
//Sending user to unauthorized the might send him to login
response.sendError(401, "The requested file is unauthorized");
else
//sending the user to forbidden
response.sendError(403, "The requested file is forbidden");
return;
}
} catch (DotDataException e1) {
Logger.error(this,e1.getMessage());
response.sendError(500,e1.getMessage());
return;
}
String h = request.getParameter("h");
String w = request.getParameter("w");
String maxh = request.getParameter("maxh");
String maxw = request.getParameter("maxw");
int height = Config.getIntProperty("DEFAULT_HEIGHT");
int width = Config.getIntProperty("DEFAULT_WIDTH");
int imageWidth = 0;
int imageHeight = 0;
double imageRatio = 0;
try {
if (fileAsContent || inode != null && inode.length() > 0 && InodeUtils.isSet(inode)) {
boolean isSet = false;
boolean isCont = false;
String fileName = "";
String inodeOrId = "";
String contAssetPath = "";
int imgWidth = 0;
int imgLength = 0;
String ext = "";
if(fileAsContent || id!=null && InodeUtils.isSet(identifier.getId()) && identifier.getAssetType().equals("contentlet")){
Contentlet cont = null;
if(fileAsContent) {
cont = APILocator.getContentletAPI().find(id, user, true);
inode = id;
} else {
try {
boolean live=true;
if(request.getSession().getAttribute("tm_date")!=null) {
live=true;
Identifier ident=APILocator.getIdentifierAPI().find(id);
if(UtilMethods.isSet(ident.getSysPublishDate()) || UtilMethods.isSet(ident.getSysExpireDate())) {
Date fdate=new Date(Long.parseLong((String)request.getSession().getAttribute("tm_date")));
if(UtilMethods.isSet(ident.getSysPublishDate()) && ident.getSysPublishDate().before(fdate))
live=false;
if(UtilMethods.isSet(ident.getSysExpireDate()) && ident.getSysExpireDate().before(fdate))
return; // expired!
}
}
cont = APILocator.getContentletAPI().findContentletByIdentifier(id, live, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, true);
}
catch(DotContentletStateException e) {
cont = APILocator.getContentletAPI().findContentletByIdentifier(id, false, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, true);
}
if(cont==null) {
Logger.debug(this.getClass(), "Can't find content with id " + id);
return;
}
}
FileAsset fa = APILocator.getFileAssetAPI().fromContentlet(cont);
isSet = InodeUtils.isSet(cont.getInode());
fileName = fa.getFileName();
isCont = true;
inodeOrId = cont.getInode();
contAssetPath = APILocator.getFileAssetAPI().getRealAssetPath(cont.getInode());
java.util.Map<String, Object> keyValueMap = null;
String JSONValue = cont.getStringProperty(FileAssetAPI.META_DATA_FIELD);
//Convert JSON to Table Display {key, value, order}
if(UtilMethods.isSet(JSONValue)){
keyValueMap = com.dotmarketing.portlets.structure.model.KeyValueFieldUtil.JSONValueToHashMap(JSONValue);
if(UtilMethods.isSet((String)keyValueMap.get("width"))){
imgWidth = Integer.parseInt((String)keyValueMap.get("width"));
}
if(UtilMethods.isSet((String)keyValueMap.get("height"))){
imgLength = Integer.parseInt((String)keyValueMap.get("height"));
}
}
if(!contAssetPath.endsWith(java.io.File.separator)){
contAssetPath += java.io.File.separator;
}
ext = fa.getExtension();
}else if(InodeUtils.isSet(identifier.getId())){
file = fileAPI.find(inode,user,true);
isSet =InodeUtils.isSet(file.getInode());
fileName = file.getFileName();
inodeOrId = inode;
imgWidth = file.getWidth();
imgLength = file.getHeight();
ext = file.getExtension();
}
if (isSet) {
// Set the size