| | |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | |
| | | @Value("${app.media-url:${app.media.url:}}") |
| | | @Value("${app.media-url}") |
| | | private String mediaBaseUrl; |
| | | |
| | | public MediaGraphqlApi(MediaRepository mediaRepository, MediaService mediaService) { |
| | |
| | | } |
| | | |
| | | @QueryMapping |
| | | public List<MediaResponse> mediasByTarget(@Argument Integer targetType, @Argument String targetId) { |
| | | public List<MediaResponse> mediasByTarget(@Argument Integer targetType, @Argument Long targetId) { |
| | | try { |
| | | // 使用 JDBC 直接查询,避免 Hibernate 类型转换问题 |
| | | String sql = "SELECT id, name, path, file_size, file_ext, media_type, target_type, target_id, thumb_path, duration, description " + |
| | | "FROM t_media WHERE target_type = ? AND target_id = ? AND target_id REGEXP '^[0-9]+$'"; |
| | | "FROM t_media WHERE target_type = ? AND target_id = ?"; |
| | | |
| | | List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, targetType, Long.parseLong(targetId)); |
| | | List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, targetType, targetId); |
| | | |
| | | List<MediaResponse> responses = new ArrayList<>(); |
| | | for (Map<String, Object> row : rows) { |
| | |
| | | r.setMediaType(m.getMediaType()); |
| | | r.setTargetType(m.getTargetType()); |
| | | r.setTargetId(m.getTargetId()); |
| | | r.setThumbPath(m.getThumbPath()); |
| | | r.setDuration(m.getDuration()); |
| | | r.setDescription(m.getDescription()); |
| | | |
| | | // 追加 fullUrl(前端也可自行拼接) |
| | | if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && m.getPath() != null) { |
| | | String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl; |
| | | String p = m.getPath().startsWith("/") ? m.getPath() : ("/" + m.getPath()); |
| | | r.setFullUrl(base + p); |
| | | } |
| | | |
| | | // 追加 fullThumbUrl |
| | | if (mediaBaseUrl != null && !mediaBaseUrl.isEmpty() && m.getThumbPath() != null) { |
| | | String base = mediaBaseUrl.endsWith("/") ? mediaBaseUrl.substring(0, mediaBaseUrl.length() - 1) : mediaBaseUrl; |
| | | String p = m.getThumbPath().startsWith("/") ? m.getThumbPath() : ("/" + m.getThumbPath()); |
| | | r.setFullThumbUrl(base + p); |
| | | } |
| | | |
| | | return r; |
| | | } |
| | | |
| | |
| | | System.out.println("输入参数: " + input); |
| | | System.out.println("targetType: " + input.getTargetType()); |
| | | System.out.println("targetId: " + input.getTargetId()); |
| | | System.out.println("thumbPath: " + input.getThumbPath()); |
| | | |
| | | try { |
| | | Media result = mediaService.saveMedia( |
| | | input.getName(), |
| | | input.getPath(), |
| | | input.getFileSize(), |
| | | input.getFileExt(), |
| | | input.getMediaType(), |
| | | input.getTargetType(), |
| | | input.getTargetId() |
| | | ); |
| | | System.out.println("保存成功,媒体ID: " + result.getId()); |
| | | Media result; |
| | | |
| | | // 如果有缩略图路径,使用支持缩略图的方法 |
| | | if (input.getThumbPath() != null && !input.getThumbPath().trim().isEmpty()) { |
| | | result = mediaService.saveMedia( |
| | | input.getName(), |
| | | input.getPath(), |
| | | input.getFileSize(), |
| | | input.getFileExt(), |
| | | input.getMediaType(), |
| | | input.getTargetType(), |
| | | input.getTargetId(), |
| | | input.getThumbPath() |
| | | ); |
| | | System.out.println("保存成功(含缩略图),媒体ID: " + result.getId()); |
| | | } else { |
| | | result = mediaService.saveMedia( |
| | | input.getName(), |
| | | input.getPath(), |
| | | input.getFileSize(), |
| | | input.getFileExt(), |
| | | input.getMediaType(), |
| | | input.getTargetType(), |
| | | input.getTargetId() |
| | | ); |
| | | System.out.println("保存成功,媒体ID: " + result.getId()); |
| | | } |
| | | |
| | | return result; |
| | | } catch (Exception e) { |
| | | System.err.println("保存媒体失败: " + e.getMessage()); |
| | |
| | | } |
| | | |
| | | @MutationMapping |
| | | public Boolean deleteMedia(@Argument String id) { |
| | | public Boolean deleteMedia(@Argument Long id) { |
| | | System.out.println("=== deleteMedia GraphQL调用 ==="); |
| | | System.out.println("要删除的媒体ID: " + id); |
| | | |
| | | try { |
| | | Long mediaId = Long.parseLong(id); |
| | | System.out.println("转换后的媒体ID: " + mediaId); |
| | | |
| | | Boolean result = mediaService.deleteMedia(mediaId); |
| | | Boolean result = mediaService.deleteMedia(id); |
| | | System.out.println("删除结果: " + result); |
| | | return result; |
| | | } catch (Exception e) { |