Skip to content

Commit

Permalink
修复: 文件双击打开,编辑后保存线上文件并没有更新 #32
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlindM committed Dec 29, 2022
1 parent 6164482 commit ad2ba88
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions server/src/main/java/com/tshell/core/FileManagerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void upload(String channelId, UploadDTO uploadDTO) {
try (Stream<Path> paths = Files.walk(file.toPath())) {
paths.forEach((path -> {
File internalFile = path.toFile();
String relativePath = transformSeparators(StrUtil.subAfter(FileUtil.getCanonicalPath(internalFile), FileUtil.getAbsolutePath(file.getParentFile()), true), separator);
String relativePath = transformFileSeparators(StrUtil.subAfter(FileUtil.getCanonicalPath(internalFile), FileUtil.getAbsolutePath(file.getParentFile()), true), separator);
if (!relativePath.startsWith(separator)) {
relativePath = separator + relativePath;
}
Expand Down Expand Up @@ -181,37 +181,13 @@ public static String separatorsToWindows(String path) {
return path != null && path.indexOf(47) != -1 ? path.replace('/', '\\') : path;
}

public static String transformSeparators(String path, String separators) {
public static String transformFileSeparators(String path, String separators) {
if (path == null) {
return null;
} else {
return separators.indexOf(92) != -1 ? separatorsToWindows(path) : separatorsToUnix(path);
}
}
/* public String convertSeparators(String path,String separator){
PathUtils
String s3=s2.indexOf(90) != -1?s2.replace(, '/'):;
if(s2.indexOf(47) != -1 ){
s3= s2.replace('\\', '/');
}
// 不是 Windows //
if(path.indexOf(47) != -1 ){
return path.replace('\\', '/') : path;
}
String tempPath=path;
String tempSeparator=separator;
if(path.indexOf(47) != -1 ){
return path.replace('\\', '/') ;
}else {
}
}*/


public void removeFile(String channelId, String path) {
Expand Down Expand Up @@ -242,7 +218,7 @@ public void download(String channelId, String path,boolean isDirectory) {
FileManager fileManager = getFileManager(channelId);
if(isDirectory){
fileManager.fileInfos(path).forEach(fileInfo -> {
String relativePath = transformSeparators(StrUtil.subAfter(fileInfo.path(),path, true), File.separator);
String relativePath = transformFileSeparators(StrUtil.subAfter(fileInfo.path(),path, true), File.separator);
String completePath = Path.of(savePath, relativePath).toString();
if (fileInfo.type() == FileType.DIRECTORY) {
FileUtil.mkdir(completePath);
Expand Down Expand Up @@ -541,14 +517,17 @@ public TransferCountVO getTransferTaskCount(String channelId) {
}




public void openFile(String channelId, String path) {

String fileName = FileUtil.getName(path);
String savePath = Path.of(tempDir, fileName).toString();

TtyConnector tyConnector = getTyConnector(channelId);
FileManager fileManager = tyConnector.getFileManager();
String id = "%s-%s".formatted(tyConnector.getSessionId(), path);

String id = "%s-%s".formatted(tyConnector.getSessionId(), pathEncode(path));
String savePath = Path.of(tempDir, id).toString();

tempFileInfoCache.remove(id);
long size = fileManager.getSize(path);
Expand All @@ -574,6 +553,7 @@ public void openFile(String channelId, String path) {
}
WebSocket.sendMsg(channelId, WebSocket.MsgType.OPEN_FILE_PROGRESS, objectMapper.writeValueAsString(new Progress(100.00, channelId, fileName, TransferRecord.Status.COMPLETE, id)));
WebSocket.sendMsg(channelId, WebSocket.MsgType.OPEN_FILE, savePath);

tempFileInfoCache.put(id, new TempInfo(channelId, savePath, path));
watchTempFile();
} catch (IOException e) {
Expand Down Expand Up @@ -622,4 +602,8 @@ public void onModify(WatchEvent<?> event, Path currentPath) {
public void deleteTempDir() {
FileUtil.del(tempDir);
}

public String pathEncode(String path){
return path.indexOf(47) != -1 ? path.replace('/', '%') : path.replace('\\', '%');
}
}

0 comments on commit ad2ba88

Please sign in to comment.