|
|
@@ -22,6 +22,7 @@ import com.dji.sample.media.model.*;
|
|
|
import com.dji.sample.media.service.IFileService;
|
|
|
import com.dji.sample.media.service.IMediaDirService;
|
|
|
import com.dji.sample.wayline.dao.IWaylineJobMapper;
|
|
|
+import com.dji.sample.wayline.model.dto.WaylineFileDTO;
|
|
|
import com.dji.sample.wayline.model.entity.WaylineJobEntity;
|
|
|
import com.dji.sample.wayline.service.IWaylineFileService;
|
|
|
import com.dji.sdk.cloudapi.device.DeviceEnum;
|
|
|
@@ -33,6 +34,9 @@ import com.dji.sdk.common.CoordinateUtil;
|
|
|
import com.dji.sdk.common.Pagination;
|
|
|
import com.dji.sdk.common.PaginationData;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.compress.archivers.ArchiveEntry;
|
|
|
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
|
|
+import org.dom4j.Document;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -48,12 +52,15 @@ import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.Executor;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
/**
|
|
|
* @author sean
|
|
|
@@ -105,7 +112,9 @@ public class FileServiceImpl implements IFileService {
|
|
|
private static final String DOT = ".";
|
|
|
private static final String SPLIT_DOT = "\\.";
|
|
|
private static final String JPEG = "JPEG";
|
|
|
+ private static final String JPG = "JPG";
|
|
|
private static final String MP4 = "MP4";
|
|
|
+ private static final String ZIP_FILE_SUFFIX = ".zip";
|
|
|
|
|
|
private Optional<MediaFileEntity> getMediaByFingerprint(String workspaceId, String fingerprint) {
|
|
|
MediaFileEntity fileEntity = mapper.selectOne(new LambdaQueryWrapper<MediaFileEntity>()
|
|
|
@@ -205,41 +214,37 @@ public class FileServiceImpl implements IFileService {
|
|
|
fileEntity.setFileId(UUID.randomUUID().toString());
|
|
|
int result = mapper.insert(fileEntity);
|
|
|
//更新exif信息
|
|
|
- updateExifInfo(fileEntity,workspaceId,file);
|
|
|
+ updateExifInfo(fileEntity,workspaceId,file.getObjectKey(), file.getName());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- private void updateExifInfo(MediaFileEntity fileEntity, String workspaceId, MediaUploadCallbackRequest file) {
|
|
|
+ private void updateExifInfo(MediaFileEntity fileEntity, String workspaceId,String objectKey, String fileName) {
|
|
|
//生成缩略图
|
|
|
fileThreadPool.execute(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
- String objectKey = file.getObjectKey();
|
|
|
-
|
|
|
//生成缩略图
|
|
|
- String tObjectKey = getThumbnailObjectKey(file.getName());
|
|
|
+ String tObjectKey = getThumbnailObjectKey(fileName);
|
|
|
putThumbnailImage(tObjectKey, objectKey, true);
|
|
|
|
|
|
//获取图片信息
|
|
|
MediaExifDTO exif = PicExifUtil.getPicExif(objectKey);
|
|
|
|
|
|
- if (file != null) {
|
|
|
- fileEntity.setImageWidth(exif.getImageWidth());
|
|
|
- fileEntity.setImageHeight(exif.getImageHeight());
|
|
|
- fileEntity.setXResolution(exif.getXResolution());
|
|
|
- fileEntity.setYResolution(exif.getYResolution());
|
|
|
- fileEntity.setLatitude(BigDecimal.valueOf(CoordinateUtil.checkValidVal(exif.getLatitude())).setScale(14, RoundingMode.DOWN));
|
|
|
- fileEntity.setLongitude(BigDecimal.valueOf(CoordinateUtil.checkValidVal(exif.getLongitude())).setScale(14, RoundingMode.DOWN));
|
|
|
- fileEntity.setLongitudeRef(exif.getLongitudeRef());
|
|
|
- fileEntity.setLatitudeRef(exif.getLatitudeRef());
|
|
|
- fileEntity.setMediaType(exif.getPictureType());
|
|
|
- fileEntity.setPictureType(exif.getPictureType());
|
|
|
- fileEntity.setDurationSeconds(exif.getDurationSeconds());
|
|
|
-
|
|
|
- mapper.update(fileEntity,
|
|
|
- new LambdaUpdateWrapper<MediaFileEntity>().eq(MediaFileEntity::getFileId, fileEntity.getFileId()));
|
|
|
- }
|
|
|
+ fileEntity.setImageWidth(exif.getImageWidth());
|
|
|
+ fileEntity.setImageHeight(exif.getImageHeight());
|
|
|
+ fileEntity.setXResolution(exif.getXResolution());
|
|
|
+ fileEntity.setYResolution(exif.getYResolution());
|
|
|
+ fileEntity.setLatitude(BigDecimal.valueOf(CoordinateUtil.checkValidVal(exif.getLatitude())).setScale(14, RoundingMode.DOWN));
|
|
|
+ fileEntity.setLongitude(BigDecimal.valueOf(CoordinateUtil.checkValidVal(exif.getLongitude())).setScale(14, RoundingMode.DOWN));
|
|
|
+ fileEntity.setLongitudeRef(exif.getLongitudeRef());
|
|
|
+ fileEntity.setLatitudeRef(exif.getLatitudeRef());
|
|
|
+ fileEntity.setMediaType(exif.getPictureType());
|
|
|
+ fileEntity.setPictureType(exif.getPictureType());
|
|
|
+ fileEntity.setDurationSeconds(exif.getDurationSeconds());
|
|
|
+
|
|
|
+ mapper.update(fileEntity,
|
|
|
+ new LambdaUpdateWrapper<MediaFileEntity>().eq(MediaFileEntity::getFileId, fileEntity.getFileId()));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
|
|
|
@@ -912,11 +917,137 @@ public class FileServiceImpl implements IFileService {
|
|
|
try {
|
|
|
mapper.deleteById(id);
|
|
|
ossService.deleteObject(OssConfiguration.bucket, entity.getObjectKey());
|
|
|
+ ossService.deleteObject(OssConfiguration.bucket, getThumbnailObjectKey(entity.getFileName()));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void importZipFile(MultipartFile file, String workspaceId, String creator) {
|
|
|
+
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ if (Objects.nonNull(filename) && !filename.toLowerCase().endsWith(ZIP_FILE_SUFFIX)) {
|
|
|
+ throw new RuntimeException("文件格式不正确.");
|
|
|
+ }
|
|
|
+ String dirName = filename.substring(0, filename.toLowerCase().indexOf(ZIP_FILE_SUFFIX));
|
|
|
+ MediaDirDTO dirDto = mediaDirService.getMediaDirByName(workspaceId,dirName,null);
|
|
|
+ if(dirDto == null) {
|
|
|
+ //文件夹不存在,则创建文件夹
|
|
|
+ if (dirDto == null) {
|
|
|
+ MediaDirEntity.MediaDirEntityBuilder dirBuilder = MediaDirEntity.builder();
|
|
|
+ dirBuilder.dirName(dirName)
|
|
|
+ .workspaceId(workspaceId)
|
|
|
+ .waylineName("N/A")
|
|
|
+ .createTime(System.currentTimeMillis())
|
|
|
+ .deviceName("N/A")
|
|
|
+ .payload("N/A")
|
|
|
+ .username(getUserName());
|
|
|
+ dirDto = mediaDirService.createDir(dirBuilder.build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ MediaDirDTO finalDirDto = dirDto;
|
|
|
+
|
|
|
+
|
|
|
+ try (ZipInputStream unzipFile = new ZipInputStream(file.getInputStream())) {
|
|
|
+ file.getInputStream().close();
|
|
|
+ ZipEntry nextEntry;
|
|
|
+ while ((nextEntry = unzipFile.getNextEntry()) != null) {
|
|
|
+
|
|
|
+ // 手动处理文件名的编码问题
|
|
|
+ log.info("===========正在处理文件====:" + nextEntry.getName());
|
|
|
+ String[] pathArray = nextEntry.getName().split("/");
|
|
|
+ String entryName = pathArray[pathArray.length - 1];
|
|
|
+ if (!entryName.toUpperCase().endsWith(JPG) && !entryName.toUpperCase().endsWith(MP4)) {
|
|
|
+ unzipFile.closeEntry();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 将 InputStream 读取到字节数组中
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int length;
|
|
|
+ try {
|
|
|
+ while ((length = unzipFile.read(buffer)) != -1) {
|
|
|
+ baos.write(buffer, 0, length);
|
|
|
+ }
|
|
|
+ unzipFile.closeEntry();
|
|
|
+ }catch (Exception e) {
|
|
|
+ log.error("===手工上传,将 InputStream 读取到字节数组中 异常");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
|
|
|
+ baos.close();
|
|
|
+ uploadOssAndSave(entryName, inputStream, finalDirDto, workspaceId);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void uploadOssAndSave(String entryName, InputStream inputStream, MediaDirDTO finalDirDto,String workspaceId) {
|
|
|
+ fileThreadPool.execute(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ String fileId = UUID.randomUUID().toString();
|
|
|
+ String objectKey = OssConfiguration.objectDirPrefix + File.separator + entryName;
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ ossService.putObject(OssConfiguration.bucket, objectKey, inputStream);
|
|
|
+
|
|
|
+ log.info("手动上传文件到oss成功. {}", entryName);
|
|
|
+
|
|
|
+ MediaFileEntity fileEntity = MediaFileEntity.builder()
|
|
|
+ .dirId(Integer.valueOf(finalDirDto.getId()))
|
|
|
+ .fileId(fileId)
|
|
|
+ .fileName(entryName)
|
|
|
+ .filePath(finalDirDto.getDirName())
|
|
|
+ .workspaceId(workspaceId)
|
|
|
+ .objectKey(objectKey)
|
|
|
+ .subFileType(0)
|
|
|
+ .isOriginal(true)
|
|
|
+ .size(ossService.getObjectSize(OssConfiguration.bucket, objectKey))
|
|
|
+ .createTime(System.currentTimeMillis())
|
|
|
+ .updateTime(System.currentTimeMillis())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ mapper.insert(fileEntity);
|
|
|
+
|
|
|
+ //生成缩略图
|
|
|
+ String tObjectKey = getThumbnailObjectKey(entryName);
|
|
|
+ putThumbnailImage(tObjectKey, objectKey, true);
|
|
|
+
|
|
|
+ //获取图片信息
|
|
|
+ MediaExifDTO exif = PicExifUtil.getPicExif(objectKey);
|
|
|
+// MediaExifDTO exif = PicExifUtil.getPicExif(inputStream, entryName.endsWith(MP4));
|
|
|
+
|
|
|
+ if(exif != null) {
|
|
|
+ fileEntity.setImageWidth(exif.getImageWidth());
|
|
|
+ fileEntity.setImageHeight(exif.getImageHeight());
|
|
|
+ fileEntity.setXResolution(exif.getXResolution());
|
|
|
+ fileEntity.setYResolution(exif.getYResolution());
|
|
|
+ fileEntity.setLatitude(BigDecimal.valueOf(CoordinateUtil.checkValidVal(exif.getLatitude())).setScale(14, RoundingMode.DOWN));
|
|
|
+ fileEntity.setLongitude(BigDecimal.valueOf(CoordinateUtil.checkValidVal(exif.getLongitude())).setScale(14, RoundingMode.DOWN));
|
|
|
+ fileEntity.setLongitudeRef(exif.getLongitudeRef());
|
|
|
+ fileEntity.setLatitudeRef(exif.getLatitudeRef());
|
|
|
+ fileEntity.setMediaType(exif.getPictureType());
|
|
|
+ fileEntity.setPictureType(exif.getPictureType());
|
|
|
+ fileEntity.setDurationSeconds(exif.getDurationSeconds());
|
|
|
+
|
|
|
+ mapper.update(fileEntity,
|
|
|
+ new LambdaUpdateWrapper<MediaFileEntity>().eq(MediaFileEntity::getFileId, fileEntity.getFileId()));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("手动上传文件失败. {}", entryName);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
}
|