Skip to content

Commit

Permalink
验证JSON (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
power721 authored Oct 1, 2024
1 parent fb518b8 commit 36720dc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/main/java/cn/har01d/alist_tvbox/service/ConfigFileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.har01d.alist_tvbox.entity.ConfigFileRepository;
import cn.har01d.alist_tvbox.exception.BadRequestException;
import cn.har01d.alist_tvbox.exception.NotFoundException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -25,10 +26,12 @@

public class ConfigFileService {
private final ConfigFileRepository repository;
private final ObjectMapper objectMapper;
private List<FileItem> labels = new ArrayList<>();

public ConfigFileService(ConfigFileRepository repository) {
public ConfigFileService(ConfigFileRepository repository, ObjectMapper objectMapper) {
this.repository = repository;
this.objectMapper = objectMapper;
}

@PostConstruct
Expand Down Expand Up @@ -115,7 +118,6 @@ private void readFile(String filepath) {
public ConfigFile create(FileDto dto) throws IOException {
validate(dto);
dto.setId(null);
dto.setPath(new File(dto.getDir(), dto.getName()).getAbsolutePath());
if (repository.existsByPath(dto.getPath())) {
throw new BadRequestException("文件已经存在");
}
Expand All @@ -133,6 +135,14 @@ private void validate(FileDto dto) {
if (StringUtils.isBlank(dto.getName())) {
throw new BadRequestException("文件名不能为空");
}
dto.setPath(new File(dto.getDir(), dto.getName()).getAbsolutePath());
if (dto.getName().endsWith(".json")) {
try {
objectMapper.readTree(dto.getContent());
} catch (IOException e) {
throw new BadRequestException("JSON格式错误", e);
}
}
}

private void writeFileContent(ConfigFile configFile) throws IOException {
Expand All @@ -156,7 +166,6 @@ public ConfigFile update(Integer id, FileDto dto) throws IOException {
}

dto.setId(id);
dto.setPath(new File(dto.getDir(), dto.getName()).getAbsolutePath());

ConfigFile other = repository.findByPath(dto.getPath());
if (other != null && !id.equals(other.getId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.har01d.alist_tvbox.entity.SubscriptionRepository;
import cn.har01d.alist_tvbox.exception.BadRequestException;
import cn.har01d.alist_tvbox.service.SubscriptionService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -14,6 +15,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
Expand All @@ -22,10 +24,12 @@
public class SubscriptionController {
private final SubscriptionRepository subscriptionRepository;
private final SubscriptionService subscriptionService;
private final ObjectMapper objectMapper;

public SubscriptionController(SubscriptionRepository subscriptionRepository, SubscriptionService subscriptionService) {
public SubscriptionController(SubscriptionRepository subscriptionRepository, SubscriptionService subscriptionService, ObjectMapper objectMapper) {
this.subscriptionRepository = subscriptionRepository;
this.subscriptionService = subscriptionService;
this.objectMapper = objectMapper;
}

@PostMapping
Expand All @@ -44,6 +48,13 @@ public Subscription save(@RequestBody Subscription subscription) {
throw new BadRequestException("订阅ID重复");
}
}
if (StringUtils.isNotBlank(subscription.getOverride())) {
try {
objectMapper.readTree(subscription.getOverride());
} catch (IOException e) {
throw new BadRequestException("JSON格式错误", e);
}
}
return subscriptionRepository.save(subscription);
}

Expand Down
13 changes: 11 additions & 2 deletions web-ui/src/views/FilesView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
<el-dialog v-model="formVisible" :fullscreen="fullscreen" :title="dialogTitle">
<el-form :model="form">
<el-form-item label="目录" label-width="120" required>
<el-select v-model="form.dir">
<el-option
v-for="item in options"
:key="item"
:label="item"
:value="item"
/>
</el-select>
<el-input v-model="form.dir" autocomplete="off"/>
</el-form-item>
<el-form-item label="名称" label-width="120" required>
Expand Down Expand Up @@ -87,18 +95,19 @@ const fullscreen = ref(false)
const form = ref({
id: 0,
name: '',
dir: '/www/tvbox',
dir: '/data',
path: '',
content: ''
})
const options = ['/data', '/www/tvbox', '/www/pg', '/www/zx']
const handleAdd = () => {
dialogTitle.value = '添加配置文件'
updateAction.value = false
form.value = {
id: 0,
name: '',
dir: '/www/tvbox',
dir: '/data',
path: '',
content: ''
}
Expand Down
2 changes: 1 addition & 1 deletion web-ui/src/views/SubscriptionsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ const load = () => {
axios.get('/api/subscriptions').then(({data}) => {
subscriptions.value = data
})
loadVersion()
}
const loadVersion = () => {
Expand All @@ -247,6 +246,7 @@ onMounted(() => {
axios.get('/api/token').then(({data}) => {
token.value = data ? '/' + (data + '').split(',')[0] : ''
load()
loadVersion()
})
})
</script>
Expand Down

0 comments on commit 36720dc

Please sign in to comment.