Skip to content

Commit ad2ba88

Browse files
committed
修复: 文件双击打开,编辑后保存线上文件并没有更新 #32
1 parent 6164482 commit ad2ba88

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

server/src/main/java/com/tshell/core/FileManagerService.java

+13-29
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void upload(String channelId, UploadDTO uploadDTO) {
145145
try (Stream<Path> paths = Files.walk(file.toPath())) {
146146
paths.forEach((path -> {
147147
File internalFile = path.toFile();
148-
String relativePath = transformSeparators(StrUtil.subAfter(FileUtil.getCanonicalPath(internalFile), FileUtil.getAbsolutePath(file.getParentFile()), true), separator);
148+
String relativePath = transformFileSeparators(StrUtil.subAfter(FileUtil.getCanonicalPath(internalFile), FileUtil.getAbsolutePath(file.getParentFile()), true), separator);
149149
if (!relativePath.startsWith(separator)) {
150150
relativePath = separator + relativePath;
151151
}
@@ -181,37 +181,13 @@ public static String separatorsToWindows(String path) {
181181
return path != null && path.indexOf(47) != -1 ? path.replace('/', '\\') : path;
182182
}
183183

184-
public static String transformSeparators(String path, String separators) {
184+
public static String transformFileSeparators(String path, String separators) {
185185
if (path == null) {
186186
return null;
187187
} else {
188188
return separators.indexOf(92) != -1 ? separatorsToWindows(path) : separatorsToUnix(path);
189189
}
190190
}
191-
/* public String convertSeparators(String path,String separator){
192-
PathUtils
193-
194-
String s3=s2.indexOf(90) != -1?s2.replace(, '/'):;
195-
if(s2.indexOf(47) != -1 ){
196-
s3= s2.replace('\\', '/');
197-
}
198-
// 不是 Windows //
199-
if(path.indexOf(47) != -1 ){
200-
return path.replace('\\', '/') : path;
201-
}
202-
203-
String tempPath=path;
204-
String tempSeparator=separator;
205-
if(path.indexOf(47) != -1 ){
206-
return path.replace('\\', '/') ;
207-
}else {
208-
209-
210-
}
211-
212-
213-
214-
}*/
215191

216192

217193
public void removeFile(String channelId, String path) {
@@ -242,7 +218,7 @@ public void download(String channelId, String path,boolean isDirectory) {
242218
FileManager fileManager = getFileManager(channelId);
243219
if(isDirectory){
244220
fileManager.fileInfos(path).forEach(fileInfo -> {
245-
String relativePath = transformSeparators(StrUtil.subAfter(fileInfo.path(),path, true), File.separator);
221+
String relativePath = transformFileSeparators(StrUtil.subAfter(fileInfo.path(),path, true), File.separator);
246222
String completePath = Path.of(savePath, relativePath).toString();
247223
if (fileInfo.type() == FileType.DIRECTORY) {
248224
FileUtil.mkdir(completePath);
@@ -541,14 +517,17 @@ public TransferCountVO getTransferTaskCount(String channelId) {
541517
}
542518

543519

520+
521+
544522
public void openFile(String channelId, String path) {
545523

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

549526
TtyConnector tyConnector = getTyConnector(channelId);
550527
FileManager fileManager = tyConnector.getFileManager();
551-
String id = "%s-%s".formatted(tyConnector.getSessionId(), path);
528+
529+
String id = "%s-%s".formatted(tyConnector.getSessionId(), pathEncode(path));
530+
String savePath = Path.of(tempDir, id).toString();
552531

553532
tempFileInfoCache.remove(id);
554533
long size = fileManager.getSize(path);
@@ -574,6 +553,7 @@ public void openFile(String channelId, String path) {
574553
}
575554
WebSocket.sendMsg(channelId, WebSocket.MsgType.OPEN_FILE_PROGRESS, objectMapper.writeValueAsString(new Progress(100.00, channelId, fileName, TransferRecord.Status.COMPLETE, id)));
576555
WebSocket.sendMsg(channelId, WebSocket.MsgType.OPEN_FILE, savePath);
556+
577557
tempFileInfoCache.put(id, new TempInfo(channelId, savePath, path));
578558
watchTempFile();
579559
} catch (IOException e) {
@@ -622,4 +602,8 @@ public void onModify(WatchEvent<?> event, Path currentPath) {
622602
public void deleteTempDir() {
623603
FileUtil.del(tempDir);
624604
}
605+
606+
public String pathEncode(String path){
607+
return path.indexOf(47) != -1 ? path.replace('/', '%') : path.replace('\\', '%');
608+
}
625609
}

0 commit comments

Comments
 (0)