Decoupling ProcessService from Video Validation and Validation Execution

This commit is contained in:
Reeverflow
2026-02-21 14:40:30 +01:00
parent 3e50cd970f
commit 99ff046d16
2 changed files with 48 additions and 42 deletions

View File

@@ -1,54 +1,12 @@
package com.example.video_downloader.services;
import com.example.video_downloader.entity.StatusEnum;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.repositories.VideoRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static java.nio.file.Path.*;
@Service
public class ProcessService {
@Autowired
private VideoRepository videoRepository;
@PostConstruct
public void validateVideos() throws HttpClientErrorException, IOException, InterruptedException {
List<Video> response = videoRepository.findUrlByStatus(StatusEnum.NEW);
for(Video video : response) {
if(video.getStatus().equals(StatusEnum.NEW)){
downloadVideo(video.getUrl(), video.getId());
Path filePath = Paths.get("downloads/" + video.getId() + ".webm");
if(Files.exists(filePath) && Files.isRegularFile(filePath)) {
System.out.println("Apparently it exists mate.");
video.setStatus(StatusEnum.SAVED);
} else {
System.out.println("Nah it does not exist mate.");
video.setStatus(StatusEnum.FAILED);
}
videoRepository.save(video);
}
}
}
//@PostConstruct
public void downloadVideo(String url, Long id) throws HttpClientErrorException, InterruptedException, IOException {
String outputPath = "downloads/" + id + ".%(ext)s";
execute("yt-dlp", "-o", outputPath, url);
}
public void execute(String... command) throws InterruptedException, IOException {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

View File

@@ -0,0 +1,48 @@
package com.example.video_downloader.services;
import com.example.video_downloader.entity.StatusEnum;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.repositories.VideoRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.HttpClientErrorException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class YtdlpService {
@Autowired
private VideoRepository videoRepository;
@Autowired
private ProcessService processService;
@PostConstruct
public void validateVideos() throws HttpClientErrorException, IOException, InterruptedException {
List<Video> response = videoRepository.findUrlByStatus(StatusEnum.NEW);
for(Video video : response) {
if(video.getStatus().equals(StatusEnum.NEW)){
downloadVideo(video.getUrl(), video.getId());
Path filePath = Paths.get("downloads/" + video.getId() + ".webm");
if(Files.exists(filePath) && Files.isRegularFile(filePath)) {
System.out.println("Apparently it exists mate.");
video.setStatus(StatusEnum.SAVED);
} else {
System.out.println("Nah it does not exist mate.");
video.setStatus(StatusEnum.FAILED);
}
videoRepository.save(video);
}
}
}
public void downloadVideo(String url, Long id) throws HttpClientErrorException, InterruptedException, IOException {
String outputPath = "downloads/" + id + ".%(ext)s";
processService.execute("yt-dlp", "-o", outputPath, url);
}
}