REFactor storage configuration and introduce DirectoryService for file management
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
package com.ddf.vodsystem.services;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DirectoryService {
|
||||||
|
|
||||||
|
private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DirectoryService.class);
|
||||||
|
|
||||||
|
@Value("${storage.outputs}")
|
||||||
|
private String outputDir;
|
||||||
|
|
||||||
|
@Value("${storage.temp.inputs}")
|
||||||
|
private String tempInputsDir;
|
||||||
|
|
||||||
|
@Value("${storage.temp.outputs}")
|
||||||
|
private String tempOutputsDir;
|
||||||
|
|
||||||
|
public File getTempInputFile(String id) {
|
||||||
|
String dir = tempInputsDir + File.separator + id;
|
||||||
|
return new File(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getTempOutputFile(String id) {
|
||||||
|
String dir = tempOutputsDir + File.separator + id;
|
||||||
|
return new File(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getOutputFile(String id) {
|
||||||
|
String dir = outputDir + File.separator + id;
|
||||||
|
return new File(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveData(File file, MultipartFile multipartFile) {
|
||||||
|
try {
|
||||||
|
Path filePath = Paths.get(file.getAbsolutePath());
|
||||||
|
Files.copy(multipartFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createDirectory(String dir) throws IOException {
|
||||||
|
// Create the directory if it doesn't exist
|
||||||
|
Path outputPath = Paths.get(dir);
|
||||||
|
if (Files.notExists(outputPath)) {
|
||||||
|
Files.createDirectories(outputPath);
|
||||||
|
logger.info("Created directory: {}", outputPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void createDirectoriesIfNotExist() throws IOException {
|
||||||
|
createDirectory(tempInputsDir);
|
||||||
|
createDirectory(tempOutputsDir);
|
||||||
|
createDirectory(outputDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,19 +2,14 @@ package com.ddf.vodsystem.services;
|
|||||||
|
|
||||||
import com.ddf.vodsystem.entities.Job;
|
import com.ddf.vodsystem.entities.Job;
|
||||||
import com.ddf.vodsystem.entities.VideoMetadata;
|
import com.ddf.vodsystem.entities.VideoMetadata;
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -25,18 +20,17 @@ import org.slf4j.LoggerFactory;
|
|||||||
public class UploadService {
|
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}")
|
|
||||||
private String inputDir;
|
|
||||||
@Value("${temp.vod.output}")
|
|
||||||
private String outputDir;
|
|
||||||
|
|
||||||
private final JobService jobService;
|
private final JobService jobService;
|
||||||
private final MetadataService metadataService;
|
private final MetadataService metadataService;
|
||||||
|
private final DirectoryService directoryService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UploadService(JobService jobService, MetadataService metadataService) {
|
public UploadService(JobService jobService,
|
||||||
|
MetadataService metadataService,
|
||||||
|
DirectoryService directoryService) {
|
||||||
this.jobService = jobService;
|
this.jobService = jobService;
|
||||||
this.metadataService = metadataService;
|
this.metadataService = metadataService;
|
||||||
|
this.directoryService = directoryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String upload(MultipartFile file) {
|
public String upload(MultipartFile file) {
|
||||||
@@ -45,13 +39,9 @@ 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(inputDir, filename);
|
File inputFile = directoryService.getTempInputFile(filename);
|
||||||
File inputFile = inputPath.toFile();
|
File outputFile = directoryService.getTempOutputFile(filename);
|
||||||
|
directoryService.saveData(inputFile, file);
|
||||||
Path outputPath = Paths.get(outputDir, filename);
|
|
||||||
File outputFile = outputPath.toFile();
|
|
||||||
|
|
||||||
moveToFile(file, inputFile);
|
|
||||||
|
|
||||||
// add job
|
// add job
|
||||||
VideoMetadata videoMetadata = metadataService.getVideoMetadata(inputFile);
|
VideoMetadata videoMetadata = metadataService.getVideoMetadata(inputFile);
|
||||||
@@ -61,15 +51,6 @@ public class UploadService {
|
|||||||
return uuid;
|
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) {
|
|
||||||
logger.error(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String generateShortUUID() {
|
private static String generateShortUUID() {
|
||||||
UUID uuid = UUID.randomUUID();
|
UUID uuid = UUID.randomUUID();
|
||||||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
||||||
@@ -88,29 +69,4 @@ public class UploadService {
|
|||||||
}
|
}
|
||||||
return fileName.substring(dotIndex + 1);
|
return fileName.substring(dotIndex + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createDirectories() throws IOException {
|
|
||||||
// Create INPUT_DIR if it doesn't exist
|
|
||||||
Path inputDirPath = Paths.get(inputDir);
|
|
||||||
if (Files.notExists(inputDirPath)) {
|
|
||||||
Files.createDirectories(inputDirPath);
|
|
||||||
logger.info("Created directory: {}", inputDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create OUTPUT_DIR if it doesn't exist
|
|
||||||
Path outputDirPath = Paths.get(outputDir);
|
|
||||||
if (Files.notExists(outputDirPath)) {
|
|
||||||
Files.createDirectories(outputDirPath);
|
|
||||||
logger.info("Created directory: {}", outputDir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
try {
|
|
||||||
createDirectories();
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("Failed to create directories: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ spring.profiles.active=local
|
|||||||
# VODs
|
# VODs
|
||||||
spring.servlet.multipart.max-file-size=2GB
|
spring.servlet.multipart.max-file-size=2GB
|
||||||
spring.servlet.multipart.max-request-size=2GB
|
spring.servlet.multipart.max-request-size=2GB
|
||||||
temp.vod.storage=videos/inputs/
|
storage.temp.inputs=videos/inputs/
|
||||||
temp.vod.output=videos/outputs/
|
storage.temp.outputs=videos/outputs/
|
||||||
|
storage.outputs=videos/clips/
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
logging.level.org.springframework.web=DEBUG
|
logging.level.org.springframework.web=DEBUG
|
||||||
Reference in New Issue
Block a user