diff --git a/src/main/java/com/ddf/vodsystem/services/DirectoryService.java b/src/main/java/com/ddf/vodsystem/services/DirectoryService.java new file mode 100644 index 0000000..3283b4d --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/services/DirectoryService.java @@ -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); + } +} \ No newline at end of file diff --git a/src/main/java/com/ddf/vodsystem/services/UploadService.java b/src/main/java/com/ddf/vodsystem/services/UploadService.java index 18c81e7..370c508 100644 --- a/src/main/java/com/ddf/vodsystem/services/UploadService.java +++ b/src/main/java/com/ddf/vodsystem/services/UploadService.java @@ -2,19 +2,14 @@ package com.ddf.vodsystem.services; import com.ddf.vodsystem.entities.Job; import com.ddf.vodsystem.entities.VideoMetadata; -import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; -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.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; @@ -25,18 +20,17 @@ import org.slf4j.LoggerFactory; public class UploadService { 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 MetadataService metadataService; + private final DirectoryService directoryService; @Autowired - public UploadService(JobService jobService, MetadataService metadataService) { + public UploadService(JobService jobService, + MetadataService metadataService, + DirectoryService directoryService) { this.jobService = jobService; this.metadataService = metadataService; + this.directoryService = directoryService; } public String upload(MultipartFile file) { @@ -45,13 +39,9 @@ public class UploadService { String extension = getFileExtension(file.getOriginalFilename()); String filename = uuid + (extension.isEmpty() ? "" : "." + extension); - Path inputPath = Paths.get(inputDir, filename); - File inputFile = inputPath.toFile(); - - Path outputPath = Paths.get(outputDir, filename); - File outputFile = outputPath.toFile(); - - moveToFile(file, inputFile); + File inputFile = directoryService.getTempInputFile(filename); + File outputFile = directoryService.getTempOutputFile(filename); + directoryService.saveData(inputFile, file); // add job VideoMetadata videoMetadata = metadataService.getVideoMetadata(inputFile); @@ -61,15 +51,6 @@ public class UploadService { 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() { UUID uuid = UUID.randomUUID(); ByteBuffer bb = ByteBuffer.wrap(new byte[16]); @@ -88,29 +69,4 @@ public class UploadService { } 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); - } - } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 773728b..fde3f3f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,8 +4,9 @@ spring.profiles.active=local # VODs spring.servlet.multipart.max-file-size=2GB spring.servlet.multipart.max-request-size=2GB -temp.vod.storage=videos/inputs/ -temp.vod.output=videos/outputs/ +storage.temp.inputs=videos/inputs/ +storage.temp.outputs=videos/outputs/ +storage.outputs=videos/clips/ # Logging logging.level.org.springframework.web=DEBUG \ No newline at end of file