diff --git a/dotCMS/hotfix_tracking.md b/dotCMS/hotfix_tracking.md index b630c84d8f06..85d8bca8833d 100644 --- a/dotCMS/hotfix_tracking.md +++ b/dotCMS/hotfix_tracking.md @@ -139,4 +139,5 @@ This maintenance release includes the following code fixes: 132. https://github.com/dotCMS/core/issues/28163 : 'alive' and 'startup' healthcheck APIs return 503 on seemingly healthy app #28163 133. https://github.com/dotCMS/core/issues/26546 : Enable better logging for getPageByPath in HTMLPageAssetAPIImpl.java #26546 134. https://github.com/dotCMS/core/issues/28366 : Uploaded images in another language than the default one do not inherit permissions #28366 -135. https://github.com/dotCMS/core/issues/28838 : Category Child Permissions Not Loading #28838 \ No newline at end of file +135. https://github.com/dotCMS/core/issues/28838 : Category Child Permissions Not Loading #28838 +136. https://github.com/dotCMS/core/issues/29079 : fileAsset Required Error while importing FileAsset through CSV #29079 \ No newline at end of file diff --git a/dotCMS/src/integration-test/java/com/dotcms/util/ImportUtilTest.java b/dotCMS/src/integration-test/java/com/dotcms/util/ImportUtilTest.java index 8bc38ded49cb..e5273c8f2eaf 100644 --- a/dotCMS/src/integration-test/java/com/dotcms/util/ImportUtilTest.java +++ b/dotCMS/src/integration-test/java/com/dotcms/util/ImportUtilTest.java @@ -2598,4 +2598,69 @@ public void importFile_PagesWithURL_success() } } + + /** + * Method to test: This test tries the {@link ImportUtil#importFile} + * Given Scenario: A Content type which has a binary field, the binary field is required. Will try to preview the import of a CSV with a URL pointing to a valid image. + * ExpectedResult: The importer should return without errors, so content will be ready to be imported. + */ + @Test + public void importFile_importBinaryFieldUsingURLWithRequiredBinaryField_success() + throws DotSecurityException, DotDataException, IOException { + + ContentType contentType = null; + CsvReader csvreader; + long time; + HashMap> results; + Reader reader; + String[] csvHeaders; + com.dotcms.contenttype.model.field.Field titleField; + + try { + time = System.currentTimeMillis(); + final String contentTypeName = "ContentTypeBinaryField" + time; + final String contentTypeVarName = "contentTypeBinaryField" + time; + + //create content type + contentType = createTestContentType(contentTypeName, contentTypeVarName); + titleField = fieldAPI.byContentTypeAndVar(contentType, TITLE_FIELD_NAME); + + final com.dotcms.contenttype.model.field.Field binaryField = FieldBuilder.builder(BinaryField.class).name(BINARY_FIELD_NAME) + .contentTypeId(contentType.id()).variable(BINARY_FIELD_NAME).sortOrder(1).required(true) + .build(); + fieldAPI.save(binaryField,user); + + //Creating csv + reader = createTempFile(TITLE_FIELD_NAME + ", " + BODY_FIELD_NAME + ", " + BINARY_FIELD_NAME + "\r\n" + + "test1" + time + ", " + + "test1" + time + ", " + + "https://raw.githubusercontent.com/dotCMS/core/master/dotCMS/src/main/webapp/html/images/skin/logo.gif"); + csvreader = new CsvReader(reader); + csvreader.setSafetySwitch(false); + csvHeaders = csvreader.getHeaders(); + + results = + ImportUtil + .importFile(0L, defaultSite.getInode(), contentType.inode(), + new String[]{titleField.id()}, true, false, + user, defaultLanguage.getId(), csvHeaders, csvreader, -1, + -1, reader, + saveAsDraftAction.getId(), getHttpRequest()); + + //Validations + validate(results, true, false, false); + + assertEquals(results.get("warnings").size(), 0); + assertEquals(results.get("errors").size(), 0); + + } finally { + try { + if (null != contentType) { + contentTypeApi.delete(contentType); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } } diff --git a/dotCMS/src/main/java/com/dotmarketing/util/ImportUtil.java b/dotCMS/src/main/java/com/dotmarketing/util/ImportUtil.java index 84d722e411b6..1e8fb669517d 100644 --- a/dotCMS/src/main/java/com/dotmarketing/util/ImportUtil.java +++ b/dotCMS/src/main/java/com/dotmarketing/util/ImportUtil.java @@ -57,6 +57,8 @@ import com.liferay.portal.language.LanguageUtil; import com.liferay.portal.model.User; import com.liferay.util.StringPool; + +import java.io.File; import java.io.IOException; import java.io.Reader; import java.io.StringWriter; @@ -1270,10 +1272,16 @@ can manage multilingual inserts for already stored records but not for the case } } try{ - if (new LegacyFieldTransformer(field).from().typeName().equals(BinaryField.class.getName()) && !preview){ - if(null != value && UtilMethods.isSet(value.toString())){ - final DotTempFile tempFile = APILocator.getTempFileAPI().createTempFileFromUrl(null,request,new URL(value.toString()),-1,-1); - cont.setBinary(field.getVelocityVarName(), tempFile.file); + if (new LegacyFieldTransformer(field).from().typeName().equals(BinaryField.class.getName())){ + if(preview){ + //To avoid creating temp files for preview, we just create a dummy file and assign it to the contentlet + final File dummyFile = File.createTempFile("dummy", ".txt", new File(ConfigUtils.getAssetTempPath())); + cont.setBinary(field.getVelocityVarName(), dummyFile); + } else { + if (null != value && UtilMethods.isSet(value.toString())) { + final DotTempFile tempFile = APILocator.getTempFileAPI().createTempFileFromUrl(null, request, new URL(value.toString()), -1,1000L); + cont.setBinary(field.getVelocityVarName(), tempFile.file); + } } } else { conAPI.setContentletProperty(cont, field, value);