Skip to content

Commit

Permalink
#24738 save point
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Jun 12, 2023
1 parent c185692 commit 14972d6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.rest.api.v1.assets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.dotcms.contenttype.exception.NotFoundInDbException;
Expand Down Expand Up @@ -169,4 +170,41 @@ public void Test_Parse_Asset_Under_Root() throws DotDataException, DotSecurityEx
}


/**
* Given scenario: We request a ur with a non-existing folder
* Expected: Should resolve the resource and create the missing folder
* @throws DotDataException
* @throws DotSecurityException
*/
@Test
public void Test_Parse_Asset_Path_Create_Missing_Folder() throws DotDataException, DotSecurityException {
final String newFolder = String.format("foo%s",System.currentTimeMillis());

final Folder folderByPath = APILocator.getFolderAPI()
.findFolderByPath(newFolder, host, APILocator.systemUser(), false);
//Test Folder we intend to create does not exist

assertNull(folderByPath.getIdentifier());

final String url = String.format("http://%s/%s/bar.txt", host.getHostname(), newFolder);
final ResolvedAssetAndPath parse = AssetPathResolver.newInstance()
.resolve(url, APILocator.systemUser(), true);

assertNotNull(parse.path());
assertNotNull(parse.asset());

final String expectedFolderPath = String.format("/%s", newFolder);

assertEquals(host.getHostname(), parse.host());
assertEquals(expectedFolderPath, parse.path().replaceAll("/bar.txt",""));
assertEquals("bar.txt", parse.asset());

final Folder folderByPathAfter = APILocator.getFolderAPI()
.findFolderByPath(expectedFolderPath, host, APILocator.systemUser(), false);
//Test Folder we intend to create does not exist
assertNotNull(folderByPathAfter);
assertNotNull(folderByPathAfter.getIdentifier());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

import java.util.Optional;

/**
* Assets Request Form is a json representation of a request to get assets
* The assetsPath is the uri to the asset, it can be a folder or a file.
*/
@Value.Style(typeImmutable="*", typeAbstract="Abstract*")
@Value.Style(typeImmutable = "*", typeAbstract = "Abstract*")
@Value.Immutable
@JsonSerialize(as = AssetsRequestForm.class)
@JsonDeserialize(as = AssetsRequestForm.class)
Expand All @@ -18,9 +20,8 @@ public interface AbstractAssetsRequestForm {
String assetPath();

@JsonProperty("language")
String language();
Optional<String> language();

@JsonProperty("live")
Boolean live();

}
Optional<Boolean> live();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotcms.rest.api.v1.asset;

import static org.apache.commons.lang3.BooleanUtils.toBooleanDefaultIfNull;
import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;

import com.dotcms.browser.BrowserAPI;
Expand Down Expand Up @@ -271,6 +272,7 @@ FolderView toAssetsFolder(final Folder folder) {

public File getAssetContent(final AssetsRequestForm form, final User user)
throws DotDataException, DotSecurityException {

final String path = form.assetPath();
final ResolvedAssetAndPath assetAndPath = AssetPathResolver.newInstance()
.resolve(path, user);
Expand All @@ -296,38 +298,37 @@ public File getAssetContent(final AssetsRequestForm form, final User user)
.showImages(true)
.showContent(true);

if (null != form.language()) {
final Language language = languageAPI.getLanguage(form.language());
if (form.language().isPresent()) {
final Language language = lang(form.language().get());
builder.withLanguageId(language.getId());
}

if(null != form.live()){
final boolean live = BooleanUtils.toBoolean(form.live());
if (form.live().isPresent()) {
final boolean live = BooleanUtils.toBoolean(form.live().get());
builder.showWorking(!live);
}

//We're requesting an asset specifically therefore we need to find it and build the response
if(folder.isSystemFolder()){
builder.withHostOrFolderId(host.getIdentifier());
} else {
builder.withHostOrFolderId(folder.getInode());
}
builder.withFilter(assetName);
final List<Treeable> folderContent = browserAPI.getFolderContentList(builder.build());
assets = folderContent.stream().filter(Contentlet.class::isInstance).map(
Contentlet.class::cast).collect(Collectors.toList());
if (assets.isEmpty()) {

throw new NotFoundInDbException(
String.format(" Asset [%s] not found for lang [%s] and working/live state [%b] ",
assetName, defaultIfEmpty(form.language(), "unspecified"),
BooleanUtils.toString(form.live(), "live", "working", "unspecified"))
);
}
final Contentlet asset = assets.get(0);
final FileAsset fileAsset = fileAssetAPI.fromContentlet(asset);
return fileAsset.getFileAsset();

//We're requesting an asset specifically therefore we need to find it and build the response
if (folder.isSystemFolder()) {
builder.withHostOrFolderId(host.getIdentifier());
} else {
builder.withHostOrFolderId(folder.getInode());
}
builder.withFilter(assetName);
final List<Treeable> folderContent = browserAPI.getFolderContentList(builder.build());
assets = folderContent.stream().filter(Contentlet.class::isInstance).map(
Contentlet.class::cast).collect(Collectors.toList());
if (assets.isEmpty()) {

throw new NotFoundInDbException(
String.format(" Asset [%s] not found for lang [%s] and working/live state [%b] ",
assetName, form.language().orElse("unspecified"),
BooleanUtils.toString(form.live().get(), "live", "working", "unspecified"))
);
}
final Contentlet asset = assets.get(0);
final FileAsset fileAsset = fileAssetAPI.fromContentlet(asset);
return fileAsset.getFileAsset();
}

public WebAssetView saveUpdateAsset(final HttpServletRequest request, final FileUploadData form,
Expand All @@ -338,7 +339,7 @@ public WebAssetView saveUpdateAsset(final HttpServletRequest request, final File
final InputStream fileInputStream = form.getFileInputStream();
final FileUploadDetail detail = form.getDetail();
final String assetPath = detail.getAssetPath();
final boolean live = BooleanUtils.toBoolean((detail.getLive()));
final boolean live = toBooleanDefaultIfNull(detail.getLive(),true);
final Language lang = lang(detail.getLanguage());
final ResolvedAssetAndPath assetAndPath = AssetPathResolver.newInstance()
.resolve(assetPath, user, true);
Expand All @@ -360,7 +361,7 @@ public WebAssetView saveUpdateAsset(final HttpServletRequest request, final File
.withUser(user)
.showFiles(true)
.showFolders(true)
.showArchived(false)
.showArchived(true)
.showWorking(true)
.showLinks(false)
.showDotAssets(false)
Expand Down Expand Up @@ -399,8 +400,17 @@ public WebAssetView saveUpdateAsset(final HttpServletRequest request, final File

} else {
final Contentlet asset = found.get();
final Contentlet checkout = contentletAPI.checkout(asset.getInode(), user,
false);

if(asset.isArchived()){
contentletAPI.unarchive(asset, user, false);
}

if(asset.isLocked()){
contentletAPI.unlock(asset, user, false);
}

final Contentlet checkout = contentletAPI.checkout(asset.getInode(), user, false);

updateFileAsset(tempFile.file, folder, lang, checkout);
savedAsset = checkinOrPublish(checkout, user, live);
}
Expand All @@ -423,15 +433,16 @@ void disposeTempFile(final DotTempFile tempFile){
}
}

Contentlet checkinOrPublish(final Contentlet contentlet, User user, final boolean live) throws DotDataException, DotSecurityException {
Contentlet checkinOrPublish(final Contentlet checkout, User user, final boolean live) throws DotDataException, DotSecurityException {
if(live){
contentletAPI.publish(contentlet, user, false);
return contentlet;
contentletAPI.publish(checkout, user, false);
return checkout;
} else {
if(checkout.isLive()){
contentletAPI.unpublish(checkout, user, false);
}
}
return contentletAPI.checkinWithoutVersioning(contentlet,
(ContentletRelationships) null, null, null,
user, false);
// contentletAPI.checkin(contentlet, user, false);
return contentletAPI.checkin(checkout, user, false);
}

Contentlet makeFileAsset(final File file, final Folder folder, Language lang)
Expand All @@ -443,9 +454,9 @@ Contentlet makeFileAsset(final File file, final Folder folder, Language lang)


Contentlet updateFileAsset(final File file, final Folder folder, final Language lang, final Contentlet contentlet){
final String fileName = file.getName();
contentlet.setProperty(FileAssetAPI.TITLE_FIELD, fileName);
contentlet.setProperty(FileAssetAPI.FILE_NAME_FIELD, fileName);
final String name = file.getName();
contentlet.setProperty(FileAssetAPI.TITLE_FIELD, name);
contentlet.setProperty(FileAssetAPI.FILE_NAME_FIELD, name);
contentlet.setProperty(FileAssetAPI.BINARY_FIELD, file);
contentlet.setFolder(folder.getInode());
contentlet.setLanguageId(lang.getId());
Expand All @@ -457,7 +468,11 @@ Language lang(final String language) {
final String[] split = language.split("_", 2);
return languageAPI.getLanguage(split[0], split[1]);
}
).getOrElse(() -> languageAPI.getDefaultLanguage());
).getOrElse(() -> {
final Language defaultLanguage = languageAPI.getDefaultLanguage();
Logger.warn(this, String.format("Unable to get language from param [%s]. Defaulting to [%s]." , language, defaultLanguage));
return defaultLanguage;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public Response getAssetsInfo(@Context final HttpServletRequest request,
}


@Path("/")
@GET
@Path("/download")
@POST
@JSONP
@NoCache
@Produces({MediaType.APPLICATION_JSON, "application/javascript"})
public Response getAssetContent(@Context final HttpServletRequest request,
public Response download(@Context final HttpServletRequest request,
@Context final HttpServletResponse response,
AssetsRequestForm form
) throws DotSecurityException, DotDataException {
Expand Down Expand Up @@ -107,7 +107,7 @@ public Response saveUpdateAsset(
final User user = initDataObject.getUser();

final WebAssetView webAssetView = helper.saveUpdateAsset(request, form, user);
//Logger.info(this, String.format("User [%s] is requesting assets info for path [%s]", user.getUserId(), form.assetPath()));
Logger.info(this, String.format("User [%s] is uploading asset for path [%s]", user.getUserId(), form.getAssetPath()));
return Response.ok(new WebAssetEntityView(webAssetView)).build();
}

Expand Down

0 comments on commit 14972d6

Please sign in to comment.