Skip to content

Commit

Permalink
fix: temporary dirs are created by default
Browse files Browse the repository at this point in the history
Modify not to automatically create temporary directories.
* AIModelServiceData should not be created, but not fully
  tested.

Log: Not create temporary dirs by default
  • Loading branch information
rb-union committed Jul 26, 2024
1 parent a997933 commit 3e31964
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
42 changes: 27 additions & 15 deletions libimageviewer/service/aimodelservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,21 @@ AIModelServiceData::AIModelServiceData(AIModelService *q)
supportNameToModel = initDBusModelList();
qInfo() << qPrintable("Support image enhance models:") << supportNameToModel;

if (!enhanceTemp.isValid()) {
qWarning() << qPrintable("Create enhance temp dir failed") << enhanceTemp.errorString();
} else {
qInfo() << qPrintable("Enhance temp dir:") << enhanceTemp.path();
}
// QTemporaryDir::isValid() 会创建临时文件路径,在没有模型数据时不进行路径判断
if (!supportNameToModel.isEmpty()) {
enhanceTemp.reset(new QTemporaryDir);
if (!enhanceTemp->isValid()) {
qWarning() << qPrintable("Create enhance temp dir failed") << enhanceTemp->errorString();
} else {
qInfo() << qPrintable("Enhance temp dir:") << enhanceTemp->path();
}

if (!convertTemp.isValid()) {
qWarning() << qPrintable("Create convert temp dir failed") << convertTemp.errorString();
} else {
qInfo() << qPrintable("Convert temp dir:") << convertTemp.path();
convertTemp.reset(new QTemporaryDir);
if (!convertTemp->isValid()) {
qWarning() << qPrintable("Create convert temp dir failed") << convertTemp->errorString();
} else {
qInfo() << qPrintable("Convert temp dir:") << convertTemp->path();
}
}
}

Expand All @@ -82,7 +87,7 @@ QList<QPair<int, QString>> AIModelServiceData::initDBusModelList()

if (modelList.isEmpty()) {
auto error = interface.lastError();
qWarning() << QString("[Enhance DBus] Get model list failed, %1: %2").arg(error.name()).arg(error.message());
qInfo() << QString("[Enhance DBus] No AI models on device? Get model list failed, %1: %2").arg(error.name()).arg(error.message());
return {};
}

Expand Down Expand Up @@ -237,7 +242,7 @@ AIModelService *AIModelService::instance()
*/
bool AIModelService::isValid() const
{
return !dptr->supportNameToModel.isEmpty();
return dptr && !dptr->supportNameToModel.isEmpty();
}

/**
Expand Down Expand Up @@ -337,7 +342,10 @@ QString AIModelService::imageProcessing(const QString &filePath, int modelID, co

// 生命周期交由子线程维护
QImage caputureImage = image.copy();
dptr->lastOutput = dptr->enhanceTemp.filePath(QString("%1.png").arg(dptr->enhanceCache.size()));
if (!dptr->enhanceTemp) {
return {};
}
dptr->lastOutput = dptr->enhanceTemp->filePath(QString("%1.png").arg(dptr->enhanceCache.size()));
QString model = dptr->mapModelInfo.value(modelID)->model;

EnhancePtr ptr(new EnhanceInfo(sourceFile, dptr->lastOutput, model));
Expand Down Expand Up @@ -449,15 +457,15 @@ void AIModelService::cancelProcess(const QString &output)
*/
bool AIModelService::isTemporaryFile(const QString &filePath)
{
return dptr->enhanceCache.contains(filePath);
return isValid() && dptr->enhanceCache.contains(filePath);
}

/**
@return 若为图像增强文件,返回 \a filePath 指向的源文件,否则返回自身
*/
QString AIModelService::sourceFilePath(const QString &filePath)
{
if (dptr->enhanceCache.contains(filePath)) {
if (isValid() && dptr->enhanceCache.contains(filePath)) {
auto ptr = dptr->enhanceCache.value(filePath);
return ptr->source;
}
Expand Down Expand Up @@ -719,8 +727,12 @@ QString AIModelService::checkConvertFile(const QString &filePath, const QImage &
return {};
}

if (!dptr->convertTemp) {
return {};
}

QString cvtFile;
cvtFile = dptr->convertTemp.filePath(QString("%1_%2.png").arg(dptr->convertCache.size()).arg(QFileInfo(filePath).fileName()));
cvtFile = dptr->convertTemp->filePath(QString("%1_%2.png").arg(dptr->convertCache.size()).arg(QFileInfo(filePath).fileName()));

_locker.unlock();
if (!image.save(cvtFile, "PNG")) {
Expand Down
4 changes: 2 additions & 2 deletions libimageviewer/service/aimodelservice_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ class AIModelServiceData
QList<QPair<int, QString>> supportNameToModel; // 缓存的支持模型列表<模型ID,名称>

QString lastOutput; // 最近的图像增强输出文件
QTemporaryDir enhanceTemp; // 图像增强文件临时目录
QScopedPointer<QTemporaryDir> enhanceTemp; // 图像增强文件临时目录
QHash<QString, EnhancePtr> enhanceCache; // 图像增强缓存信息(仅主线程访问)

QMutex cacheMutex;
QTemporaryDir convertTemp; // 图像类型转换文件临时目录
QScopedPointer<QTemporaryDir> convertTemp; // 图像类型转换文件临时目录
QHash<QString, QString> convertCache; // 缓存的信息,可能多个线程访问

QFutureWatcher<EnhancePtr> enhanceWatcher;
Expand Down

0 comments on commit 3e31964

Please sign in to comment.