|
|
@@ -2,9 +2,11 @@ package com.dji.sample.media.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.ZipUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.dji.sample.common.util.CoordinateUtil;
|
|
|
import com.dji.sample.common.util.PicExifUtil;
|
|
|
+import com.dji.sample.common.util.ThumbnailatorUtil;
|
|
|
import com.dji.sample.common.util.UserRequest;
|
|
|
import com.dji.sample.component.oss.model.OssConfiguration;
|
|
|
import com.dji.sample.component.oss.service.impl.OssServiceContext;
|
|
|
@@ -32,10 +34,10 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.net.URL;
|
|
|
@@ -76,6 +78,11 @@ public class FileServiceImpl implements IFileService {
|
|
|
|
|
|
private static final String IMAGE = "image";
|
|
|
private static final String VIDEO = "video";
|
|
|
+ private static final String TAIL = "_100x100";
|
|
|
+ private static final String T_PATH = "thumbnails/";
|
|
|
+ private static final String DOT = ".";
|
|
|
+ private static final String SPLIT_DOT = "\\.";
|
|
|
+ private static final String JPEG = "JPEG";
|
|
|
|
|
|
private Optional<MediaFileEntity> getMediaByFingerprint(String workspaceId, String fingerprint) {
|
|
|
MediaFileEntity fileEntity = mapper.selectOne(new LambdaQueryWrapper<MediaFileEntity>()
|
|
|
@@ -144,7 +151,8 @@ public class FileServiceImpl implements IFileService {
|
|
|
MediaFileEntity fileEntity = this.fileUploadConvertToEntity(workspaceId,file);
|
|
|
fileEntity.setWorkspaceId(workspaceId);
|
|
|
fileEntity.setFileId(UUID.randomUUID().toString());
|
|
|
- return mapper.insert(fileEntity);
|
|
|
+ int result = mapper.insert(fileEntity);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -263,12 +271,16 @@ public class FileServiceImpl implements IFileService {
|
|
|
}
|
|
|
dirDto = mediaDirService.createDir(dirBuilder.build());
|
|
|
}
|
|
|
+ //生成缩略图
|
|
|
+ String tObjectKey = getThumbnailObjectKey(file.getName());
|
|
|
+ putThumbnailImage(tObjectKey,file.getObjectKey());
|
|
|
|
|
|
MediaFileEntity.MediaFileEntityBuilder builder = MediaFileEntity.builder();
|
|
|
|
|
|
if (file != null) {
|
|
|
//获取图片信息
|
|
|
MediaExifDTO exif = PicExifUtil.getPicExif(file.getObjectKey());
|
|
|
+
|
|
|
//经纬度转换
|
|
|
//AMap aMap = CoordinateUtil.transform(file.getMetadata().getShootPosition().getLng(),file.getMetadata().getShootPosition().getLat());
|
|
|
AMap aMap = CoordinateUtil.transform(exif.getLongitude(),exif.getLatitude());
|
|
|
@@ -355,6 +367,8 @@ public class FileServiceImpl implements IFileService {
|
|
|
taskName = mediaDirDTO.getDirName();
|
|
|
}
|
|
|
MediaTypeEnum mediaTypeEnum = convertMediaType(entity);
|
|
|
+ String url = ossService.getObjectUrl(OssConfiguration.bucket, entity.getObjectKey()).toString();
|
|
|
+ String thumbnailURL = getThumbnailURL(entity);
|
|
|
builder.fileName(entity.getFileName())
|
|
|
.fileId(entity.getFileId())
|
|
|
.filePath(entity.getFilePath())
|
|
|
@@ -383,7 +397,8 @@ public class FileServiceImpl implements IFileService {
|
|
|
Instant.ofEpochMilli(entity.getCreateTime()), ZoneId.systemDefault()))
|
|
|
.xResolution(entity.getXResolution())
|
|
|
.yResolution(entity.getYResolution())
|
|
|
- .url(ossService.getObjectUrl(OssConfiguration.bucket, entity.getObjectKey()).toString())
|
|
|
+ .url(url)
|
|
|
+ .thumbnailUrl(null == thumbnailURL ? url : thumbnailURL)
|
|
|
.longitudeRef(entity.getLongitudeRef())
|
|
|
.latitudeRef(entity.getLatitudeRef())
|
|
|
.mediaType(mediaTypeEnum.getVal())
|
|
|
@@ -396,6 +411,40 @@ public class FileServiceImpl implements IFileService {
|
|
|
return builder.build();
|
|
|
}
|
|
|
|
|
|
+ private String getThumbnailURL(MediaFileEntity entity) {
|
|
|
+ String originFileName = entity.getFileName();
|
|
|
+ //缩略图路径
|
|
|
+ String tObjectKey = getThumbnailObjectKey(originFileName);
|
|
|
+ putThumbnailImage(tObjectKey,entity.getObjectKey());
|
|
|
+ return ossService.getObjectUrl(OssConfiguration.bucket, tObjectKey).toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void putThumbnailImage(String tObjectKey, String originObjectKey) {
|
|
|
+ if(originObjectKey.endsWith(PicExifUtil.MP4)) {
|
|
|
+ System.out.println(originObjectKey);
|
|
|
+ }
|
|
|
+ //生成缩略图
|
|
|
+ if(!ossService.objectExists(OssConfiguration.bucket,tObjectKey)) {
|
|
|
+ BufferedImage bufferedImage = ThumbnailatorUtil.getThumbnail(originObjectKey,100,100);
|
|
|
+ if(bufferedImage != null) {
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ try {
|
|
|
+ ImageIO.write(bufferedImage, JPEG, outputStream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ossService.putObject(OssConfiguration.bucket,tObjectKey,new ByteArrayInputStream(outputStream.toByteArray()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getThumbnailObjectKey(String originFileName) {
|
|
|
+ String[] nameSplit = originFileName.split(SPLIT_DOT);
|
|
|
+ //缩略图路径
|
|
|
+ return T_PATH + nameSplit[0] + TAIL + DOT + PicExifUtil.JPG;
|
|
|
+ }
|
|
|
+
|
|
|
private MediaTypeEnum convertMediaType(MediaFileEntity entity) {
|
|
|
if(StringUtils.hasText(entity.getMediaType())) {
|
|
|
if (entity.getMediaType().startsWith(IMAGE)) {
|
|
|
@@ -544,4 +593,52 @@ public class FileServiceImpl implements IFileService {
|
|
|
.collect(Collectors.toMap(a -> a.getDirId(), MediaFileSumEntity::getSumSize));
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Boolean updateFile(MediaFileDTO mediaFileDTO) {
|
|
|
+ int update = mapper.update(this.dtoToEntity(mediaFileDTO),
|
|
|
+ new LambdaUpdateWrapper<MediaFileEntity>().eq(MediaFileEntity::getFileId, mediaFileDTO.getFileId()));
|
|
|
+ return update > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private MediaFileEntity dtoToEntity(MediaFileDTO mediaFileDTO) {
|
|
|
+ MediaFileEntity.MediaFileEntityBuilder builder = MediaFileEntity.builder();
|
|
|
+
|
|
|
+ if (mediaFileDTO != null) {
|
|
|
+ builder.fileName(mediaFileDTO.getFileName())
|
|
|
+ .fileId(mediaFileDTO.getFileId())
|
|
|
+ .filePath(mediaFileDTO.getFilePath())
|
|
|
+ .isOriginal(mediaFileDTO.getIsOriginal())
|
|
|
+ .fingerprint(mediaFileDTO.getFingerprint())
|
|
|
+ .objectKey(mediaFileDTO.getObjectKey())
|
|
|
+ .tinnyFingerprint(mediaFileDTO.getTinnyFingerprint())
|
|
|
+ .payload(mediaFileDTO.getPayload())
|
|
|
+ .createTime(mediaFileDTO.getCreateTime() != null ?
|
|
|
+ mediaFileDTO.getCreateTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() : null)
|
|
|
+ .drone(mediaFileDTO.getDrone())
|
|
|
+ .jobId(mediaFileDTO.getJobId())
|
|
|
+ .dirId(mediaFileDTO.getDirId())
|
|
|
+ .waylineName(mediaFileDTO.getWaylineName())
|
|
|
+ .size(mediaFileDTO.getSize())
|
|
|
+ .longitude(mediaFileDTO.getLongitude())
|
|
|
+ .latitude(mediaFileDTO.getLatitude())
|
|
|
+ .imageWidth(mediaFileDTO.getImageWidth())
|
|
|
+ .imageHeight(mediaFileDTO.getImageHeight())
|
|
|
+ .absoluteAltitude(mediaFileDTO.getAbsoluteAltitude())
|
|
|
+ .gimbalYawDegree(mediaFileDTO.getGimbalYawDegree())
|
|
|
+ .relativeAltitude(mediaFileDTO.getRelativeAltitude())
|
|
|
+ .taskName(mediaFileDTO.getTaskName())
|
|
|
+ .sourceFrom(mediaFileDTO.getSourceFrom())
|
|
|
+ .pictureTime(mediaFileDTO.getPictureTime() != null ?
|
|
|
+ mediaFileDTO.getPictureTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() : null)
|
|
|
+ .xResolution(mediaFileDTO.getXResolution())
|
|
|
+ .yResolution(mediaFileDTO.getYResolution())
|
|
|
+ .longitudeRef(mediaFileDTO.getLongitudeRef())
|
|
|
+ .latitudeRef(mediaFileDTO.getLatitudeRef())
|
|
|
+ .pictureType(mediaFileDTO.getPictureType())
|
|
|
+ .durationSeconds(mediaFileDTO.getDurationSeconds());
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
}
|