Example usage for com.liferay.portal.kernel.util MimeTypesUtil getContentType

List of usage examples for com.liferay.portal.kernel.util MimeTypesUtil getContentType

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util MimeTypesUtil getContentType.

Prototype

public static String getContentType(InputStream inputStream, String fileName) 

Source Link

Document

Returns the content type from the input stream and file name.

Usage

From source file:au.com.permeance.liferay.portal.verify.CustomVerifyDocumentLibrary.java

License:Open Source License

@Override
protected void checkMimeTypes() throws Exception {

    List<DLFileEntry> dlFileEntries = DLFileEntryLocalServiceUtil
            .getFileEntriesByMimeType(ContentTypes.APPLICATION_OCTET_STREAM);

    if (_log.isDebugEnabled()) {
        _log.debug("Processing " + dlFileEntries.size() + " file entries with "
                + ContentTypes.APPLICATION_OCTET_STREAM);
    }//from w w w.  j  a v a 2  s.  co m

    for (DLFileEntry dlFileEntry : dlFileEntries) {

        if (!fileEntryHasFile(dlFileEntry)) {
            if (_log.isDebugEnabled()) {
                String fileEntryDetails = buildFileEntryDetails(dlFileEntry);
                _log.warn("Skipping file entry " + dlFileEntry.getTitle() + " due to missing file : "
                        + fileEntryDetails);
            }

            continue;
        }

        InputStream inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(dlFileEntry.getUserId(),
                dlFileEntry.getFileEntryId(), dlFileEntry.getVersion(), false);

        String title = DLUtil.getTitleWithExtension(dlFileEntry.getTitle(), dlFileEntry.getExtension());

        String mimeType = MimeTypesUtil.getContentType(inputStream, title);

        if (mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
            continue;
        }

        dlFileEntry.setMimeType(mimeType);

        DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

        DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();

        dlFileVersion.setMimeType(mimeType);

        DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
    }

    if (_log.isDebugEnabled()) {
        _log.debug("Fixed file entries with invalid mime types");
    }
}

From source file:com.custom.portal.verify.CustomVerifyDocumentLibrary.java

License:Open Source License

protected String getMimeType(InputStream inputStream, String title) {
    String mimeType = null;/*from  ww w  .  j a va  2  s. com*/

    try {
        mimeType = MimeTypesUtil.getContentType(inputStream, title);
    } finally {
        StreamUtil.cleanUp(inputStream);
    }

    return mimeType;
}

From source file:com.liferay.compat.hook.webdav.CompatDLWebDAVStorageImpl.java

License:Open Source License

public Status lockResource(WebDAVRequest webDAVRequest, String owner, long timeout) throws WebDAVException {

    Resource resource = getResource(webDAVRequest);

    Lock lock = null;//from   w  w  w . ja v  a 2 s .co m
    int status = HttpServletResponse.SC_OK;

    try {
        if (resource == null) {
            status = HttpServletResponse.SC_CREATED;

            HttpServletRequest request = webDAVRequest.getHttpServletRequest();

            String[] pathArray = webDAVRequest.getPathArray();

            long companyId = webDAVRequest.getCompanyId();
            long groupId = webDAVRequest.getGroupId();
            long parentFolderId = getParentFolderId(companyId, pathArray);
            String title = WebDAVUtil.getResourceName(pathArray);

            String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
                    ContentTypes.APPLICATION_OCTET_STREAM);

            if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
                contentType = MimeTypesUtil.getContentType(request.getInputStream(), title);
            }

            String description = StringPool.BLANK;
            String changeLog = StringPool.BLANK;

            File file = FileUtil.createTempFile(FileUtil.getExtension(title));

            file.createNewFile();

            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
            serviceContext.setAddGuestPermissions(true);

            FileEntry fileEntry = DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType,
                    title, description, changeLog, file, serviceContext);

            resource = toResource(webDAVRequest, fileEntry, false);
        }

        if (isInstanceOfDLFileEntryResourceImpl(super.getResource(webDAVRequest))) {

            FileEntry fileEntry = (FileEntry) resource.getModel();

            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAttribute(DLUtil.MANUAL_CHECK_IN_REQUIRED,
                    CompatWebDAVThreadLocal.isManualCheckInRequired());

            DLAppServiceUtil.checkOutFileEntry(fileEntry.getFileEntryId(), owner, timeout, serviceContext);

            lock = fileEntry.getLock();
        } else {
            boolean inheritable = false;

            long depth = WebDAVUtil.getDepth(webDAVRequest.getHttpServletRequest());

            if (depth != 0) {
                inheritable = true;
            }

            Folder folder = (Folder) resource.getModel();

            lock = DLAppServiceUtil.lockFolder(folder.getRepositoryId(), folder.getFolderId(), owner,
                    inheritable, timeout);
        }
    } catch (Exception e) {

        // DuplicateLock is 423 not 501

        if (!(e instanceof DuplicateLockException)) {
            throw new WebDAVException(e);
        }

        status = WebDAVUtil.SC_LOCKED;
    }

    return new Status(lock, status);
}

From source file:com.liferay.document.library.search.test.DLFileEntryIndexerLocalizedContentTest.java

License:Open Source License

protected FileEntry addFileEntry(String fileName, long groupId) throws Exception {

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(groupId);

    File file = null;/*  w  w w . ja  v  a2  s . c  o  m*/
    FileEntry fileEntry = null;

    try (InputStream inputStream = DLFileEntrySearchTest.class
            .getResourceAsStream("dependencies/" + fileName)) {

        String mimeType = MimeTypesUtil.getContentType(file, fileName);

        file = FileUtil.createTempFile(inputStream);

        fileEntry = DLAppLocalServiceUtil.addFileEntry(serviceContext.getUserId(),
                serviceContext.getScopeGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, fileName,
                mimeType, fileName, StringPool.BLANK, StringPool.BLANK, file, serviceContext);
    } finally {
        FileUtil.delete(file);
    }

    return fileEntry;
}

From source file:com.liferay.document.library.search.test.DLFileEntrySearchTest.java

License:Open Source License

@Test
public void testSearchTikaRawMetadata() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    int initialBaseModelsSearchCount = 0;

    assertBaseModelsCount(initialBaseModelsSearchCount, "Word", searchContext);

    String fileName = "OSX_Test.docx";

    InputStream inputStream = DLFileEntrySearchTest.class.getResourceAsStream("dependencies/" + fileName);

    File file = null;/*www.  ja va 2s  .  c  o m*/

    try {
        String mimeType = MimeTypesUtil.getContentType(file, fileName);

        file = FileUtil.createTempFile(inputStream);

        DLAppLocalServiceUtil.addFileEntry(serviceContext.getUserId(), serviceContext.getScopeGroupId(),
                DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, fileName, mimeType, fileName, StringPool.BLANK,
                StringPool.BLANK, file, serviceContext);
    } finally {
        FileUtil.delete(file);
    }

    assertBaseModelsCount(initialBaseModelsSearchCount + 1, "Word", searchContext);
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

protected String getContentType(HttpServletRequest request, File file, String title, String extension) {

    String contentType = GetterUtil.getString(request.getHeader(HttpHeaders.CONTENT_TYPE),
            ContentTypes.APPLICATION_OCTET_STREAM);

    if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)
            || contentType.equals(MS_OFFICE_2010_TEXT_XML_UTF8)) {

        contentType = MimeTypesUtil.getContentType(file, title);
    }//from  ww  w  .  j av  a 2 s  .c  o  m

    return contentType;
}

From source file:com.liferay.image.uploader.web.internal.portlet.action.UploadImageMVCActionCommand.java

License:Open Source License

protected FileEntry addTempImageFileEntry(PortletRequest portletRequest) throws Exception {

    UploadPortletRequest uploadPortletRequest = _portal.getUploadPortletRequest(portletRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String contentType = uploadPortletRequest.getContentType("fileName");

    String fileName = uploadPortletRequest.getFileName("fileName");

    File file = uploadPortletRequest.getFile("fileName");

    String mimeType = MimeTypesUtil.getContentType(file, fileName);

    if (!StringUtil.equalsIgnoreCase(ContentTypes.APPLICATION_OCTET_STREAM, mimeType)) {

        contentType = mimeType;/*from w  w  w  .  j av a  2  s.c o  m*/
    }

    if (!MimeTypesUtil.isWebImage(contentType)) {
        throw new ImageTypeException();
    }

    try {
        TempFileEntryUtil.deleteTempFileEntry(themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
                UploadImageUtil.getTempImageFolderName(), fileName);
    } catch (Exception e) {
    }

    return TempFileEntryUtil.addTempFileEntry(themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
            UploadImageUtil.getTempImageFolderName(), fileName, file, contentType);
}

From source file:com.liferay.knowledgebase.util.KnowledgeBaseUtil.java

License:Open Source License

public static String getMimeType(byte[] bytes, String fileName) {
    InputStream inputStream = new UnsyncByteArrayInputStream(bytes);

    try {//from w  w w  .  j  av  a2s  .  c om
        return MimeTypesUtil.getContentType(inputStream, fileName);
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}

From source file:com.liferay.portlet.documentlibrary.sharepoint.DLSharepointStorageImpl.java

License:Open Source License

@Override
public void putDocument(SharepointRequest sharepointRequest) throws Exception {

    HttpServletRequest request = sharepointRequest.getHttpServletRequest();

    String documentPath = sharepointRequest.getRootPath();
    String parentFolderPath = getParentFolderPath(documentPath);

    long groupId = SharepointUtil.getGroupId(parentFolderPath);
    long parentFolderId = getLastFolderId(groupId, parentFolderPath,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
    String title = getResourceName(documentPath);
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
            ContentTypes.APPLICATION_OCTET_STREAM);

    String extension = FileUtil.getExtension(title);

    File file = null;//  w  w w .j a v a 2 s  .  com

    try {
        file = FileUtil.createTempFile(extension);

        FileUtil.write(file, request.getInputStream());

        if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
            contentType = MimeTypesUtil.getContentType(file, title);
        }

        try {
            FileEntry fileEntry = getFileEntry(sharepointRequest);

            long fileEntryId = fileEntry.getFileEntryId();

            description = fileEntry.getDescription();

            String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(FileEntry.class.getName(),
                    fileEntry.getFileEntryId());

            serviceContext.setAssetTagNames(assetTagNames);

            DLAppServiceUtil.updateFileEntry(fileEntryId, title, contentType, title, description, changeLog,
                    false, file, serviceContext);
        } catch (NoSuchFileEntryException nsfee) {
            DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType, title, description,
                    changeLog, file, serviceContext);
        }
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public Status lockResource(WebDAVRequest webDavRequest, String owner, long timeout) throws WebDAVException {

    Resource resource = getResource(webDavRequest);

    Lock lock = null;//  w w w  . j a v  a 2  s. c om
    int status = HttpServletResponse.SC_OK;

    try {
        if (resource == null) {
            status = HttpServletResponse.SC_CREATED;

            HttpServletRequest request = webDavRequest.getHttpServletRequest();

            String[] pathArray = webDavRequest.getPathArray();

            long companyId = webDavRequest.getCompanyId();
            long groupId = webDavRequest.getGroupId();
            long parentFolderId = getParentFolderId(companyId, pathArray);
            String title = WebDAVUtil.getResourceName(pathArray);

            String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
                    ContentTypes.APPLICATION_OCTET_STREAM);

            if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
                contentType = MimeTypesUtil.getContentType(request.getInputStream(), title);
            }

            String description = StringPool.BLANK;
            String changeLog = StringPool.BLANK;

            File file = FileUtil.createTempFile(FileUtil.getExtension(title));

            file.createNewFile();

            ServiceContext serviceContext = new ServiceContext();

            serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
            serviceContext.setAddGuestPermissions(true);

            FileEntry fileEntry = DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType,
                    title, description, changeLog, file, serviceContext);

            resource = toResource(webDavRequest, fileEntry, false);
        }

        if (resource instanceof DLFileEntryResourceImpl) {
            FileEntry fileEntry = (FileEntry) resource.getModel();

            lock = DLAppServiceUtil.lockFileEntry(fileEntry.getFileEntryId(), owner, timeout);
        } else {
            boolean inheritable = false;

            long depth = WebDAVUtil.getDepth(webDavRequest.getHttpServletRequest());

            if (depth != 0) {
                inheritable = true;
            }

            Folder folder = (Folder) resource.getModel();

            lock = DLAppServiceUtil.lockFolder(folder.getRepositoryId(), folder.getFolderId(), owner,
                    inheritable, timeout);
        }
    } catch (Exception e) {

        // DuplicateLock is 423 not 501

        if (!(e instanceof DuplicateLockException)) {
            throw new WebDAVException(e);
        }

        status = WebDAVUtil.SC_LOCKED;
    }

    return new Status(lock, status);
}