Skip to content

Commit

Permalink
feat(MediaInfo): support get media info
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Oct 27, 2020
1 parent fe41844 commit 23249f1
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.configuration;

import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.controller.VideoController;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.controller.MediaStreamingReactiveController;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.handler.MediaStreamingExceptionHandler;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.handler.VideoRouteHandler;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.repository.VideoRepository;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.repository.impl.InMemoryVideoOnFileSystemRepository;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.FileService;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.MediaInfoService;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.VideoService;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.impl.FileServiceImpl;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.impl.MediaInfoServiceImpl;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.impl.VideoServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -89,8 +91,9 @@ public RouterFunction<ServerResponse> videoEndPoint(VideoRouteHandler videoRoute
}

@Bean
public VideoController videoController(VideoService videoService) {
return new VideoController(videoService);
public MediaStreamingReactiveController videoController(VideoService videoService,
MediaInfoService mediaInfoService) {
return new MediaStreamingReactiveController(videoService, mediaInfoService);
}

@Bean
Expand All @@ -101,8 +104,8 @@ public VideoService videoService(VideoRepository videoRepository) {

@Bean
@ConditionalOnMissingBean
public FileService fileService() {
return new FileServiceImpl(mediaStreamingProperties);
public FileService fileService(VideoRepository videoRepository) {
return new FileServiceImpl(mediaStreamingProperties, videoRepository);
}

@Bean
Expand All @@ -111,6 +114,12 @@ public VideoRepository videoRepository() {
return new InMemoryVideoOnFileSystemRepository();
}

@Bean
@ConditionalOnMissingBean
public MediaInfoService mediaInfoService(FileService fileService) {
return new MediaInfoServiceImpl(fileService);
}

@SuppressWarnings("SameParameterValue")
private static RequestPredicate param(String parameter) {
return RequestPredicates.all().and(request -> request.queryParam(parameter).isPresent());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.controller;

import com.drew.metadata.Directory;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.model.Video;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.MediaInfoService;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.VideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.List;

/**
* Description: VideoController, change description here.
* Description: MediaStreamingReactiveController, change description here.
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/26/2020 4:17 PM
**/
@RestController
@RequiredArgsConstructor
public class VideoController {
public class MediaStreamingReactiveController {
private final VideoService videoService;
private final MediaInfoService mediaInfoService;

@GetMapping(value = "/video-annotation", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<List<Video>> getVideo() {
return Flux.interval(Duration.ofSeconds(2)).map(aLong -> videoService.getAllVideoList());
}

@GetMapping(value = "/media-info/{name}")
public Flux<Directory> mediaInfo(@PathVariable String name) {
return mediaInfoService.getMediaInfo(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.exception.BadResourceLocationException;
import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.nio.file.FileVisitOption;
Expand All @@ -23,11 +24,19 @@ public interface FileService {
*/
Flux<Path> getAllFiles();

/**
* Gets file by file name.
*
* @param fileName the file name
* @return the file by file name
*/
Mono<Path> getFileByFileName(String fileName);

/**
* default method to create a flux from a stream of file paths
*
* @param path to traverse
* @return flux
* @return flux flux
*/
default Flux<Path> fromPath(Path path) {
return Flux.using(() -> Files.walk(path, FileVisitOption.FOLLOW_LINKS), Flux::fromStream, BaseStream::close)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services;

import com.drew.metadata.Directory;
import reactor.core.publisher.Flux;

/**
* Description: MediaInfoService, change description here.
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/27/2020 2:46 PM
**/
public interface MediaInfoService {
Flux<Directory> getMediaInfo(String name);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.impl;

import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.configuration.MediaStreamingProperties;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.model.Video;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.repository.VideoRepository;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -18,9 +21,15 @@
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {
private final MediaStreamingProperties mediaStreamingProperties;
private final VideoRepository videoRepository;

@Override
public Flux<Path> getAllFiles() {
return fromPath(Paths.get(mediaStreamingProperties.getVideoDirectoryOnFileSystem()));
}

@Override
public Mono<Path> getFileByFileName(String fileName) {
return Mono.from(videoRepository.getVideoByName(fileName).map(Video::getLocation));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.impl;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.FileService;
import com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.services.MediaInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;

/**
* Description: MediaInfoServiceImpl, change description here.
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/27/2020 2:47 PM
**/
@Slf4j
@RequiredArgsConstructor
public class MediaInfoServiceImpl implements MediaInfoService {
private final FileService fileService;

@Override
public Flux<Directory> getMediaInfo(String name) {
val fileByFileName = fileService.getFileByFileName(name);
Mono<Iterable<Directory>> iterableMono = fileByFileName.map(path -> {
Iterable<Directory> directories = null;
try {
Metadata metadata = ImageMetadataReader.readMetadata(path.toFile());
directories = metadata.getDirectories();
} catch (ImageProcessingException | IOException e) {
log.error("Cannot read file metadata!", e);
}
return directories;
});
return iterableMono.flatMapMany(Flux::fromIterable);
}
}

0 comments on commit 23249f1

Please sign in to comment.