From 71a4fdfbaf762dd74d31c14f068af0a3ac1017ed Mon Sep 17 00:00:00 2001 From: zhaojun <873019219@qq.com> Date: Sun, 5 Mar 2023 15:33:14 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=20GoogleDrive=20?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E6=96=B9=E5=BC=8F=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E8=8E=B7=E5=8F=96=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E7=9A=84=20BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/GoogleDriveServiceImpl.java | 262 +++++++++--------- 1 file changed, 137 insertions(+), 125 deletions(-) diff --git a/src/main/java/im/zhaojun/zfile/module/storage/service/impl/GoogleDriveServiceImpl.java b/src/main/java/im/zhaojun/zfile/module/storage/service/impl/GoogleDriveServiceImpl.java index 7b187ee..2e7cc2d 100644 --- a/src/main/java/im/zhaojun/zfile/module/storage/service/impl/GoogleDriveServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/module/storage/service/impl/GoogleDriveServiceImpl.java @@ -1,5 +1,6 @@ package im.zhaojun.zfile.module.storage.service.impl; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.IdUtil; @@ -57,6 +58,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; /** * @author zhaojun @@ -65,41 +67,41 @@ import java.util.List; @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Slf4j public class GoogleDriveServiceImpl extends AbstractProxyTransferService implements RefreshTokenService { - + /** * 文件类型:文件夹 */ private static final String FOLDER_MIME_TYPE = "application/vnd.google-apps.folder"; - + /** * 文件类型:快捷方式 */ private static final String SHORTCUT_MIME_TYPE = "application/vnd.google-apps.shortcut"; - + /** * 文件基础操作 API */ private static final String DRIVE_FILE_URL = "https://www.googleapis.com/drive/v3/files"; - - + + /** * 文件上传操作 API */ private static final String DRIVE_FILE_UPLOAD_URL = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"; - + /** * 刷新 AccessToken URL */ private static final String REFRESH_TOKEN_URL = "https://oauth2.googleapis.com/token"; - + @javax.annotation.Resource private StorageSourceConfigService storageSourceConfigService; - + @Override public void init() { refreshAccessToken(); } - + /** * 根据路径获取文件/文件夹 id * @@ -113,39 +115,44 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService pathList = StrUtil.splitTrim(fullPath, "/"); - + String driveId = ""; for (String subPath : pathList) { String folderIdParam = new GoogleDriveAPIParam().getDriveIdByPathParam(subPath, driveId); HttpRequest httpRequest = HttpUtil.createGet(DRIVE_FILE_URL); httpRequest.header("Authorization", "Bearer " + param.getAccessToken()); httpRequest.body(folderIdParam); - + HttpResponse httpResponse = httpRequest.execute(); - + checkHttpResponseIsError(httpResponse); - + String body = httpResponse.body(); - - JSONObject jsonObject = JSON.parseObject(body); - JSONArray files = jsonObject.getJSONArray("files"); - + + JSONObject jsonRoot = JSON.parseObject(body); + JSONArray files = jsonRoot.getJSONArray("files"); + if (files.size() == 0) { throw ExceptionUtil.wrapRuntime(new FileNotFoundException()); } - - driveId = files.getJSONObject(0).getString("id"); + + JSONObject jsonLastItem = files.getJSONObject(files.size() - 1); + if (jsonLastItem.containsKey("shortcutDetails")) { + driveId = jsonLastItem.getJSONObject("shortcutDetails").getString("targetId"); + } else { + driveId = jsonLastItem.getString("id"); + } } - + return driveId; } - + @Override public List fileList(String folderPath) throws Exception { List result = new ArrayList<>(); - + String folderId = getIdByPath(folderPath); String pageToken = ""; do { @@ -153,41 +160,41 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService downloadToStream(String pathAndName) { String fileId = getIdByPath(pathAndName); - + HttpServletRequest request = RequestHolder.getRequest(); - + HttpRequest httpRequest = HttpUtil.createGet(DRIVE_FILE_URL + "/" + fileId); httpRequest.header("Authorization", "Bearer " + param.getAccessToken()); httpRequest.body("alt=media"); httpRequest.header(HttpHeaders.RANGE, request.getHeader(HttpHeaders.RANGE)); HttpResponse httpResponse = httpRequest.executeAsync(); checkHttpResponseIsError(httpResponse); - + + try { HttpServletResponse response = RequestHolder.getResponse(); - response.setHeader(HttpHeaders.CONTENT_RANGE, httpResponse.header(HttpHeaders.CONTENT_RANGE)); - response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); - response.setHeader(HttpHeaders.CONTENT_LENGTH, httpResponse.header(HttpHeaders.CONTENT_LENGTH)); - response.setContentType(httpResponse.header(HttpHeaders.CONTENT_TYPE)); + response.setStatus(httpResponse.getStatus()); + for (Map.Entry> stringListEntry : httpResponse.headers().entrySet()) { + String key = stringListEntry.getKey(); + List values = stringListEntry.getValue(); + if (key != null && CollUtil.isNotEmpty(values)) { + response.setHeader(key, stringListEntry.getValue().get(0)); + } + } OutputStream outputStream = response.getOutputStream(); httpResponse.writeBody(outputStream, true, null); } catch (IOException e) { @@ -299,13 +311,13 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService jsonArrayToFileList(JSONArray jsonArray, String folderPath) { ArrayList fileList = new ArrayList<>(); - + for (int i = 0; i < jsonArray.size(); i++) { fileList.add(jsonObjectToFileItem(jsonArray.getJSONObject(i), folderPath)); } - + return fileList; } - - + + /** * 转换 api 返回的 json object 为 zfile 文件对象 * @@ -415,62 +427,62 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService