Support for copying files from one filesystem to another
I'd like to copy a file from the MemoryFileSystem to the LocalFileSystem (basically, write it to disk). When I try to run the following, I get the below error (localFileSystemPath is '/Users/mikem/example.mp4')
File copyFile(File memoryFileSystemFile, String localFileSystemPath) {
file.copy(path);
}
FileSystemException (FileSystemException: No such file or directory, path = 'Users' (OS Error: No such file or directory, errno = 2))
I assume this happens because file.copy tries to copy into a new file on its own filesystem (MemoryFileSystem) but the local file system's /Users directory isn't there. What is the recommended way to copy a file from one FileSystem to another? And can this be done without making an extra copy of the data in memory?
The following does work, but requires the entire file to be read into memory before writing can even begin. Maybe this isn't an issue because it's already in memory (it's on a MemoryFileSystem), and the readAsBytesSync call shortcuts and returns a reference to the existing list of bytes? Just trying to figure out the best way to write a potentially very large file from memory to disk, and this doesn't seem like it. Can't this be done in chunks with Streams to avoid having to read the whole file at once?
File copyFile(File memoryFileSystemFile, String localFileSystemPath) {
List bytes = memoryFileSystemFile.readAsBytesSync();
File newFile = LocalFileSystem().file(localFileSystemPath);
newFile.writeAsBytesSync(bytes);
return newFile;
}