IMPROVE input validation
This commit is contained in:
@@ -18,7 +18,7 @@ public class Job {
|
|||||||
private ClipConfig clipConfig;
|
private ClipConfig clipConfig;
|
||||||
|
|
||||||
// job status
|
// job status
|
||||||
private JobStatus status = JobStatus.PENDING;
|
private JobStatus status = JobStatus.NOT_READY;
|
||||||
private Float progress = 0.0f;
|
private Float progress = 0.0f;
|
||||||
|
|
||||||
public Job(String uuid, File inputFile, File outputFile) {
|
public Job(String uuid, File inputFile, File outputFile) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class DownloadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Resource downloadInput(String uuid) {
|
public Resource downloadInput(String uuid) {
|
||||||
Job job = jobService.get(uuid);
|
Job job = jobService.getJob(uuid);
|
||||||
|
|
||||||
if (job == null) {
|
if (job == null) {
|
||||||
throw new JobNotFound("Job doesn't exist");
|
throw new JobNotFound("Job doesn't exist");
|
||||||
@@ -33,7 +33,7 @@ public class DownloadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Resource downloadOutput(String uuid) {
|
public Resource downloadOutput(String uuid) {
|
||||||
Job job = jobService.get(uuid);
|
Job job = jobService.getJob(uuid);
|
||||||
|
|
||||||
if (job == null) {
|
if (job == null) {
|
||||||
throw new JobNotFound("Job doesn't exist");
|
throw new JobNotFound("Job doesn't exist");
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.ddf.vodsystem.services;
|
|||||||
import com.ddf.vodsystem.entities.ClipConfig;
|
import com.ddf.vodsystem.entities.ClipConfig;
|
||||||
import com.ddf.vodsystem.entities.Job;
|
import com.ddf.vodsystem.entities.Job;
|
||||||
import com.ddf.vodsystem.entities.JobStatus;
|
import com.ddf.vodsystem.entities.JobStatus;
|
||||||
import com.ddf.vodsystem.exceptions.JobNotFound;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -15,16 +14,8 @@ public class EditService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void edit(String uuid, ClipConfig clipConfig) {
|
public void edit(String uuid, ClipConfig clipConfig) {
|
||||||
Job job = jobService.get(uuid);
|
Job job = jobService.getJob(uuid);
|
||||||
|
validateClipConfig(clipConfig);
|
||||||
if (clipConfig.getStartPoint() != null && clipConfig.getStartPoint() < 0) {
|
|
||||||
throw new IllegalArgumentException("Start point cannot be negative");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (clipConfig.getFileSize() != null && clipConfig.getFileSize() < 100) {
|
|
||||||
throw new IllegalArgumentException("File size cannot be less than 100kb");
|
|
||||||
}
|
|
||||||
|
|
||||||
job.setClipConfig(clipConfig);
|
job.setClipConfig(clipConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,11 +24,7 @@ public class EditService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public float getProgress(String uuid) {
|
public float getProgress(String uuid) {
|
||||||
Job job = jobService.get(uuid);
|
Job job = jobService.getJob(uuid);
|
||||||
|
|
||||||
if (job == null) {
|
|
||||||
throw new JobNotFound(uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (job.getStatus() == JobStatus.FINISHED) {
|
if (job.getStatus() == JobStatus.FINISHED) {
|
||||||
return 1f;
|
return 1f;
|
||||||
@@ -45,4 +32,26 @@ public class EditService {
|
|||||||
|
|
||||||
return job.getProgress();
|
return job.getProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateClipConfig(ClipConfig clipConfig) {
|
||||||
|
Float start = clipConfig.getStartPoint();
|
||||||
|
Float end = clipConfig.getEndPoint();
|
||||||
|
Float fileSize = clipConfig.getFileSize();
|
||||||
|
|
||||||
|
if (start != null && start < 0) {
|
||||||
|
throw new IllegalArgumentException("Start point cannot be negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end != null && end < 0) {
|
||||||
|
throw new IllegalArgumentException("End point cannot be negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start != null && end != null && end <= start) {
|
||||||
|
throw new IllegalArgumentException("End point must be greater than start point");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileSize != null && fileSize < 100) {
|
||||||
|
throw new IllegalArgumentException("File size cannot be less than 100kb");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.ddf.vodsystem.services;
|
|||||||
|
|
||||||
import com.ddf.vodsystem.entities.Job;
|
import com.ddf.vodsystem.entities.Job;
|
||||||
import com.ddf.vodsystem.entities.JobStatus;
|
import com.ddf.vodsystem.entities.JobStatus;
|
||||||
|
import com.ddf.vodsystem.exceptions.JobNotFound;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -28,17 +29,19 @@ public class JobService {
|
|||||||
jobs.put(job.getUuid(), job);
|
jobs.put(job.getUuid(), job);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Job get(String uuid) {
|
public Job getJob(String uuid) {
|
||||||
return jobs.get(uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void jobReady(String uuid) {
|
|
||||||
Job job = jobs.get(uuid);
|
Job job = jobs.get(uuid);
|
||||||
|
|
||||||
if (job == null) {
|
if (job == null) {
|
||||||
throw new RuntimeException("Job not found");
|
throw new JobNotFound("Job not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jobReady(String uuid) {
|
||||||
|
Job job = getJob(uuid);
|
||||||
|
|
||||||
logger.info("Job ready: {}", job.getUuid());
|
logger.info("Job ready: {}", job.getUuid());
|
||||||
job.setStatus(JobStatus.PENDING);
|
job.setStatus(JobStatus.PENDING);
|
||||||
jobQueue.add(job);
|
jobQueue.add(job);
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ public class UploadService {
|
|||||||
private static final Logger logger = LoggerFactory.getLogger(UploadService.class);
|
private static final Logger logger = LoggerFactory.getLogger(UploadService.class);
|
||||||
|
|
||||||
@Value("${temp.vod.storage}")
|
@Value("${temp.vod.storage}")
|
||||||
private String INPUT_DIR;
|
private String inputDir;
|
||||||
@Value("${temp.vod.output}")
|
@Value("${temp.vod.output}")
|
||||||
private String OUTPUT_DIR;
|
private String outputDir;
|
||||||
|
|
||||||
private final JobService jobService;
|
private final JobService jobService;
|
||||||
|
|
||||||
@@ -42,10 +42,10 @@ public class UploadService {
|
|||||||
String extension = getFileExtension(file.getOriginalFilename());
|
String extension = getFileExtension(file.getOriginalFilename());
|
||||||
String filename = uuid + (extension.isEmpty() ? "" : "." + extension);
|
String filename = uuid + (extension.isEmpty() ? "" : "." + extension);
|
||||||
|
|
||||||
Path inputPath = Paths.get(INPUT_DIR, filename);
|
Path inputPath = Paths.get(inputDir, filename);
|
||||||
File inputFile = inputPath.toFile();
|
File inputFile = inputPath.toFile();
|
||||||
|
|
||||||
Path outputPath = Paths.get(OUTPUT_DIR, filename);
|
Path outputPath = Paths.get(outputDir, filename);
|
||||||
File outputFile = outputPath.toFile();
|
File outputFile = outputPath.toFile();
|
||||||
|
|
||||||
moveToFile(file, inputFile);
|
moveToFile(file, inputFile);
|
||||||
@@ -87,17 +87,17 @@ public class UploadService {
|
|||||||
|
|
||||||
private void createDirectories() throws IOException {
|
private void createDirectories() throws IOException {
|
||||||
// Create INPUT_DIR if it doesn't exist
|
// Create INPUT_DIR if it doesn't exist
|
||||||
Path inputDirPath = Paths.get(INPUT_DIR);
|
Path inputDirPath = Paths.get(inputDir);
|
||||||
if (Files.notExists(inputDirPath)) {
|
if (Files.notExists(inputDirPath)) {
|
||||||
Files.createDirectories(inputDirPath);
|
Files.createDirectories(inputDirPath);
|
||||||
System.out.println("Created directory: " + INPUT_DIR);
|
logger.info("Created directory: {}", inputDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create OUTPUT_DIR if it doesn't exist
|
// Create OUTPUT_DIR if it doesn't exist
|
||||||
Path outputDirPath = Paths.get(OUTPUT_DIR);
|
Path outputDirPath = Paths.get(outputDir);
|
||||||
if (Files.notExists(outputDirPath)) {
|
if (Files.notExists(outputDirPath)) {
|
||||||
Files.createDirectories(outputDirPath);
|
Files.createDirectories(outputDirPath);
|
||||||
System.out.println("Created directory: " + OUTPUT_DIR);
|
logger.info("Created directory: {}", outputDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user