Feat: 实现用户模块和文件模块

This commit is contained in:
wangyu 2021-01-09 16:59:18 +08:00
parent 470683473d
commit a43d773d9c

View File

@ -0,0 +1,70 @@
package com.flyfish.framework.file.controller;
import com.flyfish.framework.bean.Result;
import com.flyfish.framework.file.config.UploadConfiguration;
import com.flyfish.framework.file.domain.Media;
import com.flyfish.framework.file.service.MediaService;
import com.flyfish.framework.file.utils.DownloadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 媒体上传
*
* @author wangyu
*/
@RestController
@RequestMapping("/media")
public class MediaController {
@Resource
private MediaService mediaService;
@Resource
private UploadConfiguration configuration;
/**
* 上传媒体支持多个同时上传
*
* @param files 文件
* @return 结果
*/
@PostMapping("")
public Mono<Result<List<Media>>> uploadMedia(@RequestPart("file") Flux<FilePart> files) {
return files.flatMap(part -> part.filename().endsWith("m3u8") || part.filename().endsWith("m3u") ? mediaService.slice(part) : mediaService.upload(part))
.reduce(Result.accept(new ArrayList<>()), ((listResult, media) -> {
listResult.getData().add(media);
return listResult;
}));
}
@GetMapping("/**")
public Mono<Void> downloadMedia(ServerHttpRequest request, ServerHttpResponse response) {
String path = StringUtils.substringAfter(request.getURI().getPath(), "/media");
return DownloadUtils.download(configuration.getLocalPath() + path, response);
}
/**
* 上传hls切片内容
*
* @param files 文件们
* @return 结果
*/
@PostMapping("hls")
public Mono<Result<List<Media>>> uploadHls(@RequestPart("file") Flux<FilePart> files) {
return files.flatMap(mediaService::slice)
.reduce(Result.accept(new ArrayList<>()), ((listResult, media) -> {
listResult.getData().add(media);
return listResult;
}));
}
}