增加上传和获取应用图标
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.onekeycall.videotablet.controller.pub;
|
||||
|
||||
import com.onekeycall.videotablet.config.FilePath;
|
||||
import com.onekeycall.videotablet.entity.ApkIconFileInfo;
|
||||
import com.onekeycall.videotablet.entity.TabletDefaultSettings;
|
||||
import com.onekeycall.videotablet.mapper.TabletDefaultSettingsMapper;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.ApkIconService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
public class FileController {
|
||||
Logger logger = LoggerFactory.getLogger(FileController.class);
|
||||
|
||||
@Autowired
|
||||
private TabletDefaultSettingsMapper tabletDefaultSettingsMapper;
|
||||
@Autowired
|
||||
private ApkIconService apkIconService;
|
||||
|
||||
@PostMapping("/tablet/upload_avatar")
|
||||
public Result uploadAvatar(@RequestParam("file") MultipartFile multipartFile) {
|
||||
|
||||
String avatarPath = new FilePath().getAvatarPath();
|
||||
|
||||
File fileDir = new File(avatarPath);
|
||||
if (!fileDir.exists()) {
|
||||
fileDir.mkdirs();
|
||||
}
|
||||
|
||||
String originalFilename = multipartFile.getOriginalFilename();
|
||||
logger.info("originalFilename:" + originalFilename);
|
||||
File file = new File(avatarPath + File.separator + originalFilename);
|
||||
logger.info("file path = " + file.getAbsolutePath());
|
||||
|
||||
try {
|
||||
multipartFile.transferTo(file);
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage());
|
||||
return Result.error().message("upload avatarPath failed");
|
||||
}
|
||||
|
||||
// 检查是否存在默认设置记录
|
||||
TabletDefaultSettings tabletDefaultSettings = tabletDefaultSettingsMapper.getDefaultSettings();
|
||||
|
||||
if (tabletDefaultSettings == null) {
|
||||
// 如果不存在,则创建新记录
|
||||
tabletDefaultSettings = new TabletDefaultSettings();
|
||||
tabletDefaultSettings.setDefaultAvatar(originalFilename);
|
||||
tabletDefaultSettingsMapper.insertDefaultSettings(tabletDefaultSettings);
|
||||
} else {
|
||||
// 如果存在,则更新记录
|
||||
tabletDefaultSettings.setDefaultAvatar(originalFilename);
|
||||
tabletDefaultSettingsMapper.updateDefaultSettings(tabletDefaultSettings);
|
||||
}
|
||||
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/apk_icon/get_url")
|
||||
public ResponseEntity<?> getApkIconUrl(
|
||||
@RequestParam("package_name") String packageName) {
|
||||
|
||||
List<ApkIconFileInfo> apkIconFileInfoList = apkIconService.findByPackageName(packageName);
|
||||
if (apkIconFileInfoList.isEmpty()) {
|
||||
return new ResponseEntity<>(Result.notFound().message("未找到图标"), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
Optional<ApkIconFileInfo> apkIconFileInfoOptional = apkIconFileInfoList.stream().max(new Comparator<ApkIconFileInfo>() {
|
||||
@Override
|
||||
public int compare(ApkIconFileInfo o1, ApkIconFileInfo o2) {
|
||||
return Long.compare(o1.getVersionCode(), o2.getVersionCode());
|
||||
}
|
||||
});
|
||||
ApkIconFileInfo apkIconFileInfo = apkIconFileInfoOptional.get();
|
||||
File file = new File(FilePath.getApkIconPath(), apkIconFileInfo.getFileName());
|
||||
if (!file.exists()) {
|
||||
return new ResponseEntity<>(Result.notFound().message("未找到图标"), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
// 包装文件资源
|
||||
Resource resource = new FileSystemResource(file);
|
||||
|
||||
// 设置响应头(让浏览器下载而非直接打开)
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"attachment; filename=\"" + resource.getFilename() + "\"")
|
||||
.body(resource);
|
||||
}
|
||||
}
|
||||
@@ -35,50 +35,8 @@ public class ManageSnController {
|
||||
@Autowired
|
||||
private DeviceSnService deviceSnService;
|
||||
@Autowired
|
||||
private TabletDefaultSettingsMapper tabletDefaultSettingsMapper;
|
||||
@Autowired
|
||||
private TabletDefaultSettingsRepository tabletDefaultSettingsRepository;
|
||||
|
||||
@PostMapping("/tablet/upload_avatar")
|
||||
public Result uploadAvatar(@RequestParam("file") MultipartFile multipartFile) {
|
||||
String projectPath = System.getProperty("user.dir");
|
||||
|
||||
String avatarPath = projectPath + File.separator + FilePath.getAvatarPath();
|
||||
|
||||
File fileDir = new File(avatarPath);
|
||||
if (!fileDir.exists()) {
|
||||
fileDir.mkdirs();
|
||||
}
|
||||
|
||||
String originalFilename = multipartFile.getOriginalFilename();
|
||||
logger.info("originalFilename:" + originalFilename);
|
||||
File file = new File(avatarPath + File.separator + originalFilename);
|
||||
logger.info("file path = " + file.getAbsolutePath());
|
||||
|
||||
try {
|
||||
multipartFile.transferTo(file);
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage());
|
||||
return Result.error().message("upload avatarPath failed");
|
||||
}
|
||||
|
||||
// 检查是否存在默认设置记录
|
||||
TabletDefaultSettings tabletDefaultSettings = tabletDefaultSettingsMapper.getDefaultSettings();
|
||||
|
||||
if (tabletDefaultSettings == null) {
|
||||
// 如果不存在,则创建新记录
|
||||
tabletDefaultSettings = new TabletDefaultSettings();
|
||||
tabletDefaultSettings.setDefaultAvatar(originalFilename);
|
||||
tabletDefaultSettingsMapper.insertDefaultSettings(tabletDefaultSettings);
|
||||
} else {
|
||||
// 如果存在,则更新记录
|
||||
tabletDefaultSettings.setDefaultAvatar(originalFilename);
|
||||
tabletDefaultSettingsMapper.updateDefaultSettings(tabletDefaultSettings);
|
||||
}
|
||||
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
@PostMapping("/add_sn")
|
||||
public Result addSn(
|
||||
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
package com.onekeycall.videotablet.controller.sn;
|
||||
|
||||
import com.onekeycall.videotablet.config.FilePath;
|
||||
import com.onekeycall.videotablet.entity.ApkIconFileInfo;
|
||||
import com.onekeycall.videotablet.entity.Contact;
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
import com.onekeycall.videotablet.entity.DeviceLocation;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.ApkIconService;
|
||||
import com.onekeycall.videotablet.service.ContactService;
|
||||
import com.onekeycall.videotablet.service.DeviceLocationService;
|
||||
import com.onekeycall.videotablet.service.DeviceSnService;
|
||||
import com.onekeycall.videotablet.utils.HashUtils;
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
import com.onekeycall.videotablet.utils.TextUtils;
|
||||
import jakarta.validation.Valid;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,7 +36,10 @@ public class DevicesController {
|
||||
private DeviceLocationService deviceLocationService;
|
||||
@Autowired
|
||||
private ContactService contactService;
|
||||
@Autowired
|
||||
private ApkIconService apkIconService;
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(DevicesController.class);
|
||||
|
||||
@PostMapping("/update_location")
|
||||
public Result updateLocation(
|
||||
@@ -98,4 +110,50 @@ public class DevicesController {
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/upload_apk_icon")
|
||||
public Result uploadApkIcon(
|
||||
@RequestPart(value = "file") MultipartFile file,
|
||||
@RequestParam(value = "package_name") String packageName,
|
||||
@RequestParam(value = "version_code") Long versionCode,
|
||||
@RequestParam(value = "md5") String md5
|
||||
) throws Exception {
|
||||
|
||||
String iconPath = FilePath.getApkIconPath();
|
||||
logger.info("uploadApkIcon, iconPath: {}", iconPath);
|
||||
File fileDir = new File(iconPath);
|
||||
if (!fileDir.exists()) {
|
||||
fileDir.mkdirs();
|
||||
}
|
||||
|
||||
String fileMd5 = HashUtils.calculateMultipartFileMd5(file);
|
||||
|
||||
if (!fileMd5.equals(md5)) {
|
||||
return Result.error().message("file md5 not match");
|
||||
}
|
||||
|
||||
logger.info("uploadApkIcon, fileMd5: {}", fileMd5);
|
||||
if (apkIconService.existsByPackageNameAndMd5(packageName, md5)) {
|
||||
return Result.error().message("apk icon already exists");
|
||||
}
|
||||
|
||||
String originName = file.getOriginalFilename();
|
||||
String fileExtension = FilenameUtils.getExtension(originName);
|
||||
if (TextUtils.isEmpty(fileExtension)) {
|
||||
return Result.error().message("file extension is empty");
|
||||
}
|
||||
String fileName = packageName + "_" + md5 + "." + fileExtension;
|
||||
File destFile = new File(fileDir, fileName);
|
||||
file.transferTo(destFile);
|
||||
|
||||
ApkIconFileInfo apkIconFileInfo = new ApkIconFileInfo();
|
||||
apkIconFileInfo.setPackageName(packageName);
|
||||
apkIconFileInfo.setVersionCode(versionCode);
|
||||
apkIconFileInfo.setMd5(md5);
|
||||
apkIconFileInfo.setOriginFileName(originName);
|
||||
apkIconFileInfo.setFileName(fileName);
|
||||
apkIconFileInfo.setFilSize(file.getSize());
|
||||
apkIconService.save(apkIconFileInfo);
|
||||
|
||||
return Result.ok();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user