Check file transfer input before writing

This commit is contained in:
crschnick 2024-07-09 09:06:46 +00:00
parent 43a512bb30
commit 9f232aa7d1

View file

@ -7,9 +7,7 @@ import io.xpipe.core.store.FileNames;
import io.xpipe.core.store.FilePath;
import io.xpipe.core.store.FileSystem;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
@ -220,11 +218,28 @@ public class BrowserFileTransferOperation {
continue;
}
transfer(sourceFile, targetFile, transferred, totalSize, start);
}
}
updateProgress(BrowserTransferProgress.finished(source.getName(), totalSize.get()));
}
private void transfer(FileSystem.FileEntry sourceFile, String targetFile, AtomicLong transferred, AtomicLong totalSize, Instant start) throws Exception {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
var fileSize = sourceFile.getFileSystem().getFileSize(sourceFile.getPath());
inputStream = sourceFile.getFileSystem().openInput(sourceFile.getPath());
inputStream = new BufferedInputStream(sourceFile.getFileSystem().openInput(sourceFile.getPath()), 1024);
inputStream.mark(1024);
var streamStart = new byte[1024];
var streamStartLength = inputStream.read(streamStart, 0, 1024);
if (streamStartLength < 1024) {
inputStream.close();
inputStream = new ByteArrayInputStream(streamStart);
} else {
inputStream.reset();
}
outputStream = target.getFileSystem().openOutput(targetFile, fileSize);
transferFile(sourceFile, inputStream, outputStream, transferred, totalSize, start);
inputStream.transferTo(OutputStream.nullOutputStream());
@ -272,9 +287,6 @@ public class BrowserFileTransferOperation {
throw exception;
}
}
}
updateProgress(BrowserTransferProgress.finished(source.getName(), totalSize.get()));
}
private void deleteSingle(FileSystem.FileEntry source) throws Exception {
source.getFileSystem().delete(source.getPath());