Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds files always directly under /conf/apm/scripts bug #292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/aem/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ aem {
jar {
bundle {
exportPackage("com.cognifide.apm.core.*")
importPackage("javax.annotation;version=0.0.0")
attribute("Sling-Model-Packages",
listOf(
"com.cognifide.apm.core.endpoints",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
@RequiredArgsConstructor
class FileDescriptor {

private static final String SCRIPT_PATH = "/conf/apm/scripts";

private final String path;

private final String name;

private final InputStream inputStream;

public static FileDescriptor createFileDescriptor(String originalFileName, String savePath, InputStream inputStream) {
Expand All @@ -40,20 +44,28 @@ public static FileDescriptor createFileDescriptor(String originalFileName, Strin
}

private static String getPathFromOriginalFileName(String savePath, String originalFileName) {
String path = savePath;
if (originalFileName.contains("/")) {
String subPath = StringUtils.substringBeforeLast(originalFileName, "/");
if (subPath.startsWith(savePath)) {
subPath = StringUtils.substringAfter(subPath, savePath);
}
return savePath + (subPath.startsWith("/") ? subPath : "/" + subPath);
if (!subPath.isEmpty()) {
path = savePath + (subPath.startsWith("/") ? "" : "/") + subPath;
}
}
if (!path.startsWith(SCRIPT_PATH)) {
path = SCRIPT_PATH + (path.startsWith("/") ? "" : "/") + path;
}
return savePath;
return path;
}

private static String getFileNameFromOriginalFileName(String originalFileName) {
String fileName = originalFileName;
if (originalFileName.contains("/")) {
return StringUtils.substringAfterLast(originalFileName, "/");
fileName = StringUtils.substringAfterLast(originalFileName, "/");
}
return originalFileName;
return fileName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public final class ScriptNode {

public static final String APM_SCRIPT = "apm:Script";

public static final String APM_SAVE_PATH = "apm:savePath";

public static final String APM_LAUNCH_ENABLED = "apm:launchEnabled";

public static final String APM_LAUNCH_MODE = "apm:launchMode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,27 +20,21 @@
package com.cognifide.apm.core.scripts;

import com.cognifide.apm.api.scripts.Script;
import java.io.InputStream;
import com.cognifide.apm.core.endpoints.ScriptUploadForm;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;

public interface ScriptStorage {

/**
* Get scripts path depending on instance type
*/
String getSavePath();
/**
* Remove script
*/
void remove(Script script, ResourceResolver resolver) throws RepositoryException;

/**
* Remove script
*/
void remove(Script script, ResourceResolver resolver) throws RepositoryException;

/**
* Save script, for example from upload
*/
Script save(String fileName, InputStream input, LaunchMetadata launchMetadata, boolean overwrite,
ResourceResolver resolver) throws RepositoryException, PersistenceException;
/**
* Save script, for example from upload
*/
Script save(ScriptUploadForm form, ResourceResolver resolver) throws RepositoryException, PersistenceException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import com.cognifide.apm.api.services.ScriptFinder;
import com.cognifide.apm.core.Apm;
import com.cognifide.apm.core.Property;
import com.cognifide.apm.core.endpoints.ScriptUploadForm;
import com.day.cq.commons.jcr.JcrConstants;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -67,8 +67,6 @@ public class ScriptStorageImpl implements ScriptStorage {

private static final Pattern PATH_PATTERN = Pattern.compile("/[\\da-zA-Z\\-/]+");

private static final String SCRIPT_PATH = "/conf/apm/scripts";

private static final Charset SCRIPT_ENCODING = StandardCharsets.UTF_8;

@Reference
Expand All @@ -85,23 +83,14 @@ public void remove(final Script script, ResourceResolver resolver) throws Reposi
}

@Override
public Script save(String fileName, InputStream input, LaunchMetadata launchMetadata, boolean overwrite,
ResourceResolver resolver) throws RepositoryException, PersistenceException {

FileDescriptor fileDescriptor = FileDescriptor.createFileDescriptor(fileName, getSavePath(), input);

public Script save(ScriptUploadForm form, ResourceResolver resolver) throws RepositoryException, PersistenceException {
FileDescriptor fileDescriptor = FileDescriptor.createFileDescriptor(form.getFileName(), form.getSavePath(), form.getFile());
validate(Collections.singletonList(fileDescriptor));

return saveScript(fileDescriptor, launchMetadata, overwrite, resolver);
}

@Override
public String getSavePath() {
return SCRIPT_PATH;
return saveScript(fileDescriptor, form.toLaunchMetadata(), form.getOverwrite(), resolver);
}

private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetadata, boolean overwrite,
ResourceResolver resolver) {
ResourceResolver resolver) {
Script result = null;
try {
final Session session = resolver.adaptTo(Session.class);
Expand Down Expand Up @@ -185,9 +174,10 @@ private List<String> validate(FileDescriptor file) {
}

private static void ensurePropertyMatchesPattern(List<String> errors, String property, String value,
Pattern pattern) {
Pattern pattern) {
if (!pattern.matcher(value).matches()) {
errors.add(format("Invalid %s: \"%s\"", property, value));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ScriptUploadForm @Inject constructor(
@param:RequestParameter("file", optional = false) val file: InputStream,
@param:RequestParameter("file", optional = false) @param:FileName val fileName: String,
@param:RequestParameter("overwrite") val overwrite: Boolean,
@param:RequestParameter(ScriptNode.APM_SAVE_PATH) val savePath: String?,
@param:RequestParameter(ScriptNode.APM_LAUNCH_ENABLED) val launchEnabled: Boolean,
@param:RequestParameter(ScriptNode.APM_LAUNCH_MODE) val launchMode: LaunchMode,
@param:RequestParameter(ScriptNode.APM_LAUNCH_ENVIRONMENT) val launchEnvironment: LaunchEnvironment?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ScriptUploadServlet : AbstractFormServlet<ScriptUploadForm>(ScriptUploadFo

override fun doPost(form: ScriptUploadForm, resourceResolver: ResourceResolver): ResponseEntity<Any> {
return try {
val script = scriptStorage.save(form.fileName, form.file, form.toLaunchMetadata(), form.overwrite, resourceResolver)
val script = scriptStorage.save(form, resourceResolver)
scriptManager.process(script, ExecutionMode.VALIDATION, resourceResolver)
ok {
message = "File successfully saved"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
this.$saveAndCloseButton = this.$el.find('#saveAndCloseButton').eq(0);
this.$logger = this.$el.find('.apm-console-logger');
this.initialValue = this.$textArea.val();
this.savePath = this.$el.find('#script-form').attr('action');
this.formAction = this.$el.find('#script-form').attr('action');
this.editor = this.initEditor();
this.delegateEvents();
}
Expand All @@ -73,14 +73,16 @@
getFileName: function () {
return this.$fileName.val() + '.apm';
},
getFullPath: function () {
getSavePath: function () {
let fileName = this.getFileName();
if (this.savePath.endsWith('/' + fileName)) {
fileName = this.savePath;
} else {
fileName = this.savePath + '/' + fileName;
let savePath = this.formAction;
if (savePath.endsWith('/' + fileName)) {
savePath = savePath.split('/' + fileName)[0];
}
return fileName;
return savePath;
},
getFullPath: function () {
return this.getSavePath() + '/' + this.getFileName();
},
getOverwrite: function () {
return this.isFileNameLocked() ? 'true' : 'false';
Expand All @@ -104,6 +106,7 @@
formData.set('apm:launchEnabled', this.getLaunchEnabled());
formData.set('overwrite', this.getOverwrite());
formData.set('file', new Blob([value], {type: 'text/plain'}), this.getFullPath());
formData.set('apm:savePath', this.getSavePath());

$.ajax({
type: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@
fileUploader
.on('coral-fileupload:fileadded', function(event) {
let filename = event.detail.item.file.name;
let savePath = window.location.pathname.split('.html')[1];
event.detail.item._parameters = [
{ name: "apm:launchEnabled", value: true },
{ name: "apm:launchMode", value: "ON_DEMAND" },
{name: 'apm:launchEnabled', value: true},
{name: 'apm:launchMode', value: 'ON_DEMAND'},
{name: 'apm:savePath', value: savePath},
];
fileUploader.upload(filename);
})
.on('coral-fileupload:load', function(event) {
let savePath = window.location.pathname.split('.html')[1];
fileUploader.uploadQueue.forEach(function(item, index) {
let filename = event.detail.item.file.name;
if (item.file.name === filename) {
item._parameters = [
{name: 'apm:savePath', value: savePath},
];
fileUploader.uploadQueue.splice(index, 1);
}
});
Expand Down