summaryrefslogtreecommitdiffstats
path: root/src/gui/platform
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@qt.io>2021-11-09 13:10:13 +0100
committerMorten Johan Sørvig <morten.sorvig@qt.io>2021-11-26 10:04:31 +0100
commitf92d4ffeade1a95106dc127e3b686cc639a46de4 (patch)
tree67bc5ffbadbae214ad6015bff81df53f93100667 /src/gui/platform
parent59600a514ba99ed62b46237d8f160dea84474190 (diff)
wasm: Move streamFile() to qstdweb
This function is useful also outside of local file access, for example when reading clipboard (file) content. Change-Id: I132546deb6df2969467051c348c05d9331d2cfd2 Reviewed-by: David Skoland <david.skoland@qt.io> Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Diffstat (limited to 'src/gui/platform')
-rw-r--r--src/gui/platform/wasm/qwasmlocalfileaccess.cpp40
1 files changed, 1 insertions, 39 deletions
diff --git a/src/gui/platform/wasm/qwasmlocalfileaccess.cpp b/src/gui/platform/wasm/qwasmlocalfileaccess.cpp
index a5460f0ba7..e810a1ad8e 100644
--- a/src/gui/platform/wasm/qwasmlocalfileaccess.cpp
+++ b/src/gui/platform/wasm/qwasmlocalfileaccess.cpp
@@ -48,44 +48,6 @@ QT_BEGIN_NAMESPACE
namespace QWasmLocalFileAccess {
-void streamFile(const qstdweb::File &file, uint32_t offset, uint32_t length, char *buffer, const std::function<void ()> &completed)
-{
- // Read file in chunks in order to avoid holding two copies in memory at the same time
- const uint32_t chunkSize = 256 * 1024;
- const uint32_t end = offset + length;
- // assert end < file.size
- auto fileReader = std::make_shared<qstdweb::FileReader>();
-
- auto chunkCompleted = std::make_shared<std::function<void (uint32_t, char *buffer)>>();
- *chunkCompleted = [=](uint32_t chunkBegin, char *chunkBuffer) mutable {
-
- // Copy current chunk from JS memory to Wasm memory
- qstdweb::ArrayBuffer result = fileReader->result();
- qstdweb::Uint8Array(result).copyTo(chunkBuffer);
-
- // Read next chunk if not at buffer end
- uint32_t nextChunkBegin = std::min(chunkBegin + result.byteLength(), end);
- uint32_t nextChunkEnd = std::min(nextChunkBegin + chunkSize, end);
- if (nextChunkBegin == end) {
- completed();
- chunkCompleted.reset();
- return;
- }
- char *nextChunkBuffer = chunkBuffer + result.byteLength();
- fileReader->onLoad([=]() { (*chunkCompleted)(nextChunkBegin, nextChunkBuffer); });
- qstdweb::Blob blob = file.slice(nextChunkBegin, nextChunkEnd);
- fileReader->readAsArrayBuffer(blob);
- };
-
- // Read first chunk. First iteration is a dummy iteration with no available data.
- (*chunkCompleted)(offset, buffer);
-}
-
-void streamFile(const qstdweb::File &file, char *buffer, const std::function<void ()> &completed)
-{
- streamFile(file, 0, file.size(), buffer, completed);
-}
-
void readFiles(const qstdweb::FileList &fileList,
const std::function<char *(uint64_t size, const std::string name)> &acceptFile,
const std::function<void ()> &fileDataReady)
@@ -109,7 +71,7 @@ void readFiles(const qstdweb::FileList &fileList,
}
// Read file data into caller-provided buffer
- streamFile(file, buffer, [=]() {
+ file.stream(buffer, [=]() {
fileDataReady();
(*readFile)(fileIndex + 1);
});