retro-imager/localfileextractthread.cpp
Floris Bos fa7637e7dc 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
2020-06-01 19:43:51 +02:00

74 lines
1.6 KiB
C++

/*
* 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;
}