109 lines
2.7 KiB
C++
109 lines
2.7 KiB
C++
#include "immichpathtraverser.h"
|
|
#include "logger.h"
|
|
#include <QDateTime>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QSaveFile>
|
|
|
|
ImmichPathTraverser::ImmichPathTraverser(const ImmichConfig &configIn)
|
|
: PathTraverser(""),
|
|
config(configIn),
|
|
client(configIn),
|
|
cache(configIn)
|
|
{
|
|
loadAssets();
|
|
}
|
|
|
|
ImmichPathTraverser::~ImmichPathTraverser() {}
|
|
|
|
void ImmichPathTraverser::loadAssets()
|
|
{
|
|
assetIds.clear();
|
|
assetNames.clear();
|
|
assetDateTimes.clear();
|
|
QVector<ImmichAsset> assets = client.fetchAssets();
|
|
for (const auto &asset : assets)
|
|
{
|
|
if (asset.id.isEmpty())
|
|
continue;
|
|
assetIds.append(asset.id);
|
|
assetNames.insert(asset.id, asset.originalFileName);
|
|
if (!asset.exifDateTime.isEmpty())
|
|
assetDateTimes.insert(asset.id, asset.exifDateTime);
|
|
}
|
|
Log("Immich assets loaded: ", assetIds.size());
|
|
lastRefresh = QDateTime::currentDateTime();
|
|
}
|
|
|
|
QStringList ImmichPathTraverser::getImages() const
|
|
{
|
|
if (refreshDue())
|
|
{
|
|
Log("Immich refresh due, reloading assets.");
|
|
const_cast<ImmichPathTraverser*>(this)->loadAssets();
|
|
}
|
|
return assetIds;
|
|
}
|
|
|
|
const std::string ImmichPathTraverser::getImagePath(const std::string image) const
|
|
{
|
|
QString assetId = QString::fromStdString(image);
|
|
QString name = assetNames.value(assetId);
|
|
QString path = cache.getCachedPath(assetId, name, client);
|
|
if (!path.isEmpty())
|
|
{
|
|
QString dateTime = assetDateTimes.value(assetId);
|
|
if (!dateTime.isEmpty())
|
|
writeExifSidecar(path, dateTime);
|
|
}
|
|
return path.toStdString();
|
|
}
|
|
|
|
ImageDisplayOptions ImmichPathTraverser::UpdateOptionsForImage(const std::string& filename, const ImageDisplayOptions& options) const
|
|
{
|
|
Q_UNUSED(filename);
|
|
return options;
|
|
}
|
|
|
|
bool ImmichPathTraverser::refreshDue() const
|
|
{
|
|
if (config.refreshSeconds <= 0)
|
|
return false;
|
|
if (!lastRefresh.isValid())
|
|
return true;
|
|
return lastRefresh.secsTo(QDateTime::currentDateTime()) >= config.refreshSeconds;
|
|
}
|
|
|
|
bool ImmichPathTraverser::shouldReloadImages() const
|
|
{
|
|
return refreshDue();
|
|
}
|
|
|
|
void ImmichPathTraverser::writeExifSidecar(const QString &imagePath, const QString &dateTime) const
|
|
{
|
|
if (imagePath.isEmpty() || dateTime.isEmpty())
|
|
return;
|
|
if (imagePath.endsWith(".exif") || imagePath.endsWith(".skip"))
|
|
return;
|
|
|
|
QString sidecarPath = imagePath + ".exif";
|
|
QFileInfo info(sidecarPath);
|
|
if (info.exists())
|
|
{
|
|
QFile existing(sidecarPath);
|
|
if (existing.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
{
|
|
QString current = QString::fromUtf8(existing.readAll()).trimmed();
|
|
existing.close();
|
|
if (current == dateTime.trimmed())
|
|
return;
|
|
}
|
|
}
|
|
|
|
QSaveFile file(sidecarPath);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
return;
|
|
file.write(dateTime.trimmed().toUtf8());
|
|
file.commit();
|
|
}
|