001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.imagegallery.webdav;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.util.MimeTypesUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.webdav.BaseResourceImpl;
021    import com.liferay.portal.kernel.webdav.WebDAVException;
022    import com.liferay.portal.model.Image;
023    import com.liferay.portal.service.ImageLocalServiceUtil;
024    import com.liferay.portlet.imagegallery.model.IGImage;
025    
026    import java.io.InputStream;
027    
028    /**
029     * @author Alexander Chow
030     */
031    public class IGImageResourceImpl extends BaseResourceImpl {
032    
033            public IGImageResourceImpl(IGImage image, String parentPath, String name) {
034                    super(
035                            parentPath, name, image.getNameWithExtension(),
036                            image.getCreateDate(), image.getModifiedDate(),
037                            image.getImageSize());
038    
039                    setModel(image);
040                    setClassName(IGImage.class.getName());
041                    setPrimaryKey(image.getPrimaryKey());
042    
043                    _image = image;
044            }
045    
046            public boolean isCollection() {
047                    return false;
048            }
049    
050            public String getContentType() {
051                    String type = StringPool.BLANK;
052    
053                    try {
054                            type = _image.getImageType();
055                    }
056                    catch (Exception e) {
057                    }
058    
059                    return MimeTypesUtil.getContentType(type);
060            }
061    
062            public InputStream getContentAsStream() throws WebDAVException {
063                    try {
064                            Image image = ImageLocalServiceUtil.getImage(
065                                    _image.getLargeImageId());
066    
067                            byte[] bytes = image.getTextObj();
068    
069                            return new UnsyncByteArrayInputStream(bytes);
070                    }
071                    catch (Exception e) {
072                            throw new WebDAVException(e);
073                    }
074            }
075    
076            private IGImage _image;
077    
078    }