Search in sources :

Example 16 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class ArtistStagedModelDataHandler method doImportStagedModel.

@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, Artist artist) throws Exception {
    long userId = portletDataContext.getUserId(artist.getUserUuid());
    ServiceContext serviceContext = portletDataContext.createServiceContext(artist);
    Artist importedArtist = null;
    if (portletDataContext.isDataStrategyMirror()) {
        Artist existingArtist = ArtistLocalServiceUtil.fetchArtistByUuidAndGroupId(artist.getUuid(), portletDataContext.getScopeGroupId());
        if (existingArtist == null) {
            serviceContext.setUuid(artist.getUuid());
            importedArtist = ArtistLocalServiceUtil.addArtist(userId, artist.getName(), artist.getBio(), null, serviceContext);
        } else {
            importedArtist = ArtistLocalServiceUtil.updateArtist(userId, existingArtist.getArtistId(), artist.getName(), artist.getBio(), null, serviceContext);
        }
    } else {
        importedArtist = ArtistLocalServiceUtil.addArtist(userId, artist.getName(), artist.getBio(), null, serviceContext);
    }
    Element artistElement = portletDataContext.getImportDataStagedModelElement(artist);
    List<Element> attachmentElements = portletDataContext.getReferenceDataElements(artistElement, FileEntry.class, PortletDataContext.REFERENCE_TYPE_WEAK);
    for (Element attachmentElement : attachmentElements) {
        String path = attachmentElement.attributeValue("path");
        FileEntry fileEntry = (FileEntry) portletDataContext.getZipEntryAsObject(path);
        InputStream inputStream = null;
        try {
            String binPath = attachmentElement.attributeValue("bin-path");
            if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) {
                try {
                    inputStream = _getContentStream(fileEntry);
                } catch (NoSuchFileException nsfe) {
                }
            } else {
                inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
            }
            if (inputStream == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Unable to import attachment for file entry " + fileEntry.getFileEntryId());
                }
                continue;
            }
            importedArtist = ArtistLocalServiceUtil.updateArtist(userId, importedArtist.getArtistId(), importedArtist.getName(), importedArtist.getBio(), inputStream, serviceContext);
        } finally {
            StreamUtil.cleanUp(inputStream);
        }
    }
    portletDataContext.importClassedModel(artist, importedArtist);
}
Also used : Artist(org.liferay.jukebox.model.Artist) ServiceContext(com.liferay.portal.service.ServiceContext) UnsyncByteArrayInputStream(com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream) InputStream(java.io.InputStream) Element(com.liferay.portal.kernel.xml.Element) NoSuchFileException(com.liferay.portlet.documentlibrary.NoSuchFileException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry)

Example 17 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class SongImpl method getSongURL.

public String getSongURL(ThemeDisplay themeDisplay, String audioContainer) throws PortalException, SystemException {
    FileEntry fileEntry = getFileEntry(themeDisplay, Constants.SONGS_FOLDER_NAME);
    if (fileEntry == null) {
        return StringPool.BLANK;
    }
    if (!AudioProcessorUtil.hasAudio(fileEntry.getLatestFileVersion())) {
        return StringPool.BLANK;
    }
    if (Validator.isNull(audioContainer)) {
        audioContainer = "mp3";
    }
    String queryString = "&audioPreview=1&type=" + audioContainer;
    return DLUtil.getPreviewURL(fileEntry, fileEntry.getLatestFileVersion(), themeDisplay, queryString);
}
Also used : FileEntry(com.liferay.portal.kernel.repository.model.FileEntry)

Example 18 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class KBArticleLocalServiceImpl method addKBArticleAttachment.

protected void addKBArticleAttachment(long userId, long groupId, long resourcePrimKey, String selectedFileName) throws PortalException, SystemException {
    FileEntry tempFileEntry = TempFileUtil.getTempFile(groupId, userId, selectedFileName, KnowledgeBaseConstants.TEMP_FOLDER_NAME);
    InputStream inputStream = tempFileEntry.getContentStream();
    String mimeType = tempFileEntry.getMimeType();
    addAttachment(userId, resourcePrimKey, selectedFileName, inputStream, mimeType);
    if (tempFileEntry != null) {
        TempFileUtil.deleteTempFile(tempFileEntry.getFileEntryId());
    }
}
Also used : InputStream(java.io.InputStream) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry)

Example 19 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class WebServerServlet method sendFile.

protected void sendFile(HttpServletRequest request, HttpServletResponse response, User user, String[] pathArray) throws Exception {
    FileEntry fileEntry = getFileEntry(pathArray);
    if (fileEntry == null) {
        throw new NoSuchFileEntryException();
    }
    String version = ParamUtil.getString(request, "version");
    if (Validator.isNull(version)) {
        if (Validator.isNotNull(fileEntry.getVersion())) {
            version = fileEntry.getVersion();
        }
    }
    String tempFileId = DLUtil.getTempFileId(fileEntry.getFileEntryId(), version);
    FileVersion fileVersion = fileEntry.getFileVersion(version);
    String fileName = fileVersion.getTitle();
    String extension = fileVersion.getExtension();
    if (Validator.isNotNull(extension) && !fileName.endsWith(StringPool.PERIOD + extension)) {
        fileName += StringPool.PERIOD + extension;
    }
    boolean converted = false;
    String targetExtension = ParamUtil.getString(request, "targetExtension");
    int imageThumbnail = ParamUtil.getInteger(request, "imageThumbnail");
    int documentThumbnail = ParamUtil.getInteger(request, "documentThumbnail");
    int previewFileIndex = ParamUtil.getInteger(request, "previewFileIndex");
    boolean audioPreview = ParamUtil.getBoolean(request, "audioPreview");
    boolean videoPreview = ParamUtil.getBoolean(request, "videoPreview");
    int videoThumbnail = ParamUtil.getInteger(request, "videoThumbnail");
    InputStream inputStream = null;
    long contentLength = 0;
    if ((imageThumbnail > 0) && (imageThumbnail <= 3)) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(fileVersion.getExtension());
        int thumbnailIndex = imageThumbnail - 1;
        inputStream = ImageProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex);
        contentLength = ImageProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex);
        converted = true;
    } else if ((documentThumbnail > 0) && (documentThumbnail <= 3)) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(PDFProcessorImpl.THUMBNAIL_TYPE);
        int thumbnailIndex = documentThumbnail - 1;
        inputStream = PDFProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex);
        contentLength = PDFProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex);
        converted = true;
    } else if (previewFileIndex > 0) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(PDFProcessorImpl.PREVIEW_TYPE);
        inputStream = PDFProcessorUtil.getPreviewAsStream(fileVersion, previewFileIndex);
        contentLength = PDFProcessorUtil.getPreviewFileSize(fileVersion, previewFileIndex);
        converted = true;
    } else if (audioPreview) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(AudioProcessor.PREVIEW_TYPE);
        inputStream = AudioProcessorUtil.getPreviewAsStream(fileVersion);
        contentLength = AudioProcessorUtil.getPreviewFileSize(fileVersion);
        converted = true;
    } else if (videoPreview) {
        String type = ParamUtil.getString(request, "type");
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(type);
        inputStream = VideoProcessorUtil.getPreviewAsStream(fileVersion, type);
        contentLength = VideoProcessorUtil.getPreviewFileSize(fileVersion, type);
        response.setHeader(HttpHeaders.ACCEPT_RANGES, HttpHeaders.ACCEPT_RANGES_BYTES_VALUE);
        List<Range> ranges = null;
        try {
            ranges = ServletResponseUtil.getRanges(request, response, contentLength);
        } catch (IOException ioe) {
            if (_log.isErrorEnabled()) {
                _log.error(ioe);
            }
            response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + contentLength);
            response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
            return;
        }
        if ((ranges != null) && (ranges.size() > 0)) {
            if (_log.isDebugEnabled()) {
                _log.debug("Video range requested");
            }
            String contentType = MimeTypesUtil.getContentType(fileName);
            ServletResponseUtil.write(request, response, fileName, ranges, inputStream, contentLength, contentType);
            return;
        }
        converted = true;
    } else if ((videoThumbnail > 0) && (videoThumbnail <= 3)) {
        fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(VideoProcessorImpl.THUMBNAIL_TYPE);
        int thumbnailIndex = videoThumbnail - 1;
        inputStream = VideoProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex);
        contentLength = VideoProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex);
        converted = true;
    } else {
        inputStream = fileVersion.getContentStream(true);
        contentLength = fileVersion.getSize();
        if (Validator.isNotNull(targetExtension)) {
            File convertedFile = DocumentConversionUtil.convert(tempFileId, inputStream, extension, targetExtension);
            if (convertedFile != null) {
                fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(targetExtension);
                inputStream = Files.newInputStream(convertedFile.toPath());
                contentLength = convertedFile.length();
                converted = true;
            }
        }
    }
    String contentType = null;
    if (converted) {
        contentType = MimeTypesUtil.getContentType(fileName);
    } else {
        contentType = fileVersion.getMimeType();
    }
    ServletResponseUtil.sendFile(request, response, fileName, inputStream, contentLength, contentType);
}
Also used : InputStream(java.io.InputStream) FileVersion(com.liferay.portal.kernel.repository.model.FileVersion) NoSuchFileEntryException(com.liferay.portlet.documentlibrary.NoSuchFileEntryException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry) IOException(java.io.IOException) Range(com.liferay.portal.kernel.servlet.Range) File(java.io.File)

Example 20 with FileEntry

use of com.liferay.portal.kernel.repository.model.FileEntry in project liferay-ide by liferay.

the class WebServerServlet method sendFile.

protected void sendFile(HttpServletResponse response, User user, long groupId, long folderId, String title) throws Exception {
    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(groupId, folderId, title);
    String contentType = fileEntry.getMimeType();
    response.setContentType(contentType);
    InputStream inputStream = fileEntry.getContentStream();
    ServletResponseUtil.write(response, inputStream);
}
Also used : InputStream(java.io.InputStream) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) DLFileEntry(com.liferay.portlet.documentlibrary.model.DLFileEntry)

Aggregations

FileEntry (com.liferay.portal.kernel.repository.model.FileEntry)34 DLFileEntry (com.liferay.portlet.documentlibrary.model.DLFileEntry)15 PortalException (com.liferay.portal.kernel.exception.PortalException)8 Folder (com.liferay.portal.kernel.repository.model.Folder)7 ServiceContext (com.liferay.portal.service.ServiceContext)6 IOException (java.io.IOException)6 SystemException (com.liferay.portal.kernel.exception.SystemException)5 JSONObject (com.liferay.portal.kernel.json.JSONObject)5 Element (com.liferay.portal.kernel.xml.Element)5 User (com.liferay.portal.model.User)5 NoSuchFileEntryException (com.liferay.portlet.documentlibrary.NoSuchFileEntryException)5 InputStream (java.io.InputStream)5 StringBundler (com.liferay.portal.kernel.util.StringBundler)4 ArrayList (java.util.ArrayList)4 NoSuchGroupException (com.liferay.portal.NoSuchGroupException)3 RepositoryException (com.liferay.portal.kernel.repository.RepositoryException)3 FileVersion (com.liferay.portal.kernel.repository.model.FileVersion)3 Group (com.liferay.portal.model.Group)3 Repository (com.liferay.portal.model.Repository)3 PrincipalException (com.liferay.portal.security.auth.PrincipalException)3