修复某些存储源对用户基路径处理异常 bug

This commit is contained in:
zhaojun
2025-07-26 21:27:27 +08:00
parent fe2921d9bb
commit ede6d0e525
6 changed files with 24 additions and 17 deletions
@@ -182,7 +182,8 @@ public abstract class AbstractMicrosoftDriveService<P extends MicrosoftDrivePara
@Override
public FileItemResult getFileItem(String pathAndName) {
return getOriginFileItem(pathAndName);
String fullPath = StringUtils.concat(getCurrentUserBasePath(), pathAndName);
return getOriginFileItem(fullPath);
}
@@ -123,7 +123,6 @@ public abstract class AbstractS3BaseFileService<P extends S3BaseParam> extends A
*/
public List<FileItemResult> s3FileList(String path) {
String bucketName = param.getBucketName();
path = StringUtils.trimStartSlashes(path);
String fullPath = StringUtils.concatTrimStartSlashes(param.getBasePath(), getCurrentUserBasePath(), path, StringUtils.SLASH);
List<FileItemResult> fileItemList = new ArrayList<>();
@@ -116,8 +116,8 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService<GoogleD
*
* @return 文件/文件夹 id
*/
private String getIdByPath(String path) {
String fullPath = StringUtils.concat(param.getBasePath(), getCurrentUserBasePath(), path);
private String getIdByPath(String path, boolean concatCurrentUserBasePath) {
String fullPath = StringUtils.concat(param.getBasePath(), concatCurrentUserBasePath ? getCurrentUserBasePath() : "", path);
if (StringUtils.isEmpty(fullPath) || StringUtils.equals(fullPath, StringUtils.SLASH)) {
return StringUtils.isEmpty(param.getDriveId()) ? "root" : param.getDriveId();
}
@@ -183,8 +183,6 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService<GoogleD
public FileItemResult getFileItem(String pathAndName) {
String fileId = getIdByPath(pathAndName);
String folderName = FileUtils.getParentPath(pathAndName);
HttpRequest httpRequest = commonHttpRequest(HttpUtil.createGet(DRIVE_FILE_URL + StringUtils.SLASH + fileId));
httpRequest.body("fields=id,name,mimeType,shortcutDetails,size,modifiedTime");
HttpResponse httpResponse = httpRequest.execute();
@@ -197,7 +195,8 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService<GoogleD
String body = httpResponse.body();
JSONObject jsonObject = JSON.parseObject(body);
return jsonObjectToFileItem(jsonObject, folderName);
String folderPath = FileUtils.getParentPath(pathAndName);
return jsonObjectToFileItem(jsonObject, folderPath);
}
@@ -297,7 +296,7 @@ public class GoogleDriveServiceImpl extends AbstractProxyTransferService<GoogleD
@Override
public ResponseEntity<Resource> downloadToStream(String pathAndName) {
String fileId = getIdByPath(pathAndName);
String fileId = getIdByPath(pathAndName, false);
HttpServletRequest request = RequestHolder.getRequest();
@@ -95,9 +95,8 @@ public class SftpServiceImpl extends AbstractProxyTransferService<SftpParam> {
}
@Override
public FileItemResult getFileItem(String pathAndName) {
String fullPath = StringUtils.concat(param.getBasePath(), getCurrentUserBasePath(), pathAndName);
public FileItemResult getFileItem(String pathAndName, boolean containUserBasePath) {
String fullPath = StringUtils.concat(param.getBasePath(), containUserBasePath ? getCurrentUserBasePath() : "", pathAndName);
Sftp sftp = null;
try {
@@ -121,6 +120,10 @@ public class SftpServiceImpl extends AbstractProxyTransferService<SftpParam> {
}
}
@Override
public FileItemResult getFileItem(String pathAndName) {
return getFileItem(pathAndName, true);
}
@Override
public boolean newFolder(String path, String name) {
@@ -230,7 +233,7 @@ public class SftpServiceImpl extends AbstractProxyTransferService<SftpParam> {
throw new BizException(ErrorCode.BIZ_UNSUPPORTED_PROXY_DOWNLOAD);
}
FileItemResult fileItem = getFileItem(pathAndName);
FileItemResult fileItem = getFileItem(pathAndName, false);
if (fileItem == null) {
throw new NotFoundAccessException(ErrorCode.BIZ_FILE_NOT_EXIST);
}
@@ -336,7 +336,7 @@ public class UpYunServiceImpl extends AbstractProxyTransferService<UpYunParam> {
@Override
public ResponseEntity<Resource> downloadToStream(String pathAndName) throws Exception {
String fullUrl = StringUtils.concat(param.getBasePath(),getCurrentUserBasePath(), pathAndName);
String fullUrl = StringUtils.concat(param.getBasePath(), pathAndName);
Response response = restManager.readFile(fullUrl);
InputStream inputStream = response.body().byteStream();
String fileName = FileUtils.getName(pathAndName);
@@ -5,6 +5,7 @@ import cn.hutool.core.util.URLUtil;
import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.impl.SardineException;
import com.github.sardine.impl.io.ContentLengthInputStream;
import im.zhaojun.zfile.core.util.CollectionUtils;
import im.zhaojun.zfile.core.util.FileUtils;
import im.zhaojun.zfile.core.util.RequestHolder;
@@ -92,8 +93,12 @@ public class WebdavServiceImpl extends AbstractProxyTransferService<WebdavParam>
@Override
public FileItemResult getFileItem(String pathAndName) {
return getFileItem(pathAndName, true);
}
public FileItemResult getFileItem(String pathAndName, boolean containUserBasePath) {
try {
String requestUrl = getRequestPath(pathAndName);
String requestUrl = getRequestPath(containUserBasePath, pathAndName);
List<DavResource> resources = sardine.list(requestUrl);
DavResource davResource = CollectionUtils.getLast(resources);
if (davResource == null) {
@@ -160,9 +165,9 @@ public class WebdavServiceImpl extends AbstractProxyTransferService<WebdavParam>
@Override
public ResponseEntity<Resource> downloadToStream(String pathAndName) throws IOException {
FileItemResult fileItem = getFileItem(pathAndName);
InputStream inputStream = sardine.get(getRequestPath(false, pathAndName));
RequestHolder.writeFile(inputStream, fileItem.getName(), fileItem.getSize(), false, param.isProxyLinkForceDownload());
ContentLengthInputStream inputStream = (ContentLengthInputStream) sardine.get(getRequestPath(false, pathAndName));
String fileName = FileUtils.getName(pathAndName);
RequestHolder.writeFile(inputStream, fileName, inputStream.getLength(), false, param.isProxyLinkForceDownload());
return null;
}