Skip to content

Commit

Permalink
#24738 pusg files
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Jun 5, 2023
1 parent db7c977 commit f0fea21
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dotcms.rest.api.v1.asset;

import static com.liferay.util.StringPool.BLANK;
import static com.liferay.util.StringPool.DOUBLE_SLASH;
import static com.liferay.util.StringPool.FORWARD_SLASH;

import com.dotcms.contenttype.exception.NotFoundInDbException;
Expand Down Expand Up @@ -51,6 +50,20 @@ public class AssetPathResolver {
* @throws DotSecurityException
*/
public ResolvedAssetAndPath resolve(final String url, final User user)
throws DotDataException, DotSecurityException{
return resolve(url, user, false);
}

/**
* Main entry point for resolving a path to an asset. This method will attempt to resolve the path to a folder or
* @param url
* @param user
* @param createMissingFolders
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
public ResolvedAssetAndPath resolve(final String url, final User user, final boolean createMissingFolders)
throws DotDataException, DotSecurityException {

try {
Expand All @@ -68,10 +81,10 @@ public ResolvedAssetAndPath resolve(final String url, final User user)
}

//This line determines if the path is a folder
final Optional<Folder> folder = resolveFolder(path, host, user);
final Optional<Folder> folder = resolveExistingFolder(path, host, user);
if(folder.isEmpty()){
//if we've got this far we need to verify if the path is an asset. The folder will be expected to be the parent folder
Optional<FolderAndAsset> folderAndAsset = resolveAssetAndFolder(uri.getPath(), host, user);
Optional<FolderAndAsset> folderAndAsset = resolveAssetAndFolder(uri.getPath(), host, user, createMissingFolders);
if(folderAndAsset.isEmpty()){
throw new NotFoundInDbException(String.format("Unable to determine a valid folder or asset from uri: [%s].", url));
}
Expand Down Expand Up @@ -129,7 +142,7 @@ Optional<Host> resolveHosBytName(final String hostName, final User user) throws
* @throws DotDataException
* @throws DotSecurityException
*/
Optional<Folder> resolveFolder(final String rawPath, final Host host, final User user) throws DotDataException, DotSecurityException {
Optional<Folder> resolveExistingFolder(final String rawPath, final Host host, final User user) throws DotDataException, DotSecurityException {
Preconditions.checkNotEmpty(rawPath,IllegalArgumentException.class, String.format("Failed determining path from [%s].", rawPath));
String path = BLANK.equals(rawPath) ? FORWARD_SLASH : rawPath;
path = !path.endsWith(FORWARD_SLASH) ? path + FORWARD_SLASH : path;
Expand All @@ -151,7 +164,7 @@ Optional<Folder> resolveFolder(final String rawPath, final Host host, final User
* @throws DotDataException
* @throws DotSecurityException
*/
Optional<FolderAndAsset> resolveAssetAndFolder(final String rawPath, final Host host, final User user)
Optional<FolderAndAsset> resolveAssetAndFolder(final String rawPath, final Host host, final User user, final boolean createMissingFolder)
throws DotDataException, DotSecurityException {
final String startsWithForwardSlash = "^\\/[a-zA-Z0-9\\.\\-]+$";
// if our path starts with / followed by a string then we're looking at file asset in the root folder
Expand All @@ -169,14 +182,22 @@ Optional<FolderAndAsset> resolveAssetAndFolder(final String rawPath, final Host

final String parentFolderPath = Try.of(()-> rawPath.substring(0, index)).getOrNull();
final String assetName = Try.of(()->rawPath.substring(index + 1)).getOrNull();
final Folder parentFolder = folderAPI.findFolderByPath(parentFolderPath + FORWARD_SLASH, host, user, false);
final String folderPath = parentFolderPath + FORWARD_SLASH;
final Folder parentFolder = folderAPI.findFolderByPath(folderPath, host, user, false);

if (null != parentFolder && UtilMethods.isSet(parentFolder.getInode())) {
return Optional.of(
FolderAndAsset.builder().folder(parentFolder).asset(assetName).build()
);
}

if(createMissingFolder){
final Folder folder = folderAPI.createFolders(folderPath, host, user, false);
return Optional.of(
FolderAndAsset.builder().folder(folder).asset(assetName).build()
);
}

return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.dotmarketing.business.Treeable;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAsset;
import com.dotmarketing.portlets.fileassets.business.FileAssetAPI;
Expand All @@ -24,8 +25,12 @@
import io.vavr.control.Try;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -40,19 +45,25 @@ public class WebAssetHelper {

FileAssetAPI fileAssetAPI;

ContentletAPI contentletAPI;

BrowserAPI browserAPI;

/**
* Constructor for testing
* @param languageAPI
* @param fileAssetAPI
* @param contentletAPI
* @param browserAPI
*/
WebAssetHelper(
final LanguageAPI languageAPI,
final FileAssetAPI fileAssetAPI, final BrowserAPI browserAPI) {
final FileAssetAPI fileAssetAPI,
final ContentletAPI contentletAPI,
final BrowserAPI browserAPI) {
this.languageAPI = languageAPI;
this.fileAssetAPI = fileAssetAPI;
this.contentletAPI = contentletAPI;
this.browserAPI = browserAPI;
}

Expand All @@ -63,6 +74,7 @@ public class WebAssetHelper {
this(
APILocator.getLanguageAPI(),
APILocator.getFileAssetAPI(),
APILocator.getContentletAPI(),
APILocator.getBrowserAPI()
);
}
Expand Down Expand Up @@ -237,12 +249,45 @@ FolderView toAssetsFolder(final Folder folder) {
.build();
}

public void createOrReplaceAsset(final String assetPath, final String fileName, final InputStream fileInputStream, final User user){
System.out.println(assetPath);
System.out.println(fileName);
System.out.println(fileInputStream);
public void createOrReplaceAsset(final String rawPath, final String fileName, final InputStream fileInputStream, final User user)
throws DotDataException, DotSecurityException, IOException {

final ResolvedAssetAndPath assetAndPath = AssetPathResolver.newInstance()
.resolve(rawPath, user, true);

final Host host = assetAndPath.resolvedHost();
final Folder folder = assetAndPath.resolvedFolder();
final String assetName = assetAndPath.asset();

System.out.println(host);
System.out.println(folder);
System.out.println(assetName);

if(null != fileInputStream) {
//TODO validate size length

final Path tempFile = Files.createTempFile(assetName, "temp");

Files.copy(fileInputStream, tempFile);

final Contentlet contentlet = makeFileAsset(tempFile, folder);
final Contentlet savedContentlet = contentletAPI.checkin(contentlet, user, false);

}
}

Contentlet makeFileAsset(final Path filePath, final Folder folder){
final File file = filePath.toFile();
final Contentlet contentlet = new Contentlet();
final String fileName = file.getName();
contentlet.setProperty(FileAssetAPI.TITLE_FIELD, fileName);
contentlet.setProperty(FileAssetAPI.FILE_NAME_FIELD, fileName);
contentlet.setProperty(FileAssetAPI.BINARY_FIELD, file);
contentlet.setFolder(folder.getInode());
return contentlet;
}



/**
* Creates a new instance of {@link WebAssetHelper}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ public Response putAsset(
.rejectWhenNoUser(true).init();

final User user = initDataObject.getUser();
/*
String fileName = form.contentDispositionHeader().getFileName();
String assetPath = form.assetPath();
InputStream fileInputStream = form.fileInputStream();
final String fileName = contentDispositionHeader == null ? null : contentDispositionHeader.getFileName();
helper.createOrReplaceAsset(assetPath, fileName, fileInputStream, user);
*/

//Logger.info(this, String.format("User [%s] is requesting assets info for path [%s]", user.getUserId(), form.assetPath()));

return Response.ok(new WebAssetEntityView(null)).build();
Expand Down

0 comments on commit f0fea21

Please sign in to comment.