Skip to content

Commit

Permalink
Fix #192 temp file names reused, temp files overwritten.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddennedy committed Feb 10, 2016
1 parent 79e80b3 commit 7d2f965
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 63 deletions.
17 changes: 5 additions & 12 deletions src/docks/encodedock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,15 @@ MeltJob* EncodeDock::createMeltJob(Mlt::Service* service, const QString& target,
}

// get temp filename
QTemporaryFile tmp(QDir::tempPath().append("/shotcut-XXXXXX"));
QTemporaryFile tmp;
tmp.open();
QString tmpName = tmp.fileName();
tmp.close();
tmpName.append(".mlt");
MLT.saveXML(tmpName, service);
MLT.saveXML(tmp.fileName(), service);

// parse xml
QFile f1(tmpName);
QFile f1(tmp.fileName());
f1.open(QIODevice::ReadOnly);
QDomDocument dom(tmpName);
QDomDocument dom(tmp.fileName());
dom.setContent(&f1);
f1.close();

Expand Down Expand Up @@ -631,13 +629,8 @@ MeltJob* EncodeDock::createMeltJob(Mlt::Service* service, const QString& target,
(mytarget.endsWith(".mp4") || mytarget.endsWith(".mov")))
consumerNode.setAttribute("strict", "experimental");

// save new xml
f1.open(QIODevice::WriteOnly);
QTextStream ts(&f1);
dom.save(ts, 2);
f1.close();

return new EncodeJob(target, tmpName);
return new EncodeJob(target, dom.toString(2));
}

void EncodeDock::runMelt(const QString& target, int realtime)
Expand Down
20 changes: 6 additions & 14 deletions src/jobs/encodejob.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015 Meltytech, LLC
* Copyright (c) 2012-2016 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -72,11 +72,9 @@ void EncodeJob::onVideoQualityTriggered()
reportPath += ".txt";

// Get temp filename for the new XML.
QTemporaryFile tmp(QDir::tempPath().append("/shotcut-XXXXXX"));
QTemporaryFile tmp;
tmp.open();
QString tmpName = tmp.fileName();
tmp.close();
tmpName.append(".mlt");

// Generate the XML for the comparison.
Mlt::Tractor tractor(MLT.profile());
Expand All @@ -88,12 +86,12 @@ void EncodeJob::onVideoQualityTriggered()
tractor.set_track(encoded, 1);
tractor.plant_transition(vqm);
vqm.set("render", 0);
MLT.saveXML(tmpName, &tractor);
MLT.saveXML(tmp.fileName(), &tractor);

// Add consumer element to XML.
QFile f1(tmpName);
QFile f1(tmp.fileName());
f1.open(QIODevice::ReadOnly);
QDomDocument dom(tmpName);
QDomDocument dom(tmp.fileName());
dom.setContent(&f1);
f1.close();

Expand All @@ -107,14 +105,8 @@ void EncodeJob::onVideoQualityTriggered()
consumerNode.setAttribute("real_time", -1);
consumerNode.setAttribute("terminate_on_pause", 1);

// Save the new XML.
f1.open(QIODevice::WriteOnly);
QTextStream ts(&f1);
dom.save(ts, 2);
f1.close();

// Create job and add it to the queue.
JOBS.add(new VideoQualityJob(objectName(), tmpName, reportPath));
JOBS.add(new VideoQualityJob(objectName(), dom.toString(2), reportPath));
}
}
}
20 changes: 11 additions & 9 deletions src/jobs/meltjob.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015 Meltytech, LLC
* Copyright (c) 2012-2016 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -24,25 +24,28 @@
#include <QApplication>
#include <QAction>
#include <QDialog>
#include <QDir>
#include <QDebug>
#include "mainwindow.h"
#include "dialogs/textviewerdialog.h"

MeltJob::MeltJob(const QString& name, const QString& xml)
: AbstractJob(name)
, m_xml(xml)
, m_xml(QDir::tempPath().append("/shotcut-XXXXXX.mlt"))
, m_isStreaming(false)
{
QAction* action = new QAction(tr("View XML"), this);
action->setToolTip(tr("View the MLT XML for this job"));
connect(action, SIGNAL(triggered()), this, SLOT(onViewXmlTriggered()));
m_standardActions << action;
m_xml.open();
m_xml.write(xml.toUtf8());
m_xml.close();
}

MeltJob::~MeltJob()
{
qDebug();
QFile::remove(m_xml);
}

void MeltJob::start()
Expand All @@ -57,7 +60,7 @@ void MeltJob::start()
QStringList args;
args << "-progress2";
args << "-abort";
args << m_xml;
args << xmlPath();
qDebug() << meltPath.absoluteFilePath() << args;
#ifdef Q_OS_WIN
if (m_isStreaming) args << "-getc";
Expand All @@ -69,12 +72,11 @@ void MeltJob::start()
AbstractJob::start();
}

QString MeltJob::xml() const
QString MeltJob::xml()
{
QFile f(m_xml);
f.open(QIODevice::ReadOnly);
QString s(f.readAll());
f.close();
m_xml.open();
QString s(m_xml.readAll());
m_xml.close();
return s;
}

Expand Down
10 changes: 5 additions & 5 deletions src/jobs/meltjob.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015 Meltytech, LLC
* Copyright (c) 2012-2016 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -20,6 +20,7 @@
#define MELTJOB_H

#include "abstractjob.h"
#include <QTemporaryFile>

class MeltJob : public AbstractJob
{
Expand All @@ -28,17 +29,16 @@ class MeltJob : public AbstractJob
MeltJob(const QString& name, const QString& xml);
virtual ~MeltJob();
void start();
QString xml() const;
QString xmlPath() const { return m_xml; }
QString xml();
QString xmlPath() const { return m_xml.fileName(); }
void setIsStreaming(bool streaming);

public slots:
void onViewXmlTriggered();

private:
QString m_xml;
QTemporaryFile m_xml;
bool m_isStreaming;

};

#endif // MELTJOB_H
6 changes: 3 additions & 3 deletions src/jobs/videoqualityjob.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015 Meltytech, LLC
* Copyright (c) 2012-2016 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -27,9 +27,9 @@
#include "mainwindow.h"
#include "dialogs/textviewerdialog.h"

VideoQualityJob::VideoQualityJob(const QString& name, const QString& xmlPath,
VideoQualityJob::VideoQualityJob(const QString& name, const QString& xml,
const QString& reportPath)
: MeltJob(name, xmlPath)
: MeltJob(name, xml)
, m_reportPath(reportPath)
{
QAction* action = new QAction(tr("Open"), this);
Expand Down
4 changes: 2 additions & 2 deletions src/jobs/videoqualityjob.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015 Meltytech, LLC
* Copyright (c) 2012-2016 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -25,7 +25,7 @@ class VideoQualityJob : public MeltJob
{
Q_OBJECT
public:
VideoQualityJob(const QString& name, const QString& xmlPath,
VideoQualityJob(const QString& name, const QString& xml,
const QString& reportPath);

private slots:
Expand Down
26 changes: 8 additions & 18 deletions src/qmltypes/qmlfilter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Meltytech, LLC
* Copyright (c) 2013-2016 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -182,28 +182,24 @@ void QmlFilter::analyze(bool isAudio)
Mlt::Service service(mlt_service(m_filter->get_data("service")));

// get temp filename for input xml
QTemporaryFile tmp(QDir::tempPath().append("/shotcut-XXXXXX"));
QTemporaryFile tmp;
tmp.open();
QString tmpName = tmp.fileName();
tmp.close();
tmpName.append(".mlt");
m_filter->set("results", NULL, 0);
int disable = m_filter->get_int("disable");
m_filter->set("disable", 0);
MLT.saveXML(tmpName, &service);
MLT.saveXML(tmp.fileName(), &service);
m_filter->set("disable", disable);

// get temp filename for output xml
QTemporaryFile tmpTarget(QDir::tempPath().append("/shotcut-XXXXXX"));
QTemporaryFile tmpTarget;
tmpTarget.open();
QString target = tmpTarget.fileName();
tmpTarget.close();
target.append(".mlt");

// parse xml
QFile f1(tmpName);
QFile f1(tmp.fileName());
f1.open(QIODevice::ReadOnly);
QDomDocument dom(tmpName);
QDomDocument dom(tmp.fileName());
dom.setContent(&f1);
f1.close();

Expand All @@ -221,15 +217,9 @@ void QmlFilter::analyze(bool isAudio)
else
consumerNode.setAttribute("audio_off", 1);
consumerNode.setAttribute("no_meta", 1);
consumerNode.setAttribute("resource", target);
consumerNode.setAttribute("resource", tmpTarget.fileName());

// save new xml
f1.open(QIODevice::WriteOnly);
QTextStream ts(&f1);
dom.save(ts, 2);
f1.close();

AbstractJob* job = new MeltJob(target, tmpName);
AbstractJob* job = new MeltJob(tmpTarget.fileName(), dom.toString(2));
if (job) {
AnalyzeDelegate* delegate = new AnalyzeDelegate(m_filter);
connect(job, &AbstractJob::finished, delegate, &AnalyzeDelegate::onAnalyzeFinished);
Expand Down

0 comments on commit 7d2f965

Please sign in to comment.