List of usage examples for com.liferay.portal.kernel.util FileUtil createTempFile
public static File createTempFile(String extension)
From source file:com.liferay.journal.exportimport.data.handler.JournalArticleStagedModelDataHandler.java
License:Open Source License
@Override protected void doImportStagedModel(PortletDataContext portletDataContext, JournalArticle article) throws Exception { long userId = portletDataContext.getUserId(article.getUserUuid()); long authorId = _journalCreationStrategy.getAuthorUserId(portletDataContext, article); if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) { userId = authorId;// www . j a v a 2 s.c o m } User user = _userLocalService.getUser(userId); Map<Long, Long> folderIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(JournalFolder.class); long folderId = MapUtil.getLong(folderIds, article.getFolderId(), article.getFolderId()); String articleId = article.getArticleId(); boolean autoArticleId = false; if (Validator.isNumber(articleId) || (_journalArticleLocalService.fetchArticle(portletDataContext.getScopeGroupId(), articleId, JournalArticleConstants.VERSION_DEFAULT) != null)) { autoArticleId = true; } Map<String, String> articleIds = (Map<String, String>) portletDataContext .getNewPrimaryKeysMap(JournalArticle.class + ".articleId"); String newArticleId = articleIds.get(articleId); if (Validator.isNotNull(newArticleId)) { // A sibling of a different version was already assigned a new // article id articleId = newArticleId; autoArticleId = false; } String content = article.getContent(); content = _journalArticleExportImportContentProcessor.replaceImportContentReferences(portletDataContext, article, content); article.setContent(content); String newContent = _journalCreationStrategy.getTransformedContent(portletDataContext, article); if (newContent != JournalCreationStrategy.ARTICLE_CONTENT_UNCHANGED) { article.setContent(newContent); } Date displayDate = article.getDisplayDate(); int displayDateMonth = 0; int displayDateDay = 0; int displayDateYear = 0; int displayDateHour = 0; int displayDateMinute = 0; if (displayDate != null) { Calendar displayCal = CalendarFactoryUtil.getCalendar(user.getTimeZone()); displayCal.setTime(displayDate); displayDateMonth = displayCal.get(Calendar.MONTH); displayDateDay = displayCal.get(Calendar.DATE); displayDateYear = displayCal.get(Calendar.YEAR); displayDateHour = displayCal.get(Calendar.HOUR); displayDateMinute = displayCal.get(Calendar.MINUTE); if (displayCal.get(Calendar.AM_PM) == Calendar.PM) { displayDateHour += 12; } } Date expirationDate = article.getExpirationDate(); int expirationDateMonth = 0; int expirationDateDay = 0; int expirationDateYear = 0; int expirationDateHour = 0; int expirationDateMinute = 0; boolean neverExpire = true; if (expirationDate != null) { Calendar expirationCal = CalendarFactoryUtil.getCalendar(user.getTimeZone()); expirationCal.setTime(expirationDate); expirationDateMonth = expirationCal.get(Calendar.MONTH); expirationDateDay = expirationCal.get(Calendar.DATE); expirationDateYear = expirationCal.get(Calendar.YEAR); expirationDateHour = expirationCal.get(Calendar.HOUR); expirationDateMinute = expirationCal.get(Calendar.MINUTE); neverExpire = false; if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) { expirationDateHour += 12; } } Date reviewDate = article.getReviewDate(); int reviewDateMonth = 0; int reviewDateDay = 0; int reviewDateYear = 0; int reviewDateHour = 0; int reviewDateMinute = 0; boolean neverReview = true; if (reviewDate != null) { Calendar reviewCal = CalendarFactoryUtil.getCalendar(user.getTimeZone()); reviewCal.setTime(reviewDate); reviewDateMonth = reviewCal.get(Calendar.MONTH); reviewDateDay = reviewCal.get(Calendar.DATE); reviewDateYear = reviewCal.get(Calendar.YEAR); reviewDateHour = reviewCal.get(Calendar.HOUR); reviewDateMinute = reviewCal.get(Calendar.MINUTE); neverReview = false; if (reviewCal.get(Calendar.AM_PM) == Calendar.PM) { reviewDateHour += 12; } } Map<String, String> ddmStructureKeys = (Map<String, String>) portletDataContext .getNewPrimaryKeysMap(DDMStructure.class + ".ddmStructureKey"); String parentDDMStructureKey = MapUtil.getString(ddmStructureKeys, article.getDDMStructureKey(), article.getDDMStructureKey()); Map<String, Long> ddmStructureIds = (Map<String, Long>) portletDataContext .getNewPrimaryKeysMap(DDMStructure.class); long ddmStructureId = 0; if (article.getClassNameId() != 0) { ddmStructureId = ddmStructureIds.get(article.getClassPK()); } Map<String, String> ddmTemplateKeys = (Map<String, String>) portletDataContext .getNewPrimaryKeysMap(DDMTemplate.class + ".ddmTemplateKey"); String parentDDMTemplateKey = MapUtil.getString(ddmTemplateKeys, article.getDDMTemplateKey(), article.getDDMTemplateKey()); File smallFile = null; try { Element articleElement = portletDataContext.getImportDataStagedModelElement(article); if (article.isSmallImage()) { String smallImagePath = articleElement.attributeValue("small-image-path"); if (Validator.isNotNull(article.getSmallImageURL())) { String smallImageURL = _journalArticleExportImportContentProcessor .replaceImportContentReferences(portletDataContext, article, article.getSmallImageURL()); article.setSmallImageURL(smallImageURL); } else if (Validator.isNotNull(smallImagePath)) { byte[] bytes = portletDataContext.getZipEntryAsByteArray(smallImagePath); if (bytes != null) { smallFile = FileUtil.createTempFile(article.getSmallImageType()); FileUtil.write(smallFile, bytes); } } } JournalArticle latestArticle = _journalArticleLocalService .fetchLatestArticle(article.getResourcePrimKey()); if ((latestArticle != null) && (latestArticle.getId() == article.getId())) { List<Element> attachmentElements = portletDataContext.getReferenceDataElements(article, DLFileEntry.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 = FileEntryUtil.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; } TempFileEntryUtil.addTempFileEntry(portletDataContext.getScopeGroupId(), userId, JournalArticleStagedModelDataHandler.class.getName(), fileEntry.getFileName(), inputStream, fileEntry.getMimeType()); } finally { StreamUtil.cleanUp(inputStream); } } } String articleURL = null; boolean addGroupPermissions = _journalCreationStrategy.addGroupPermissions(portletDataContext, article); boolean addGuestPermissions = _journalCreationStrategy.addGuestPermissions(portletDataContext, article); ServiceContext serviceContext = portletDataContext.createServiceContext(article); serviceContext.setAddGroupPermissions(addGroupPermissions); serviceContext.setAddGuestPermissions(addGuestPermissions); if ((expirationDate != null) && expirationDate.before(new Date())) { article.setStatus(WorkflowConstants.STATUS_EXPIRED); } if ((article.getStatus() != WorkflowConstants.STATUS_APPROVED) && (article.getStatus() != WorkflowConstants.STATUS_SCHEDULED)) { serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT); } JournalArticle importedArticle = null; String articleResourceUuid = articleElement.attributeValue("article-resource-uuid"); // Used when importing LARs with journal schemas under 1.1.0 _setLegacyValues(article); if (portletDataContext.isDataStrategyMirror()) { serviceContext.setUuid(article.getUuid()); serviceContext.setAttribute("articleResourceUuid", articleResourceUuid); serviceContext.setAttribute("urlTitle", article.getUrlTitle()); boolean preloaded = GetterUtil.getBoolean(articleElement.attributeValue("preloaded")); JournalArticle existingArticle = fetchExistingArticle(articleResourceUuid, portletDataContext.getScopeGroupId(), articleId, newArticleId, preloaded); JournalArticle existingArticleVersion = null; if (existingArticle != null) { existingArticleVersion = fetchExistingArticleVersion(article.getUuid(), portletDataContext.getScopeGroupId(), existingArticle.getArticleId(), article.getVersion()); } if ((existingArticle != null) && (existingArticleVersion == null)) { autoArticleId = false; articleId = existingArticle.getArticleId(); } if (existingArticleVersion == null) { importedArticle = _journalArticleLocalService.addArticle(userId, portletDataContext.getScopeGroupId(), folderId, article.getClassNameId(), ddmStructureId, articleId, autoArticleId, article.getVersion(), article.getTitleMap(), article.getDescriptionMap(), article.getContent(), parentDDMStructureKey, parentDDMTemplateKey, article.getLayoutUuid(), displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, expirationDateMonth, expirationDateDay, expirationDateYear, expirationDateHour, expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute, neverReview, article.isIndexable(), article.isSmallImage(), article.getSmallImageURL(), smallFile, null, articleURL, serviceContext); } else { importedArticle = _journalArticleLocalService.updateArticle(userId, existingArticle.getGroupId(), folderId, existingArticle.getArticleId(), article.getVersion(), article.getTitleMap(), article.getDescriptionMap(), article.getContent(), parentDDMStructureKey, parentDDMTemplateKey, article.getLayoutUuid(), displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, expirationDateMonth, expirationDateDay, expirationDateYear, expirationDateHour, expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute, neverReview, article.isIndexable(), article.isSmallImage(), article.getSmallImageURL(), smallFile, null, articleURL, serviceContext); String articleUuid = article.getUuid(); String importedArticleUuid = importedArticle.getUuid(); if (!articleUuid.equals(importedArticleUuid)) { importedArticle.setUuid(articleUuid); _journalArticleLocalService.updateJournalArticle(importedArticle); } } } else { importedArticle = _journalArticleLocalService.addArticle(userId, portletDataContext.getScopeGroupId(), folderId, article.getClassNameId(), ddmStructureId, articleId, autoArticleId, article.getVersion(), article.getTitleMap(), article.getDescriptionMap(), article.getContent(), parentDDMStructureKey, parentDDMTemplateKey, article.getLayoutUuid(), displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, expirationDateMonth, expirationDateDay, expirationDateYear, expirationDateHour, expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute, neverReview, article.isIndexable(), article.isSmallImage(), article.getSmallImageURL(), smallFile, null, articleURL, serviceContext); } portletDataContext.importClassedModel(article, importedArticle); if (Validator.isNull(newArticleId)) { articleIds.put(article.getArticleId(), importedArticle.getArticleId()); } Map<Long, Long> articlePrimaryKeys = (Map<Long, Long>) portletDataContext .getNewPrimaryKeysMap(JournalArticle.class + ".primaryKey"); articlePrimaryKeys.put(article.getPrimaryKey(), importedArticle.getPrimaryKey()); } finally { if (smallFile != null) { smallFile.delete(); } } }
From source file:com.liferay.marketplace.service.impl.AppLocalServiceImpl.java
License:Open Source License
@Override public void installApp(long remoteAppId) throws PortalException { App app = appPersistence.findByRemoteAppId(remoteAppId); if (!DLStoreUtil.hasFile(app.getCompanyId(), CompanyConstants.SYSTEM, app.getFilePath())) { throw new NoSuchFileException(); }//from w w w.ja va2s . c o m String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR) + StringPool.SLASH + Time.getTimestamp(); InputStream inputStream = null; ZipFile zipFile = null; try { inputStream = DLStoreUtil.getFileAsStream(app.getCompanyId(), CompanyConstants.SYSTEM, app.getFilePath()); if (inputStream == null) { throw new IOException("Unable to open file at " + app.getFilePath()); } File liferayPackageFile = FileUtil.createTempFile(inputStream); zipFile = new ZipFile(liferayPackageFile); Enumeration<ZipEntry> enu = (Enumeration<ZipEntry>) zipFile.entries(); while (enu.hasMoreElements()) { ZipEntry zipEntry = enu.nextElement(); String fileName = zipEntry.getName(); if (!fileName.endsWith(".jar") && !fileName.endsWith(".war") && !fileName.endsWith(".xml") && !fileName.endsWith(".zip") && !fileName.equals("liferay-marketplace.properties")) { continue; } if (_log.isInfoEnabled()) { _log.info("Extracting " + fileName + " from app " + app.getAppId()); } InputStream zipInputStream = null; try { zipInputStream = zipFile.getInputStream(zipEntry); if (fileName.equals("liferay-marketplace.properties")) { String propertiesString = StringUtil.read(zipInputStream); Properties properties = PropertiesUtil.load(propertiesString); processMarketplaceProperties(properties); } else { File pluginPackageFile = new File(tmpDir + StringPool.SLASH + fileName); FileUtil.write(pluginPackageFile, zipInputStream); String bundleSymbolicName = StringPool.BLANK; String bundleVersion = StringPool.BLANK; String contextName = StringPool.BLANK; AutoDeploymentContext autoDeploymentContext = new AutoDeploymentContext(); if (fileName.endsWith(".jar")) { Manifest manifest = BundleUtil.getManifest(pluginPackageFile); Attributes attributes = manifest.getMainAttributes(); bundleSymbolicName = GetterUtil.getString(attributes.getValue("Bundle-SymbolicName")); bundleVersion = GetterUtil.getString(attributes.getValue("Bundle-Version")); contextName = GetterUtil.getString(attributes.getValue("Web-ContextPath")); } else { contextName = getContextName(fileName); autoDeploymentContext.setContext(contextName); } autoDeploymentContext.setFile(pluginPackageFile); DeployManagerUtil.deploy(autoDeploymentContext); if (Validator.isNotNull(bundleSymbolicName) || Validator.isNotNull(contextName)) { moduleLocalService.addModule(app.getUserId(), app.getAppId(), bundleSymbolicName, bundleVersion, contextName); } } } finally { StreamUtil.cleanUp(zipInputStream); } } } catch (ZipException ze) { if (_log.isInfoEnabled()) { _log.info("Deleting corrupt package from app " + app.getAppId(), ze); } deleteApp(app); } catch (IOException ioe) { throw new PortalException(ioe.getMessage()); } catch (Exception e) { _log.error(e, e); } finally { FileUtil.deltree(tmpDir); if (zipFile != null) { try { zipFile.close(); } catch (IOException ioe) { } } StreamUtil.cleanUp(inputStream); clearInstalledAppsCache(); } }
From source file:com.liferay.message.boards.service.test.MBMessageLocalServiceTest.java
License:Open Source License
@Test public void testAddMessageAttachment() throws Exception { MBMessage message = addMessage(null, false); byte[] fileBytes = FileUtil.getBytes(getClass(), "dependencies/company_logo.png"); File file = FileUtil.createTempFile(fileBytes); MBMessageLocalServiceUtil.addMessageAttachment(TestPropsValues.getUserId(), message.getMessageId(), "test", file, "image/png"); Assert.assertEquals(1, message.getAttachmentsFileEntriesCount()); }
From source file:com.liferay.message.boards.service.test.MBMessageLocalServiceTest.java
License:Open Source License
@Test public void testDeleteMessageAttachment() throws Exception { MBMessage message = addMessage(null, false); byte[] fileBytes = FileUtil.getBytes(getClass(), "dependencies/company_logo.png"); File file = FileUtil.createTempFile(fileBytes); MBMessageLocalServiceUtil.addMessageAttachment(TestPropsValues.getUserId(), message.getMessageId(), "test", file, "image/png"); Assert.assertEquals(1, message.getAttachmentsFileEntriesCount()); MBMessageLocalServiceUtil.deleteMessageAttachment(message.getMessageId(), "test"); Assert.assertEquals(0, message.getAttachmentsFileEntriesCount()); }
From source file:com.liferay.portlet.documentlibrary.antivirus.BaseFileAntivirusScanner.java
License:Open Source License
public void scan(byte[] bytes) throws AntivirusScannerException, SystemException { File file = null;//from w w w. j a v a2 s .c om try { file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION); FileUtil.write(file, bytes); scan(file); } catch (IOException ioe) { throw new SystemException("Unable to write temporary file", ioe); } finally { if (file != null) { file.delete(); } } }
From source file:com.liferay.portlet.documentlibrary.antivirus.BaseFileAntivirusScanner.java
License:Open Source License
public void scan(InputStream inputStream) throws AntivirusScannerException, SystemException { File file = null;/*from ww w .j av a2 s.c o m*/ try { file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION); FileUtil.write(file, inputStream); scan(file); } catch (IOException ioe) { throw new SystemException("Unable to write temporary file", ioe); } finally { if (file != null) { file.delete(); } } }
From source file:com.liferay.portlet.documentlibrary.service.DLAppServiceTest.java
License:Open Source License
public void testAddNullFileEntry() throws Exception { long folderId = _folder.getFolderId(); String description = StringPool.BLANK; String changeLog = StringPool.BLANK; ServiceContext serviceContext = new ServiceContext(); serviceContext.setAddGroupPermissions(true); serviceContext.setAddGuestPermissions(true); try {/*from w ww. j a v a 2 s . c o m*/ String name = "Bytes-null.txt"; byte[] bytes = null; FileEntry fileEntry = DLAppServiceUtil.addFileEntry(TestPropsValues.getGroupId(), folderId, name, ContentTypes.TEXT_PLAIN, name, description, changeLog, bytes, serviceContext); DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), name, ContentTypes.TEXT_PLAIN, name, description, changeLog, true, bytes, serviceContext); String newName = "Bytes-changed.txt"; bytes = _CONTENT.getBytes(); DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), newName, ContentTypes.TEXT_PLAIN, newName, description, changeLog, true, bytes, serviceContext); } catch (Exception e) { fail("Unable to pass null byte[] " + StackTraceUtil.getStackTrace(e)); } try { String name = "File-null.txt"; File file = null; FileEntry fileEntry = DLAppServiceUtil.addFileEntry(TestPropsValues.getGroupId(), folderId, name, ContentTypes.TEXT_PLAIN, name, description, changeLog, file, serviceContext); DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), name, ContentTypes.TEXT_PLAIN, name, description, changeLog, true, file, serviceContext); try { String newName = "File-changed.txt"; file = FileUtil.createTempFile(_CONTENT.getBytes()); DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), newName, ContentTypes.TEXT_PLAIN, newName, description, changeLog, true, file, serviceContext); } finally { FileUtil.delete(file); } } catch (Exception e) { fail("Unable to pass null File " + StackTraceUtil.getStackTrace(e)); } try { String name = "IS-null.txt"; InputStream is = null; FileEntry fileEntry = DLAppServiceUtil.addFileEntry(TestPropsValues.getGroupId(), folderId, name, ContentTypes.TEXT_PLAIN, name, description, changeLog, is, 0, serviceContext); DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), name, ContentTypes.TEXT_PLAIN, name, description, changeLog, true, is, 0, serviceContext); try { String newName = "IS-changed.txt"; is = new ByteArrayInputStream(_CONTENT.getBytes()); DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), newName, ContentTypes.TEXT_PLAIN, newName, description, changeLog, true, is, 0, serviceContext); } finally { if (is != null) { is.close(); } } } catch (Exception e) { fail("Unable to pass null InputStream " + StackTraceUtil.getStackTrace(e)); } }
From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppLocalServiceImpl.java
License:Open Source License
/** * Adds a file entry and associated metadata based on a byte array. * * <p>//from w w w. j a v a 2 s .c o m * This method takes two file names, the <code>sourceFileName</code> and the * <code>title</code>. The <code>sourceFileName</code> corresponds to the * name of the actual file being uploaded. The <code>title</code> * corresponds to a name the client wishes to assign this file after it has * been uploaded to the portal. If it is <code>null</code>, the <code> * sourceFileName</code> will be used. * </p> * * @param userId the primary key of the file entry's creator/owner * @param repositoryId the primary key of the file entry's repository * @param folderId the primary key of the file entry's parent folder * @param sourceFileName the original file's name * @param mimeType the file's MIME type * @param title the name to be assigned to the file (optionally <code>null * </code>) * @param description the file's description * @param changeLog the file's version change log * @param bytes the file's data (optionally <code>null</code>) * @param serviceContext the service context to be applied. Can set the * asset category IDs, asset tag names, and expando bridge * attributes for the file entry. In a Liferay repository, it may * include: <ul> <li> fileEntryTypeId - ID for a custom file entry * type </li> <li> fieldsMap - mapping for fields associated with a * custom file entry type </li> </ul> * @return the file entry * @throws PortalException if the parent folder could not be found or if the * file entry's information was invalid * @throws SystemException if a system exception occurred */ public FileEntry addFileEntry(long userId, long repositoryId, long folderId, String sourceFileName, String mimeType, String title, String description, String changeLog, byte[] bytes, ServiceContext serviceContext) throws PortalException, SystemException { File file = null; try { if ((bytes != null) && (bytes.length > 0)) { file = FileUtil.createTempFile(bytes); } return addFileEntry(userId, repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, file, serviceContext); } catch (IOException ioe) { throw new SystemException("Unable to write temporary file", ioe); } finally { FileUtil.delete(file); } }
From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppLocalServiceImpl.java
License:Open Source License
/** * Updates a file entry and associated metadata based on a byte array * object. If the file data is <code>null</code>, then only the associated * metadata (i.e., <code>title</code>, <code>description</code>, and * parameters in the <code>serviceContext</code>) will be updated. * * <p>// w w w . j ava 2 s.c om * This method takes two file names, the <code>sourceFileName</code> and the * <code>title</code>. The <code>sourceFileName</code> corresponds to the * name of the actual file being uploaded. The <code>title</code> * corresponds to a name the client wishes to assign this file after it has * been uploaded to the portal. * </p> * * @param userId the primary key of the user * @param fileEntryId the primary key of the file entry * @param sourceFileName the original file's name (optionally * <code>null</code>) * @param mimeType the file's MIME type (optionally <code>null</code>) * @param title the new name to be assigned to the file (optionally <code> * <code>null</code></code>) * @param description the file's new description * @param changeLog the file's version change log (optionally * <code>null</code>) * @param majorVersion whether the new file version is a major version * @param bytes the file's data (optionally <code>null</code>) * @param serviceContext the service context to be applied. Can set the * asset category IDs, asset tag names, and expando bridge * attributes for the file entry. In a Liferay repository, it may * include: <ul> <li> fileEntryTypeId - ID for a custom file entry * type </li> <li> fieldsMap - mapping for fields associated with a * custom file entry type </li> </ul> * @return the file entry * @throws PortalException if the file entry could not be found * @throws SystemException if a system exception occurred */ public FileEntry updateFileEntry(long userId, long fileEntryId, String sourceFileName, String mimeType, String title, String description, String changeLog, boolean majorVersion, byte[] bytes, ServiceContext serviceContext) throws PortalException, SystemException { File file = null; try { if ((bytes != null) && (bytes.length > 0)) { file = FileUtil.createTempFile(bytes); } return updateFileEntry(userId, fileEntryId, sourceFileName, mimeType, title, description, changeLog, majorVersion, file, serviceContext); } catch (IOException ioe) { throw new SystemException("Unable to write temporary file", ioe); } finally { FileUtil.delete(file); } }
From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppServiceImpl.java
License:Open Source License
/** * Adds a file entry and associated metadata. It is created based on a byte * array./*w ww. j av a 2 s. c om*/ * * <p> * This method takes two file names, the <code>sourceFileName</code> and the * <code>title</code>. The <code>sourceFileName</code> corresponds to the * name of the actual file being uploaded. The <code>title</code> * corresponds to a name the client wishes to assign this file after it has * been uploaded to the portal. If it is <code>null</code>, the <code> * sourceFileName</code> will be used. * </p> * * @param repositoryId the primary key of the repository * @param folderId the primary key of the file entry's parent folder * @param sourceFileName the original file's name * @param mimeType the file's MIME type * @param title the name to be assigned to the file (optionally <code>null * </code>) * @param description the file's description * @param changeLog the file's version change log * @param bytes the file's data (optionally <code>null</code>) * @param serviceContext the service context to be applied. Can set the * asset category IDs, asset tag names, and expando bridge * attributes for the file entry. In a Liferay repository, it may * include: <ul> <li> fileEntryTypeId - ID for a custom file entry * type </li> <li> fieldsMap - mapping for fields associated with a * custom file entry type </li> </ul> * @return the file entry * @throws PortalException if the parent folder could not be found or if the * file entry's information was invalid * @throws SystemException if a system exception occurred */ public FileEntry addFileEntry(long repositoryId, long folderId, String sourceFileName, String mimeType, String title, String description, String changeLog, byte[] bytes, ServiceContext serviceContext) throws PortalException, SystemException { File file = null; try { if ((bytes != null) && (bytes.length > 0)) { file = FileUtil.createTempFile(bytes); } return addFileEntry(repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, file, serviceContext); } catch (IOException ioe) { throw new SystemException("Unable to write temporary file", ioe); } finally { FileUtil.delete(file); } }