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

Feature/#2718 mapping path static resources3 #2750

Merged
merged 7 commits into from
Nov 17, 2023
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
19 changes: 19 additions & 0 deletions WebContent/WEB-INF/jsp/systemSettings.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
$set("<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_LIMIT %>"/>", settings.<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_LIMIT %>"/>);
$set("<c:out value="<%= SystemSettingsDAO.THREADS_NAME_ADDITIONAL_LENGTH %>"/>", settings.<c:out value="<%= SystemSettingsDAO.THREADS_NAME_ADDITIONAL_LENGTH %>"/>);

$set("<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_GRAPHICS_PATH %>"/>", settings.<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_GRAPHICS_PATH %>"/>);
$set("<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_UPLOADS_PATH %>"/>", settings.<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_UPLOADS_PATH %>"/>);

setDisabled($("<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_ENABLED %>"/>"), !settings.<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ENABLED %>"/>);
setDisabled($("<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_LIMIT %>"/>"), !settings.<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ENABLED %>"/> || !settings.<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_ENABLED %>"/>);

Expand Down Expand Up @@ -303,6 +306,8 @@
$get("<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_ENABLED %>"/>"),
$get("<c:out value="<%= SystemSettingsDAO.WORK_ITEMS_REPORTING_ITEMS_PER_SECOND_LIMIT %>"/>"),
$get("<c:out value="<%= SystemSettingsDAO.THREADS_NAME_ADDITIONAL_LENGTH %>"/>"),
$get("<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_GRAPHICS_PATH %>"/>"),
$get("<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_UPLOADS_PATH %>"/>"),
function(response) {
stopImageFader("saveMiscSettingsImg");
if (response.hasMessages)
Expand Down Expand Up @@ -942,6 +947,20 @@
<input id="<c:out value="<%= SystemSettingsDAO.THREADS_NAME_ADDITIONAL_LENGTH %>"/>" type="number" class="formShort"/>
</td>
</tr>
<tr>
<td class="formLabelRequired"><fmt:message key="systemsettings.webresource.graphics.path"/></td>
<td class="formField">
<input id="<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_GRAPHICS_PATH %>"/>" type="text" class="formShort" style="width: 300px;"/>
</td>
<td colspan="2" id="uploadsPathMessage" class="formError"></td>
</tr>
<tr>
<td class="formLabelRequired"><fmt:message key="systemsettings.webresource.uploads.path"/></td>
<td class="formField">
<input id="<c:out value="<%= SystemSettingsDAO.WEB_RESOURCE_UPLOADS_PATH %>"/>" type="text" class="formShort" style="width: 300px;"/>
</td>
<td colspan="2" id="graphicsPathMessage" class="formError"></td>
</tr>
<tr>
<td colspan="2" id="miscMessage" class="formError"></td>
</tr>
Expand Down
5 changes: 3 additions & 2 deletions WebContent/WEB-INF/spring-security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
<intercept-url pattern="/logout.htm" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER', 'ROLE_PUBLIC')" />

<!-- Static resources -->
<intercept-url pattern="/graphics/**" access="permitAll" />
<intercept-url pattern="/images/**" access="permitAll" />
<intercept-url pattern="/img/**" access="permitAll" />
<intercept-url pattern="/assets/**" access="permitAll" />
Expand All @@ -143,7 +142,9 @@
<intercept-url pattern="/resources/**" access="permitAll" />
<intercept-url pattern="/static/**" access="permitAll" />
<intercept-url pattern="/audio/**" access="permitAll" />
<intercept-url pattern="/uploads/**" access="permitAll" />

<intercept-url pattern="/graphics/**" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER', 'ROLE_PUBLIC')" />
<intercept-url pattern="/uploads/**" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER', 'ROLE_PUBLIC')" />

<!-- Monitoring -->
<intercept-url pattern="/monitoring/**" access="permitAll" />
Expand Down
20 changes: 14 additions & 6 deletions WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@
<filter-name>CacheHeaders</filter-name>
<url-pattern>/resources/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheHeaders</filter-name>
<url-pattern>/uploads/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheHeaders</filter-name>
<url-pattern>/assets/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>monitoring</filter-name>
<url-pattern>/*</url-pattern>
Expand Down Expand Up @@ -273,15 +281,15 @@
<url-pattern>*.shtm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.bmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
Expand Down Expand Up @@ -313,15 +321,15 @@
<url-pattern>*.fla</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.jpeg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.svg</url-pattern>
</servlet-mapping>
<servlet-mapping>
Expand Down
6 changes: 5 additions & 1 deletion scadalts-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1059,5 +1059,9 @@
"systemsettings.workitems.reporting.enabled": "Work items reporting enabled",
"systemsettings.workitems.reporting.itemspersecond.enabled": "Items per second reporting enabled",
"systemsettings.workitems.reporting.itemspersecond.limit": "Items per second reporting limit",
"systemsettings.threads.name.additional.length": "Thread name length"
"systemsettings.threads.name.additional.length": "Thread name length",
"systemsettings.webresource.uploads.path": "Uploaded images save path",
"systemsettings.webresource.graphics.path": "Custom Graphics images path",
"systemsettings.webresource.uploads.path.wrong":"Uploaded images save path must end with 'uploads' or 'uploads{0}' ",
"systemsettings.webresource.graphics.path.wrong": "Custom Graphics images path must end with 'graphics' or 'graphics{0}' "
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,36 @@
dense
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="miscSettings.webResourceGraphicsPath"
:label="$t('systemsettings.webresource.graphics.path')"
@change="watchDataChange()"
:rules="[validateGraphicsPath]"
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="miscSettings.webResourceUploadsPath"
:label="$t('systemsettings.webresource.uploads.path')"
@change="watchDataChange()"
:rules="[validateUploadsPath]"
></v-text-field>
</v-col>
</v-row>
</v-card-text>
</v-card>
</v-col>
</template>
<script>
import path from "path";

export default {
name: 'MiscSettingsComponent',

data() {
return {
valid: [],
miscSettings: undefined,
miscSettingsStore: undefined,
isMiscSettingsEdited: false,
Expand Down Expand Up @@ -175,6 +194,7 @@ export default {
title: 'systemsettings.misc.title',
changed: changed,
data: this.sumarizeDataChanges(),
valid: this.validateForm(this.valid)
});
},

Expand Down Expand Up @@ -224,6 +244,36 @@ export default {
}
return value;
},
validateGraphicsPath(v) {
let validGraphicsKey = "validGraphics"
if(this.valid.some(item => item.key === validGraphicsKey)) {
this.valid = this.valid.filter(item => item.key !== validGraphicsKey);
}
if (v.endsWith('graphics') || v.endsWith('graphics' + path.sep)) {
this.valid.push({key: validGraphicsKey,value: true})
return true;
} else {
this.valid.push({key: validGraphicsKey,value: false})
return this.$t("systemsettings.webresource.graphics.path.wrong", {0: path.sep});
}
},
validateUploadsPath(v) {
let validUploadsKey = "validUploads"
if(this.valid.some(item => item.key === validUploadsKey)) {
this.valid = this.valid.filter(item => item.key !== validUploadsKey);
}
if (v.endsWith('uploads') || v.endsWith('uploads' + path.sep)) {
this.valid.push({key: validUploadsKey, value: true});
return true;
} else {
this.valid.push({key: validUploadsKey, value: false})
return this.$t("systemsettings.webresource.uploads.path.wrong", {0: path.sep});
}
},
validateForm(paths) {
const result = paths.every(item => item.value);
return result;
}
},
};
</script>
Expand Down
4 changes: 3 additions & 1 deletion scadalts-ui/src/views/System/SystemSettings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ export default {

async componentChanged(object) {
let idx = this.componentsEdited.findIndex((x) => x.component == object.component);
if (idx == -1 && object.changed) {
if(!object.valid) {
this.componentsEdited = [];
} else if (idx == -1 && object.changed) {
this.componentsEdited.push(object);
} else if (idx != -1 && !object.changed) {
this.componentsEdited.splice(idx, 1);
Expand Down
67 changes: 27 additions & 40 deletions src/br/org/scadabr/vo/exporter/ZIPProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -27,6 +28,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.scada_lts.utils.HttpParameterUtils;
import org.scada_lts.utils.UploadFileUtils;
import org.scada_lts.web.mvc.api.json.ExportConfig;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
Expand All @@ -40,7 +42,6 @@
import com.serotonin.mango.vo.User;
import com.serotonin.mango.web.dwr.EmportDwr;

import static org.scada_lts.utils.PathSecureUtils.getRealPath;
import static org.scada_lts.utils.PathSecureUtils.toSecurePath;
import static org.scada_lts.utils.UploadFileUtils.*;

Expand Down Expand Up @@ -91,12 +92,14 @@ public void exportProject(HttpServletRequest request,
tempFiles.add(buildJSONFile(JSON_FILE_NAME, includePointValues));

List<FileToPack> filesToZip = new ArrayList<>();
if (includeUploadsFolder)
filesToZip.addAll(getUploadsFolderFiles());

if (includeGraphicsFolder)
filesToZip.addAll(getGraphicsFolderFiles());

if (includeUploadsFolder) {
for(Path path: UploadFileUtils.getUploadsSystemFilePaths())
filesToZip.addAll(getUploadsFolderFiles(path));
}
if (includeGraphicsFolder) {
for(Path path: UploadFileUtils.getGraphicsSystemFilePaths())
filesToZip.addAll(getGraphicsFolderFiles(path));
}
filesToZip.addAll(tempFiles);

ServletOutputStream out = response.getOutputStream();
Expand Down Expand Up @@ -157,11 +160,11 @@ public ModelAndView setupToImportProject(HttpServletRequest request,

public void importProject() throws Exception {

List<ZipEntry> graphicsFiles = getGraphicsFiles();
restoreFiles(graphicsFiles);
List<ZipEntry> graphicsFiles = getGraphicsFiles(graphicsFolder);
restoreFiles(graphicsFiles, getGraphicsBaseSystemFilePath(getGraphicsSystemFileToWritePath()));

List<ZipEntry> uploadFiles = getUploadFiles();
restoreFiles(uploadFiles);
List<ZipEntry> uploadFiles = getUploadFiles(uploadsFolder);
restoreFiles(uploadFiles, getUploadsBaseSystemFilePath(getUploadsSystemFileToWritePath()));

String jsonContent = getJsonContent();

Expand All @@ -170,9 +173,7 @@ public void importProject() throws Exception {

}

private void restoreFiles(List<ZipEntry> uploadFiles) {
String appPath = getRealPath();

private void restoreFiles(List<ZipEntry> uploadFiles, Path appPath) {
for (ZipEntry zipEntry : uploadFiles) {
String entryName = zipEntry.getName();
if(!entryName.isEmpty()) {
Expand Down Expand Up @@ -208,11 +209,11 @@ private void writeToFile(ZipEntry zipEntry, File file) {
}
}

private List<ZipEntry> getUploadFiles() {
private List<ZipEntry> getUploadFiles(String uploadsFolder) {
return filteringUploadFiles(filterZipFiles(uploadsFolder), zipFile);
}

private List<ZipEntry> getGraphicsFiles() {
private List<ZipEntry> getGraphicsFiles(String graphicsFolder) {
return filteringGraphicsFiles(filterZipFiles(graphicsFolder), zipFile);
}

Expand Down Expand Up @@ -241,41 +242,27 @@ private FileToPack buildJSONFile(String packAs, boolean includePointValues) {
return file;
}

private List<FileToPack> getUploadsFolderFiles() {
String uploadFolder = Common.ctx.getServletContext().getRealPath(
FILE_SEPARATOR)
+ "uploads";

private List<FileToPack> getUploadsFolderFiles(Path uploadFolder) {
List<File> files = filteringUploadFiles(FileUtil.getFilesOnDirectory(uploadFolder));

List<FileToPack> pack = new ArrayList<FileToPack>();
for (File file : files) {

String filePartialPath = uploadsFolder + file.getName();
List<FileToPack> pack = new ArrayList<>();

pack.add(new FileToPack(filePartialPath.substring(0, 7)
+ FILE_SEPARATOR + filePartialPath.substring(8), file));
for (File file : files) {
String[] uploadsBasePath = file.getAbsolutePath().split("uploads");
String filePartialPath = "uploads" + uploadsBasePath[1];
pack.add(new FileToPack(filePartialPath, file));
}
return pack;
}

private List<FileToPack> getGraphicsFolderFiles() {
String graphicFolder = Common.ctx.getServletContext().getRealPath(
FILE_SEPARATOR)
+ "graphics";

private List<FileToPack> getGraphicsFolderFiles(Path graphicFolder) {
List<File> files = filteringGraphicsFiles(FileUtil.getFilesOnDirectory(graphicFolder));

List<FileToPack> pack = new ArrayList<FileToPack>();
List<FileToPack> pack = new ArrayList<>();

for (File file : files) {

String[] pathDivided = null;

pathDivided = file.getAbsolutePath().split("graphics");

String filePartialPath = "graphics" + pathDivided[1];

String[] uploadsBasePath = file.getAbsolutePath().split("graphics");
String filePartialPath = "graphics" + uploadsBasePath[1];
pack.add(new FileToPack(filePartialPath, file));
}

Expand Down
16 changes: 10 additions & 6 deletions src/br/org/scadabr/vo/exporter/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -65,18 +67,20 @@ public static File createTxtTempFile(String text) {
return file;
}

public static List<File> getFilesOnDirectory(String directoryName) {
List<File> files = new ArrayList<File>();
public static List<File> getFilesOnDirectory(Path directoryName) {
List<File> files = new ArrayList<>();
try {
File directory = new File(directoryName);
File directory = directoryName.toFile();
if (directory.exists()) {

if (directory.isDirectory()) {
String[] filesOnDirectory = directory.list();

for (String fileName : filesOnDirectory) {
files.addAll(getFilesOnDirectory(directory
.getAbsolutePath() + FILE_SEPARATOR + fileName));
if(filesOnDirectory != null) {
for (String fileName : filesOnDirectory) {
files.addAll(getFilesOnDirectory(Paths.get(directory
.getAbsolutePath() + FILE_SEPARATOR + fileName)));
}
}
} else if (directory.isFile()) {
files.add(directory);
Expand Down
Loading
Loading