ADD JobService and UploadService

This commit is contained in:
2025-05-05 21:12:35 +02:00
parent 9f9e8251f9
commit da5d2ef853
5 changed files with 162 additions and 40 deletions

View File

@@ -0,0 +1,45 @@
package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.Job;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
@Service
public class JobService {
private LinkedList<Job> jobs = new LinkedList<>();
public void addJob(Job job) {
jobs.add(job);
}
public Job getNextJob() {
return jobs.remove();
}
public Job getJob(String uuid){
for (Job job : jobs) {
if(job.getUuid().equals(uuid)){
return job;
}
}
throw new RuntimeException("UUID not found");
}
@PostConstruct
public void startProcessingLoop() {
Thread thread = new Thread(() -> {
while (true) {
if (!jobs.isEmpty()) {
Runnable task = getNextJob();
task.run(); // Execute the task
}
}
});
thread.setDaemon(true);
thread.start();
}
}

View File

@@ -0,0 +1,56 @@
package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.Job;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Base64;
import java.util.UUID;
@Service
public class UploadService {
private static final String UPLOAD_DIR = "videos/";
private JobService jobService;
public UploadService(JobService jobService) {
this.jobService = jobService;
}
public String upload(MultipartFile file) {
// generate uuid, file
String uuid = generateShortUUID();
File uploadDir = new File(UPLOAD_DIR + uuid + ".mp4");
moveToFile(file, uploadDir);
// add job
Job job = new Job(uuid, uploadDir);
jobService.addJob(job);
return uuid;
}
private void moveToFile(MultipartFile inputFile, File outputFile) {
try {
Path filePath = Paths.get(outputFile.getAbsolutePath());
Files.copy(inputFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String generateShortUUID() {
UUID uuid = UUID.randomUUID();
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array());
}
}