完善 OneDrive 移动文件,升级一些依赖
This commit is contained in:
@@ -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}";
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 移动文件接口
|
||||
|
||||
@@ -65,6 +65,21 @@ public abstract class AbstractOneDriveBaseService<T extends OneDriveParam> 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<T extends OneDriveParam> 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());
|
||||
|
||||
@@ -138,14 +138,23 @@ public class OneDriveServiceImpl extends AbstractOneDriveBaseService<OneDrivePar
|
||||
|
||||
@Override
|
||||
public void moveItem(String startPath, String endPath) {
|
||||
// TODO 移动文件,需要先获取 item-id,@see https://learn.microsoft.com/zh-cn/graph/api/driveitem-move?view=graph-rest-1.0&tabs=http
|
||||
if (PathUtils.isFolder(startPath)) {
|
||||
// TODO 如果为文件夹,则需要递归移动文件夹下的所有文件
|
||||
|
||||
} else {
|
||||
// TODO 如果为文件,则直接移动文件
|
||||
|
||||
// 移动文件,需要先获取 item-id,@see https://learn.microsoft.com/zh-cn/graph/api/driveitem-move?view=graph-rest-1.0&tabs=http
|
||||
String startItemId = getItemId(startPath);
|
||||
String endItemId = getItemId(endPath);
|
||||
String moveUrl = OneDriveConstants.MOVE_URL.replace("{item-id}", startItemId);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Map<String, String> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
12
pom.xml
12
pom.xml
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.0.4</version>
|
||||
<version>3.0.5</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@@ -24,17 +24,17 @@
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>3.0.4</spring-boot.version>
|
||||
<spring-boot.version>3.0.5</spring-boot.version>
|
||||
<mapstruct.version>1.5.3.Final</mapstruct.version>
|
||||
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
|
||||
<mybatis-plus.version>3.5.3.1</mybatis-plus.version>
|
||||
<springdoc.version>2.0.2</springdoc.version>
|
||||
<springdoc.version>2.0.4</springdoc.version>
|
||||
<sa-token.version>1.34.0</sa-token.version>
|
||||
<hutool-all.version>5.8.13</hutool-all.version>
|
||||
<hutool-all.version>5.8.15</hutool-all.version>
|
||||
<pagehelper.version>1.4.6</pagehelper.version>
|
||||
<oshi.version>6.4.0</oshi.version>
|
||||
<oshi.version>6.4.1</oshi.version>
|
||||
<okhttps.version>3.5.3</okhttps.version>
|
||||
<caffeine.version>3.1.4</caffeine.version>
|
||||
<caffeine.version>3.1.5</caffeine.version>
|
||||
<aliyun-sdk-oss.version>3.16.0</aliyun-sdk-oss.version>
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
<activation.version>1.1.1</activation.version>
|
||||
|
||||
Reference in New Issue
Block a user