ADD JobService and UploadService
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.services.UploadService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
public class UploadController {
|
||||
|
||||
private final UploadService uploadService;
|
||||
|
||||
@Autowired
|
||||
public UploadController(UploadService uploadService) {
|
||||
this.uploadService = uploadService;
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file) {
|
||||
String uuid = uploadService.upload(file);
|
||||
|
||||
return new ResponseEntity<>(uuid, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@RestController
|
||||
public class vodController {
|
||||
|
||||
private static final String UPLOAD_DIR = "videos/";
|
||||
|
||||
@PostMapping("/upload")
|
||||
public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file) {
|
||||
try {
|
||||
File uploadDir = new File(UPLOAD_DIR);
|
||||
|
||||
if (!uploadDir.exists()) {
|
||||
uploadDir.mkdirs();
|
||||
}
|
||||
|
||||
Path filePath = Paths.get(UPLOAD_DIR, file.getOriginalFilename());
|
||||
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
return ResponseEntity.ok("Uploaded: " + file.getOriginalFilename());
|
||||
} catch (IOException e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body("Upload failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/main/java/com/ddf/vodsystem/entities/Job.java
Normal file
33
src/main/java/com/ddf/vodsystem/entities/Job.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.ddf.vodsystem.entities;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.File;
|
||||
|
||||
@Data
|
||||
public class Job implements Runnable {
|
||||
private String uuid;
|
||||
private File file;
|
||||
private boolean started;
|
||||
private float progress;
|
||||
|
||||
// configs
|
||||
private float startPoint;
|
||||
private float endPoint;
|
||||
private float fps;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public Job(String uuid, File file) {
|
||||
this.uuid = uuid;
|
||||
this.file = file;
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.started = true;
|
||||
this.progress = 0;
|
||||
|
||||
System.out.println("<UNK>");
|
||||
}
|
||||
}
|
||||
45
src/main/java/com/ddf/vodsystem/services/JobService.java
Normal file
45
src/main/java/com/ddf/vodsystem/services/JobService.java
Normal 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();
|
||||
}
|
||||
}
|
||||
56
src/main/java/com/ddf/vodsystem/services/UploadService.java
Normal file
56
src/main/java/com/ddf/vodsystem/services/UploadService.java
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user