Thursday 25 October 2012

How to add file to media gallery

      It is usefull to upload and store files in Liferay's Documets and Media library. How to do it check this post.
      For version below 6.1 check this post.

      For version 6.1 you can do it next way:
          1) If you want upload file as logged in user you can use this method:
import com.liferay.portlet.documentlibrary.model.DLFileEntry;
import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
import com.liferay.portal.kernel.util.MimeTypesUtil

FileEntry fileEntry = DLAppServiceUtil.addFileEntry(repositoryId,
                                                        folderId,
                                                        fotoFileName,
                                                        MimeTypesUtil.getContentType(fotoFilename),
                                                        title,
                                                        description,
                                                        changeLog,
                                                        bytes,
                                                        serviceContext);
           also you may require to set some permission for newly uploaded file, it can be done with next method:
import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
import com.liferay.portal.model.ResourceConstants;
import com.liferay.portal.security.permission.ActionKeys;
ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId(
                DLFileEntry.class.getName(),
                ResourceConstants.SCOPE_INDIVIDUAL,
                String.valueOf(fileEntry
                 .getFileEntryId()),
                roleId,
                new String[] {ActionKeys.VIEW});
          if you want immediatly show uploaded image you may need to force Liferay generate thumbnails, can be done with this:
import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
ImageProcessorUtil.generateImages(fileEntry.getLatestFileVersion());

        2) In case of uploading file as guest you should do next:
              2.1) Override resource-actions/documentlibrary.xml in to ext-plugin.
                      Delete <action-key>ADD_DOCUMENT</action-key> from  <guest-unsupported>
                      block.
              2.2) redeploy ext-plugin, after that you will be able to set Add document for guest user.
              2.3) set permission Add document for some folder from Control panel for Guest user.
              2.4) Use next method (previous method will throw PrincipalException):
private void addFileToGallery(final byte[] bytes,
                                final String fileName,
                                final String title,
                                final long companyId,
                                final long userId,
                                final ActionRequest request) {
    long repositoryId = 10180;
    long folderId = 111111;
    File file = null;
    try {

      ServiceContext serviceContext = ServiceContextFactory.getInstance(request);

      long fileEntryTypeId = ParamUtil.getLong(serviceContext, "fileEntryTypeId", -1L);
      Map<String, Fields> fieldsMap = new HashMap<String, Fields>();

      file = FileUtil.createTempFile(bytes);
      DLFileEntryLocalServiceUtil.addFileEntry(userId,
                                               repositoryId,
                                               repositoryId,
                                               folderId,
                                               fileName,
                                               MimeTypesUtil.getContentType(fileName),
                                               title,
                                               "description",
                                               "changeLog",
                                               fileEntryTypeId,
                                               fieldsMap,
                                               file,
                                               null,
                                               bytes.length,
                                               serviceContext);
    } catch (IOException e) {
      LOGGER.error("Can't create file: " + e);
    } catch (PortalException e) {
      LOGGER.error("Can't add file to gallery: " + e);
    } catch (SystemException e) {
      LOGGER.error("Can't add file to gallery: " + e);
    } finally {
      FileUtil.delete(file);
    }
  }

     To display image from Document and Media gallery:
FileEntry fe = DLAppServiceUtil.getFileEntry(fileEntryId);
String fotoThumbUrl = DLUtil.getThumbnailSrc(fe, null,themeDisplay);

BR,
Paul Butenko

No comments:

Post a Comment