Skip to content

Commit

Permalink
#29079 include in 23.10.24 LTS
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Aug 14, 2024
1 parent 0690879 commit b76ef2c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
3 changes: 2 additions & 1 deletion dotCMS/hotfix_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<String>> 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();
}
}
}
}
16 changes: 12 additions & 4 deletions dotCMS/src/main/java/com/dotmarketing/util/ImportUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b76ef2c

Please sign in to comment.