Skip to content

Commit

Permalink
#24738 file asset download endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Jun 8, 2023
1 parent 8df0fe9 commit c185692
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
public interface AbstractAssetsRequestForm {
@JsonProperty("assetPath")
String assetPath();

@JsonProperty("language")
String language();

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dotcms.rest.api.v1.asset;

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

import com.dotcms.browser.BrowserAPI;
import com.dotcms.browser.BrowserQuery;
import com.dotcms.browser.BrowserQuery.Builder;
Expand All @@ -24,7 +26,6 @@
import com.dotmarketing.portlets.languagesmanager.business.LanguageAPI;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.portlets.structure.model.ContentletRelationships;
import com.dotmarketing.util.FileUtil;
import com.dotmarketing.util.Logger;
import com.liferay.portal.model.User;
import io.vavr.control.Try;
Expand All @@ -34,7 +35,6 @@
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -108,7 +108,7 @@ public class WebAssetHelper {
* @throws DotDataException
* @throws DotSecurityException
*/
public WebAssetView getAsset(final String path, final User user)
public WebAssetView getAssetInfo(final String path, final User user)
throws DotDataException, DotSecurityException {

final ResolvedAssetAndPath assetAndPath = AssetPathResolver.newInstance()
Expand Down Expand Up @@ -269,6 +269,67 @@ FolderView toAssetsFolder(final Folder folder) {
.build();
}

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);
final Host host = assetAndPath.resolvedHost();
final Folder folder = assetAndPath.resolvedFolder();
final String assetName = assetAndPath.asset();

if (null == assetName) {
throw new IllegalArgumentException("Unspecified Asset name.");
}

final List<Contentlet> assets;

final Builder builder = BrowserQuery.builder();
builder.showDotAssets(false)
.withUser(user)
.showFiles(true)
.showFolders(true)
.showArchived(false)
.showWorking(true)
.showLinks(false)
.showDotAssets(false)
.showImages(true)
.showContent(true);

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

if(null != form.live()){
final boolean live = BooleanUtils.toBoolean(form.live());
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();

}

public WebAssetView saveUpdateAsset(final HttpServletRequest request, final FileUploadData form,
final User user) throws DotDataException, DotSecurityException, IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.Logger;
import com.liferay.portal.model.User;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -53,11 +55,37 @@ public Response getAssetsInfo(@Context final HttpServletRequest request,
Logger.info(this,
String.format("User [%s] is requesting assets info for path [%s]", user.getUserId(),
form.assetPath()));
final WebAssetView asset = helper.getAsset(form.assetPath(), user);
final WebAssetView asset = helper.getAssetInfo(form.assetPath(), user);
return Response.ok(new WebAssetEntityView(asset)).build();
}


@Path("/")
@GET
@JSONP
@NoCache
@Produces({MediaType.APPLICATION_JSON, "application/javascript"})
public Response getAssetContent(@Context final HttpServletRequest request,
@Context final HttpServletResponse response,
AssetsRequestForm form
) throws DotSecurityException, DotDataException {

final InitDataObject initDataObject = new WebResource.InitBuilder()
.requiredBackendUser(true)
.requiredFrontendUser(false)
.requestAndResponse(request, response)
.rejectWhenNoUser(true).init();

final User user = initDataObject.getUser();
Logger.info(this,
String.format("User [%s] is requesting asset content for download for path [%s]", user.getUserId(), form.assetPath()));
final File file = helper.getAssetContent(form, user);
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"")
.build();
}


@Path("/")
@PUT
@JSONP
Expand Down

0 comments on commit c185692

Please sign in to comment.