1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.imagegallery.webdav;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.util.MimeTypesUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.model.Image;
21  import com.liferay.portal.service.ImageLocalServiceUtil;
22  import com.liferay.portal.webdav.BaseResourceImpl;
23  import com.liferay.portal.webdav.WebDAVException;
24  import com.liferay.portlet.imagegallery.model.IGImage;
25  
26  import java.io.InputStream;
27  
28  /**
29   * <a href="IGImageResourceImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Alexander Chow
32   */
33  public class IGImageResourceImpl extends BaseResourceImpl {
34  
35      public IGImageResourceImpl(IGImage image, String parentPath, String name) {
36          super(
37              parentPath, name, image.getNameWithExtension(),
38              image.getCreateDate(), image.getModifiedDate(),
39              image.getImageSize());
40  
41          setModel(image);
42          setClassName(IGImage.class.getName());
43          setPrimaryKey(image.getPrimaryKey());
44  
45          _image = image;
46      }
47  
48      public boolean isCollection() {
49          return false;
50      }
51  
52      public String getContentType() {
53          String type = StringPool.BLANK;
54  
55          try {
56              type = _image.getImageType();
57          }
58          catch (Exception e) {
59          }
60  
61          return MimeTypesUtil.getContentType(type);
62      }
63  
64      public InputStream getContentAsStream() throws WebDAVException {
65          try {
66              Image image = ImageLocalServiceUtil.getImage(
67                  _image.getLargeImageId());
68  
69              byte[] bytes = image.getTextObj();
70  
71              return new UnsyncByteArrayInputStream(bytes);
72          }
73          catch (Exception e) {
74              throw new WebDAVException(e);
75          }
76      }
77  
78      private IGImage _image;
79  
80  }