Do not use libcurl for reading local files

We originally used libcurl for both downloading images from
Internet and reading local files to have the same code path for both.

It doesn't work that well in practice, as Qt and libcurl are not
on the same page how special characters such as Chinese characters
are represented in a local file URL.

So create a new class to handle local files.

Closes #76
This commit is contained in:
Floris Bos 2020-06-01 19:43:51 +02:00
parent 123542a66b
commit fa7637e7dc
6 changed files with 115 additions and 8 deletions

View file

@ -0,0 +1,74 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020 Raspberry Pi (Trading) Limited
*/
#include "localfileextractthread.h"
#include "config.h"
LocalFileExtractThread::LocalFileExtractThread(const QByteArray &url, const QByteArray &dst, const QByteArray &expectedHash, QObject *parent)
: DownloadExtractThread(url, dst, expectedHash, parent)
{
_inputBuf = (char *) qMallocAligned(IMAGEWRITER_UNCOMPRESSED_BLOCKSIZE, 4096);
}
LocalFileExtractThread::~LocalFileExtractThread()
{
_cancelled = true;
wait();
qFreeAligned(_inputBuf);
}
void LocalFileExtractThread::_cancelExtract()
{
_cancelled = true;
if (_inputfile.isOpen())
_inputfile.close();
}
void LocalFileExtractThread::run()
{
if (isImage() && !_openAndPrepareDevice())
return;
_timer.start();
_inputfile.setFileName( QUrl(_url).toLocalFile() );
if (!_inputfile.open(_inputfile.ReadOnly))
{
_onDownloadError(tr("Error opening image file"));
return;
}
_lastDlTotal = _inputfile.size();
if (isImage())
extractImageRun();
else
extractMultiFileRun();
}
ssize_t LocalFileExtractThread::_on_read(struct archive *, const void **buff)
{
if (_cancelled)
return -1;
*buff = _inputBuf;
ssize_t len = _inputfile.read(_inputBuf, IMAGEWRITER_UNCOMPRESSED_BLOCKSIZE);
if (len > 0)
{
_lastDlNow += len;
if (!_isImage)
{
_inputHash.addData(_inputBuf, len);
}
}
return len;
}
int LocalFileExtractThread::_on_close(struct archive *)
{
_inputfile.close();
return 0;
}