001    /**
002     * Copyright (c) 2000-present 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.portal.model.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.util.PropsValues;
022    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
023    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
024    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
025    
026    import java.io.InputStream;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ImageImpl extends ImageBaseImpl {
032    
033            @Override
034            public byte[] getTextObj() {
035                    if (_textObj != null) {
036                            return _textObj;
037                    }
038    
039                    long imageId = getImageId();
040    
041                    try {
042                            DLFileEntry dlFileEntry = null;
043    
044                            if (PropsValues.WEB_SERVER_SERVLET_CHECK_IMAGE_GALLERY) {
045                                    dlFileEntry =
046                                            DLFileEntryLocalServiceUtil.fetchFileEntryByAnyImageId(
047                                                    imageId);
048                            }
049    
050                            InputStream is = null;
051    
052                            if ((dlFileEntry != null) &&
053                                    (dlFileEntry.getLargeImageId() == imageId)) {
054    
055                                    is = DLStoreUtil.getFileAsStream(
056                                            dlFileEntry.getCompanyId(),
057                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
058                            }
059                            else {
060                                    is = DLStoreUtil.getFileAsStream(
061                                            _DEFAULT_COMPANY_ID, _DEFAULT_REPOSITORY_ID, getFileName());
062                            }
063    
064                            byte[] bytes = FileUtil.getBytes(is);
065    
066                            _textObj = bytes;
067                    }
068                    catch (Exception e) {
069                            _log.error("Error reading image " + imageId, e);
070                    }
071    
072                    return _textObj;
073            }
074    
075            @Override
076            public void setTextObj(byte[] textObj) {
077                    _textObj = textObj;
078            }
079    
080            protected String getFileName() {
081                    return getImageId() + StringPool.PERIOD + getType();
082            }
083    
084            private static final long _DEFAULT_COMPANY_ID = 0;
085    
086            private static final long _DEFAULT_REPOSITORY_ID = 0;
087    
088            private static final Log _log = LogFactoryUtil.getLog(ImageImpl.class);
089    
090            private byte[] _textObj;
091    
092    }