[API] Let web music list read from database

Add a feature to retrieve user photos.
pull/4/head
肥宅虾哥 2023-12-08 02:18:35 +08:00 committed by GitHub
parent 0913ef2060
commit 0bf54e666b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 0 deletions

View File

@ -1,9 +1,12 @@
package icu.samnyan.aqua.api.controller.sega.game.maimai2;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import icu.samnyan.aqua.api.model.MessageResponse;
import icu.samnyan.aqua.api.model.ReducedPageResponse;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.ProfileResp;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.PhotoResp;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.external.ExternalUserData;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.external.Maimai2DataExport;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.external.Maimai2DataImport;
@ -21,9 +24,12 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.file.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author samnyan (privateamusement@protonmail.com)
@ -89,6 +95,37 @@ public class ApiMaimai2PlayerDataController {
return divMaxLength;
}
@GetMapping("userPhoto")
public PhotoResp getUserPhoto(@RequestParam long aimeId,
@RequestParam(required = false, defaultValue = "0") int imageIndex) {
List<String> matchedFiles = new ArrayList<>();
PhotoResp Photo = new PhotoResp();
try (Stream<Path> paths = Files.walk(Paths.get("data"))) {
matchedFiles = paths
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".jpg"))
.filter(path -> {
String fileName = path.getFileName().toString();
String[] parts = fileName.split("-");
return parts.length > 0 && parts[0].equals(String.valueOf(aimeId));
})
.map(Path::getFileName)
.map(Path::toString)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
Photo.setTotalImage(matchedFiles.size());
Photo.setImageIndex(imageIndex);
if(matchedFiles.size() > imageIndex){
byte[] targetImageContent = Files.readAllBytes(Paths.get("data/" + matchedFiles.get(imageIndex)));
String divData = Base64.getEncoder().encodeToString(targetImageContent);
Photo.setDivData(divData);
}
}
catch (IOException e) {
}
return Photo;
}
@GetMapping("profile")
public ProfileResp getProfile(@RequestParam long aimeId) {
return mapper.convert(userDataRepository.findByCard_ExtId(aimeId).orElseThrow(), new TypeReference<>() {