Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ssssssss.magicapi.core.service;

import org.apache.commons.lang3.StringUtils;
import org.ssssssss.magicapi.core.resource.Resource;
import org.ssssssss.magicapi.core.model.*;
import org.ssssssss.magicapi.utils.PathUtils;
Expand All @@ -9,6 +10,8 @@
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* 资源存储服务
Expand Down Expand Up @@ -130,7 +133,10 @@ default String getScriptName(MagicEntity entity){
String fullName;
if(entity instanceof PathMagicEntity){
PathMagicEntity pme = (PathMagicEntity) entity;
fullName = String.format("/%s/%s(/%s/%s)", getGroupName(pme.getGroupId()), pme.getName(), getGroupPath(pme.getGroupId()), pme.getPath());
fullName = String.format("/%s/%s(/%s)", getGroupName(pme.getGroupId()), pme.getName(), Stream
.of(getGroupPath(pme.getGroupId()), pme.getPath())
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining("/")));
} else {
fullName = String.format("/%s/%s", getGroupName(entity.getGroupId()), entity.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import org.ssssssss.magicapi.core.service.AbstractPathMagicResourceStorage;
import org.ssssssss.magicapi.utils.PathUtils;

import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ApiInfoMagicResourceStorage extends AbstractPathMagicResourceStorage<ApiInfo> {

Expand All @@ -27,17 +28,27 @@ public Class<ApiInfo> magicClass() {

@Override
public String buildMappingKey(ApiInfo info, String path) {
return info.getMethod().toUpperCase() + ":" + super.buildMappingKey(info, path);
return info.getMethod().toUpperCase() + ":" + path;
}

@Override
public String buildMappingKey(ApiInfo info) {
return PathUtils.replaceSlash(buildMappingKey(info, this.prefix + Objects.toString(magicResourceService.getGroupPath(info.getGroupId()), "")));
String path = magicResourceService.getGroupPath(info.getGroupId());
path = Stream.of(this.prefix, path, info.getPath())
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining("/"));

// 若分组+接口路径拼接后都是空,则向上返回空
if (this.prefix.equals(path)) {
return null;
}

return PathUtils.replaceSlash(buildMappingKey(info, path));
}

@Override
public void validate(ApiInfo entity) {
notBlank(entity.getMethod(), REQUEST_METHOD_REQUIRED);
super.validate(entity);
notBlank(entity.getScript(), SCRIPT_REQUIRED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@MagicModule("magic")
public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstants {
Expand Down Expand Up @@ -78,7 +80,11 @@ private <T> T execute(RequestEntity requestEntity, PathMagicEntity info, Map<Str
MagicScriptContext scriptContext = new MagicScriptContext();
String fullGroupName = resourceService.getGroupName(info.getGroupId());
String fullGroupPath = resourceService.getGroupPath(info.getGroupId());
String scriptName = PathUtils.replaceSlash(String.format("/%s/%s(/%s/%s)", fullGroupName, info.getName(), fullGroupPath, info.getPath()));
String scriptName = PathUtils.replaceSlash(
String.format("/%s/%s(/%s)", fullGroupName, info.getName(), Stream
.of(fullGroupPath, info.getPath())
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining("/"))));
scriptContext.setScriptName(scriptName);
scriptContext.putMapIntoContext(context);
if (requestEntity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ public <T extends MagicEntity> boolean saveFile(T entity) {
}
// 路径检查
if (storage.requirePath()) {
notBlank(((PathMagicEntity) entity).getPath(), PATH_REQUIRED);
// FIXME
String newMappingKey = storage.buildKey(entity);
notBlank(newMappingKey, PATH_REQUIRED);
isTrue(pathCache.get(storage.folder()).entrySet().stream().noneMatch(entry -> entry.getValue().equals(newMappingKey) && !entry.getKey().equals(entity.getId())), PATH_CONFLICT);
}
storage.validate(entity);
Expand Down