export: clean up temp file after divelogs.de upload

This adds a cleanup function to be called after a divelogs.de upload
finishes (successful or not) to make sure the temporary zip file is
closed and removed.

Signed-off-by: Richard Fuchs <dfx@dfx.at>
This commit is contained in:
Richard Fuchs 2021-08-03 14:04:52 -04:00 committed by Dirk Hohndel
parent cf78e4cb20
commit f308a6b57b
2 changed files with 20 additions and 6 deletions

View file

@ -40,9 +40,7 @@ static QString makeTempFileName()
QTemporaryFile tmpfile; QTemporaryFile tmpfile;
tmpfile.setFileTemplate(QDir::tempPath() + "/divelogsde-upload.XXXXXXXX.dld"); tmpfile.setFileTemplate(QDir::tempPath() + "/divelogsde-upload.XXXXXXXX.dld");
tmpfile.open(); tmpfile.open();
QString filename(tmpfile.fileName()); return tmpfile.fileName();
tmpfile.close();
return filename;
} }
@ -196,6 +194,13 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
} }
void uploadDiveLogsDE::cleanupTempFile()
{
if (tempFile.isOpen())
tempFile.remove();
}
void uploadDiveLogsDE::uploadDives(const QString &filename, const QString &userid, const QString &password) void uploadDiveLogsDE::uploadDives(const QString &filename, const QString &userid, const QString &password)
{ {
QHttpPart part1, part2, part3; QHttpPart part1, part2, part3;
@ -219,12 +224,15 @@ void uploadDiveLogsDE::uploadDives(const QString &filename, const QString &useri
// prepare header with filename (of all dives) and pointer to file // prepare header with filename (of all dives) and pointer to file
args = "form-data; name=\"userfile\"; filename=\"" + filename + "\""; args = "form-data; name=\"userfile\"; filename=\"" + filename + "\"";
part1.setRawHeader("Content-Disposition", args.toLatin1()); part1.setRawHeader("Content-Disposition", args.toLatin1());
QFile *f = new QFile(filename);
if (!f->open(QIODevice::ReadOnly)) { // open new file for reading
cleanupTempFile();
tempFile.setFileName(filename);
if (!tempFile.open(QIODevice::ReadOnly)) {
qDebug() << "ERROR opening zip file: " << filename; qDebug() << "ERROR opening zip file: " << filename;
return; return;
} }
part1.setBodyDevice(f); part1.setBodyDevice(&tempFile);
multipart->append(part1); multipart->append(part1);
// Add userid // Add userid
@ -289,6 +297,7 @@ void uploadDiveLogsDE::uploadFinishedSlot()
QByteArray xmlData = reply->readAll(); QByteArray xmlData = reply->readAll();
reply->deleteLater(); reply->deleteLater();
reply = NULL; reply = NULL;
cleanupTempFile();
char *resp = xmlData.data(); char *resp = xmlData.data();
if (resp) { if (resp) {
char *parsed = strstr(resp, "<Login>"); char *parsed = strstr(resp, "<Login>");
@ -324,6 +333,7 @@ void uploadDiveLogsDE::uploadTimeoutSlot()
reply->deleteLater(); reply->deleteLater();
reply = NULL; reply = NULL;
} }
cleanupTempFile();
QString err(tr("divelogs.de not responding")); QString err(tr("divelogs.de not responding"));
report_error(err.toUtf8()); report_error(err.toUtf8());
emit uploadFinish(false, err); emit uploadFinish(false, err);
@ -337,6 +347,7 @@ void uploadDiveLogsDE::uploadErrorSlot(QNetworkReply::NetworkError error)
reply->deleteLater(); reply->deleteLater();
reply = NULL; reply = NULL;
} }
cleanupTempFile();
QString err(tr("network error %1").arg(error)); QString err(tr("network error %1").arg(error));
report_error(err.toUtf8()); report_error(err.toUtf8());
emit uploadFinish(false, err); emit uploadFinish(false, err);

View file

@ -4,6 +4,7 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QHttpMultiPart> #include <QHttpMultiPart>
#include <QTimer> #include <QTimer>
#include <QFile>
class uploadDiveLogsDE : public QObject { class uploadDiveLogsDE : public QObject {
@ -28,10 +29,12 @@ private:
uploadDiveLogsDE(); uploadDiveLogsDE();
void uploadDives(const QString &filename, const QString &userid, const QString &password); void uploadDives(const QString &filename, const QString &userid, const QString &password);
void cleanupTempFile();
// only to be used in desktop-widgets::subsurfacewebservices // only to be used in desktop-widgets::subsurfacewebservices
bool prepareDives(const QString &tempfile, bool selected); bool prepareDives(const QString &tempfile, bool selected);
QFile tempFile;
QNetworkReply *reply; QNetworkReply *reply;
QHttpMultiPart *multipart; QHttpMultiPart *multipart;
QTimer timeout; QTimer timeout;