1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }