use of org.craftercms.commons.security.permissions.annotations.ProtectedResourceId in project studio by craftercms.
the class AwsS3ServiceImpl method listItems.
/**
* {@inheritDoc}
*/
@Override
@HasPermission(type = DefaultPermission.class, action = "s3 read")
public List<S3Item> listItems(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") String siteId, @ValidateStringParam(name = "profileId") String profileId, @ValidateStringParam(name = "path") String path, @ValidateStringParam(name = "type") String type) throws AwsException {
S3Profile profile = getProfile(siteId, profileId);
AmazonS3 client = getS3Client(profile);
List<S3Item> items = new LinkedList<>();
Mimetypes mimetypes = Mimetypes.getInstance();
MimeType filerType = StringUtils.isEmpty(type) || StringUtils.equals(type, ITEM_FILTER) ? MimeTypeUtils.ALL : new MimeType(type);
String prefix = StringUtils.isEmpty(path) ? path : normalizePrefix(path);
ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(profile.getBucketName()).withPrefix(prefix).withDelimiter(delimiter);
ListObjectsV2Result result;
do {
result = client.listObjectsV2(request);
result.getCommonPrefixes().stream().map(p -> new S3Item(StringUtils.removeEnd(StringUtils.removeStart(p, prefix), delimiter), p, true)).forEach(items::add);
result.getObjectSummaries().stream().filter(o -> !StringUtils.equals(o.getKey(), prefix) && MimeType.valueOf(mimetypes.getMimetype(o.getKey())).isCompatibleWith(filerType)).map(o -> new S3Item(StringUtils.removeStart(o.getKey(), prefix), createUrl(profileId, o.getKey()), false)).forEach(items::add);
request.setContinuationToken(result.getNextContinuationToken());
} while (result.isTruncated());
return items;
}
use of org.craftercms.commons.security.permissions.annotations.ProtectedResourceId in project studio by craftercms.
the class WebDavServiceImpl method list.
/**
* {@inheritDoc}
*/
@Override
@ValidateParams
@HasPermission(type = DefaultPermission.class, action = "webdav_read")
public List<WebDavItem> list(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") final String siteId, @ValidateStringParam(name = "profileId") final String profileId, @ValidateStringParam(name = "path") final String path, @ValidateStringParam(name = "type") final String type) throws WebDavException {
WebDavProfile profile = getProfile(siteId, profileId);
String listPath = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
MimeType filterType;
try {
Sardine sardine = createClient(profile);
if (StringUtils.isEmpty(type) || type.equals(FILTER_ALL_ITEMS)) {
filterType = MimeType.valueOf(ALL_VALUE);
} else {
filterType = new MimeType(type);
}
if (StringUtils.isNotEmpty(path)) {
String[] tokens = StringUtils.split(path, "/");
for (String token : tokens) {
if (StringUtils.isNotEmpty(token)) {
listPath += StringUtils.appendIfMissing(UriUtils.encode(token, charset.name()), "/");
}
}
}
if (!sardine.exists(listPath)) {
logger.debug("Folder {0} doesn't exist", listPath);
return Collections.emptyList();
}
logger.debug("Listing resources at {0}", listPath);
List<DavResource> resources = sardine.list(listPath, 1, true);
logger.debug("Found {0} resources at {0}", resources.size(), listPath);
return resources.stream().skip(// to avoid repeating the folder being listed
1).filter(r -> r.isDirectory() || filterType.includes(MimeType.valueOf(r.getContentType()))).map(r -> new WebDavItem(getName(r), getUrl(r, profileId, profile), r.isDirectory())).collect(Collectors.toList());
} catch (Exception e) {
throw new WebDavException("Error listing resources", e);
}
}
Aggregations