diff --git a/diyfile-common/src/main/java/com/besscroft/diyfile/common/constant/storage/OneDriveConstants.java b/diyfile-common/src/main/java/com/besscroft/diyfile/common/constant/storage/OneDriveConstants.java index 54bed8e..af55f94 100644 --- a/diyfile-common/src/main/java/com/besscroft/diyfile/common/constant/storage/OneDriveConstants.java +++ b/diyfile-common/src/main/java/com/besscroft/diyfile/common/constant/storage/OneDriveConstants.java @@ -28,4 +28,10 @@ public interface OneDriveConstants { /** 文件操作 API,PATCH 更新资源,DELETE 删除资源 */ String DRIVER_ITEM_OPERATOR_URL = "https://graph.microsoft.com/v1.0/me/drive/root:{path}"; + /** 获取项目 API */ + String ITEM_URL = "https://graph.microsoft.com/v1.0/me/drive/root:{path}"; + + /** 移动文件或文件夹 API */ + String MOVE_URL = "https://graph.microsoft.com/v1.0/me/drive/items/{item-id}"; + } diff --git a/diyfile-common/src/main/java/com/besscroft/diyfile/common/util/PathUtils.java b/diyfile-common/src/main/java/com/besscroft/diyfile/common/util/PathUtils.java index ea58c1d..b0c675e 100644 --- a/diyfile-common/src/main/java/com/besscroft/diyfile/common/util/PathUtils.java +++ b/diyfile-common/src/main/java/com/besscroft/diyfile/common/util/PathUtils.java @@ -58,4 +58,14 @@ public class PathUtils { return !realPath.contains("."); } + /** + * 获取路径中的文件名 + * @param path 路径 + * @return 文件名 + */ + public static String getFileName(String path) { + int lastSlashIndex = path.lastIndexOf('/'); + return path.substring(lastSlashIndex + 1); + } + } diff --git a/diyfile-system/src/main/java/com/besscroft/diyfile/service/impl/SyncServiceImpl.java b/diyfile-system/src/main/java/com/besscroft/diyfile/service/impl/SyncServiceImpl.java index 9b320e2..bc854ba 100644 --- a/diyfile-system/src/main/java/com/besscroft/diyfile/service/impl/SyncServiceImpl.java +++ b/diyfile-system/src/main/java/com/besscroft/diyfile/service/impl/SyncServiceImpl.java @@ -32,7 +32,7 @@ public class SyncServiceImpl implements SyncService { Long beforeStorageId = storageService.getStorageIdByStorageKey(afterStorageKey); Long afterStorageId = storageService.getStorageIdByStorageKey(afterStorageKey); log.info("同步任务参数:{} {} {} {}", beforeStorageKey, beforePath, afterStorageKey, afterPath); - // TODO 同步操作 + // 同步操作 if (StrUtil.equals(beforeStorageKey, afterStorageKey)) { // 相同存储内移动文件 // 移动文件,调用对应存储 API 移动文件接口 diff --git a/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/base/AbstractOneDriveBaseService.java b/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/base/AbstractOneDriveBaseService.java index 0e27def..ebef9fd 100644 --- a/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/base/AbstractOneDriveBaseService.java +++ b/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/base/AbstractOneDriveBaseService.java @@ -65,6 +65,21 @@ public abstract class AbstractOneDriveBaseService exten */ public abstract void moveItem(String startPath, String endPath); + /** + * 获取项目 ItemId + * @param path 路径 + * @return 项目 ItemId + */ + protected String getItemId(String path) { + String itemUrl = OneDriveConstants.ITEM_URL.replace("{path}", path); + JSONObject result = JSONUtil.parseObj(OkHttps.sync(itemUrl) + .addHeader("Authorization", getAccessToken()) + .get().getBody().toString()); + String itemId = result.getStr("id"); + log.info("获取 OneDrive 项目 id:{}", itemId); + return itemId; + } + /** * 获取 OneDrive 驱动 id * @return OneDrive 驱动 id @@ -124,6 +139,7 @@ public abstract class AbstractOneDriveBaseService exten Map tokenResult = objectMapper.readValue(result.getBody().toString(), Map.class); String accessToken = tokenResult.get("access_token").toString(); caffeineCache.put(CacheConstants.ONEDRIVE_TOKEN + getStorageId(), accessToken); + log.info("accessToken 刷新成功:{}", accessToken); return accessToken; } catch (Exception e) { throw new DiyFileException(e.getMessage()); diff --git a/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/impl/OneDriveServiceImpl.java b/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/impl/OneDriveServiceImpl.java index 8acdbca..4a281ec 100644 --- a/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/impl/OneDriveServiceImpl.java +++ b/diyfile-system/src/main/java/com/besscroft/diyfile/storage/service/impl/OneDriveServiceImpl.java @@ -138,14 +138,23 @@ public class OneDriveServiceImpl extends AbstractOneDriveBaseService map = new HashMap<>(); + Map parentReference = new HashMap<>(); + parentReference.put("id", endItemId); + map.put("parentReference", parentReference); + if (!PathUtils.isFolder(startPath)) { + map.put("name", PathUtils.getFileName(PathUtils.decode(startPath))); } + JSONObject result = JSONUtil.parseObj(OkHttps.sync(moveUrl) + .addHeader("Authorization", getAccessToken()) + .bodyType("json") + .addBodyPara(map) + .patch().getBody().toString()); + log.info("移动文件结果:{}", result); } /** diff --git a/pom.xml b/pom.xml index f2f8cc4..3289c46 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.0.4 + 3.0.5 @@ -24,17 +24,17 @@ ${java.version} UTF-8 UTF-8 - 3.0.4 + 3.0.5 1.5.3.Final 0.2.0 3.5.3.1 - 2.0.2 + 2.0.4 1.34.0 - 5.8.13 + 5.8.15 1.4.6 - 6.4.0 + 6.4.1 3.5.3 - 3.1.4 + 3.1.5 3.16.0 2.3.1 1.1.1