From a4a236e4888463eb36decde292ca053b35f11a8e Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Sun, 26 Jan 2020 13:21:49 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=20OneDrive=20?= =?UTF-8?q?=E5=88=97=E7=9B=AE=E5=BD=95,=20=E6=96=87=E4=BB=B6=E6=95=B0?= =?UTF-8?q?=E8=B6=85=E5=87=BA=20200=20=E4=B8=AA=E6=97=A0=E6=B3=95=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=9A=84=20BUG.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../onedrive/service/OneDriveService.java | 63 ++++++++++++------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/main/java/im/zhaojun/onedrive/service/OneDriveService.java b/src/main/java/im/zhaojun/onedrive/service/OneDriveService.java index 894766e..f32a0d2 100644 --- a/src/main/java/im/zhaojun/onedrive/service/OneDriveService.java +++ b/src/main/java/im/zhaojun/onedrive/service/OneDriveService.java @@ -28,13 +28,13 @@ import java.util.List; @Service public class OneDriveService { - public static final String DRIVER_INFO_URL = "https://graph.microsoft.com/v1.0/drive"; + private static final String DRIVER_INFO_URL = "https://graph.microsoft.com/v1.0/drive"; - public static final String DRIVER_ROOT_URL = "https://graph.microsoft.com/v1.0/drive/root/children"; + private static final String DRIVER_ROOT_URL = "https://graph.microsoft.com/v1.0/drive/root/children"; - public static final String DRIVER_ITEMS_URL = "https://graph.microsoft.com/v1.0/drive/root:{path}:/children"; + private static final String DRIVER_ITEMS_URL = "https://graph.microsoft.com/v1.0/drive/root:{path}:/children"; - public static final String AUTHENTICATE_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token"; + private static final String AUTHENTICATE_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token"; @Value("${zfile.onedirve.clientId}") private String clientId; @@ -91,32 +91,49 @@ public class OneDriveService { } public List list(String path) { - ResponseEntity responseEntity = oneDriveRestTemplate.getForEntity("/".equalsIgnoreCase(path) ? DRIVER_ROOT_URL : DRIVER_ITEMS_URL, String.class, path); - String body = responseEntity.getBody(); - - JSONObject root = JSON.parseObject(body); - - JSONArray fileList = root.getJSONArray("value"); List result = new ArrayList<>(); + String nextLink = null; - for (int i = 0; i < fileList.size(); i++) { + do { - FileItemDTO fileItemDTO = new FileItemDTO(); - JSONObject fileItem = fileList.getJSONObject(i); - fileItemDTO.setName(fileItem.getString("name")); - fileItemDTO.setSize(fileItem.getLong("size")); - fileItemDTO.setTime(fileItem.getDate("lastModifiedDateTime")); + String requestUrl; - if (fileItem.containsKey("file")) { - fileItemDTO.setUrl(fileItem.getString("@microsoft.graph.downloadUrl")); - fileItemDTO.setType(FileTypeEnum.FILE); + if (nextLink != null) { + requestUrl = nextLink; + }else if ("/".equalsIgnoreCase(path)) { + requestUrl = DRIVER_ROOT_URL; } else { - fileItemDTO.setType(FileTypeEnum.FOLDER); + requestUrl = DRIVER_ITEMS_URL; } - fileItemDTO.setPath(path); - result.add(fileItemDTO); - } + ResponseEntity responseEntity = oneDriveRestTemplate.getForEntity(requestUrl, String.class, path); + String body = responseEntity.getBody(); + + JSONObject root = JSON.parseObject(body); + + nextLink = root.getString("@odata.nextLink"); + + JSONArray fileList = root.getJSONArray("value"); + + for (int i = 0; i < fileList.size(); i++) { + + FileItemDTO fileItemDTO = new FileItemDTO(); + JSONObject fileItem = fileList.getJSONObject(i); + fileItemDTO.setName(fileItem.getString("name")); + fileItemDTO.setSize(fileItem.getLong("size")); + fileItemDTO.setTime(fileItem.getDate("lastModifiedDateTime")); + + if (fileItem.containsKey("file")) { + fileItemDTO.setUrl(fileItem.getString("@microsoft.graph.downloadUrl")); + fileItemDTO.setType(FileTypeEnum.FILE); + } else { + fileItemDTO.setType(FileTypeEnum.FOLDER); + } + + fileItemDTO.setPath(path); + result.add(fileItemDTO); + } + } while (nextLink != null); return result; }